Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )

Advanced DNS Management
New ZoneEdit. New Managment.


Sign Up Now
> Shell Script to Move Files between Folders
post Feb 18 2009, 04:07 PM
Post #1

Whats this Lie-nix Thing?

Group: Members
Posts: 3
Joined: 18-February 09
Member No.: 13,913


I have a folder/file structure like this:


I would like to move all the .srt and .avi files into the root Folder1 and delete the subfolders Folder11, Folder12 etc. even if they have contents. The new folder/file structure should look like this:


What would a shell script look like to do this?

Also, how would I modify this script if I wanted to move only one file (example: file11.avi) rather than two (example: file11.avi and from the subfolders into the root folder.

What if I want to move three files instead of one or two, from each subfolder into the root folder?

I would appreciate any help.

I am sunning SUSE Linux 11

Go to the top of the page
+Quote Post
Start new topic
post Feb 19 2009, 11:36 AM
Post #2

Its GNU/

Group: Support Specialist
Posts: 1,807
Joined: 23-January 03
Member No.: 360

For starters.

I found this script but have not tested it.
for direct in `find -name '*' -printf '%h\n' > TEMPFILE.txt; cat TEMPFILE.txt | sort -u`; do curdirect=`pwd`; cd "$direct"; mv -t "DESTINATION DIRECTORY" *; cd "$curdirect"; done

You run the above code from within the directory which contains the subfolders you wish to move. If you are moving the subfolders up into the directory you are working from (say, '/home/user/photos/holidays' to '/home/user/photos'), you can replace 'DESTINATION DIRECTORY' with the variable $curdirect.
This method avoids the problem of copying an entire directory's subfolders and potentially taking up too much disk space.
The other thing is, you can change the asterisk in the 'find' command to search for specific files, such as *.jpg, *.pdf, whatever.

You can also use the cp command to recursively copy files and then delete the old ones.

Go to the top of the page
+Quote Post

Posts in this topic

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:


RSS Lo-Fi Version Time is now: 22nd April 2018 - 05:26 PM