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.

FREE DNS Is Back

Sign Up Now
> Shell Script to Move Files between Folders
taylormia
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



Hello,

I have a folder/file structure like this:

Folder1/Folder11/file11.avi
Folder1/Folder11/file11.srt
Folder1/Folder12/file12.avi
Folder1/Folder12/file12.srt
.
.
.etc.

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:

Folder1/file11.avi
Folder1/file11.srt
Folder1/file12.avi
Folder1/file12.srt


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 file11.srt) 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

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


Its GNU/Linuxhelp.net
*******

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



For starters.
http://tldp.org/LDP/abs/html/

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: 23rd October 2017 - 06:49 AM