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 (1 - 4)
michaelk
post Feb 18 2009, 06:41 PM
Post #2


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

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



Homework? If so post what you have done so far.
Go to the top of the page
 
+Quote Post
taylormia
post Feb 18 2009, 10:39 PM
Post #3


Whats this Lie-nix Thing?
*

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



QUOTE (michaelk @ Feb 18 2009, 05:41 PM) *
Homework? If so post what you have done so far.

Homework ?? What do you mean ?

I have a large number of media files (>1000) in a folder structure that I need to change to be compatible with a media metadata organizer application. I'm a beginner to scripting and haven't used variables or if..then or for...do type of statements before. I know I need to use these statements to perform the repetitive actions required to find each file and move it to the root folder.

I haven't put together anything yet, but am looking for ideas on how to start.

Thanks.
Go to the top of the page
 
+Quote Post
michaelk
post Feb 19 2009, 11:36 AM
Post #4


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

Group: Support Specialist
Posts: 1,800
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
taylormia
post Feb 19 2009, 07:36 PM
Post #5


Whats this Lie-nix Thing?
*

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



QUOTE (michaelk @ Feb 19 2009, 10:36 AM) *
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.


Thanks much for this.

Here is the script (mmf.sh) with my modifications:

#!/bin/bash
for direct in 'find -name '*.avi' -printf '%h\n' > TEMPFILE.txt; cat TEMPFILE.tx
t | sort -u'
do curdirect='pwd'
cd "$direct"
mv -t "$curdirect" *.avi
cd "$curdirect"
done

Here's the output when I execute the script:


mediaserver:/mediadrive/tv/24/24_SEASON_2 # ./mff.sh
./mff.sh: line 4: cd: find -name *.avi -printf %hn > TEMPFILE.txt; cat TEMPFILE.txt | sort -u: No such file or directory
mv: accessing `pwd': No such file or directory
./mff.sh: line 6: cd: pwd: No such file or directory

It looks like the 'find -name '*.avi' -printf '%h\n' > TEMPFILE.txt; cat TEMPFILE.txt | sort -u' command is not being executed as part of the 'for" statement.

When I type in the above command on the console, I get:

mediaserver:/mediadrive/tv/24/24_SEASON_2 # find -name '*.avi' -printf '%h\n' > TEMPFILE.txt; cat TEMPFILE.txt | sort -u
./24_2X01_8AM-9AM
./24_2X02_9AM-10AM
./24_2X03_10AM-11AM
./24_2X04_11AM-12PM
./24_2X05_12PM-1PM
./24_2X06_1PM-2PM
./24_2X07_2PM-3PM
./24_2X08_3PM-4PM
./24_2X09_4PM-5PM
./24_2X10_5PM-6PM
./24_2X11_6PM-7PM
./24_2X12_7PM-8PM
./24_2X13_8PM-9PM
./24_2X14_9PM-10PM
./24_2X15_10PM-11PM
./24_2X16_11PM-12AM
./24_2X17_12AM-1AM
./24_2X18_1AM-2AM
./24_2X19_2AM-3AM
./24_2X20_3AM-4AM
./24_2X21_4AM-5AM
./24_2X22_5AM-6AM
./24_2X23_6AM-7AM
./24_2X24_7AM-8AM
./24_THE_DESTRUCTION_OF_CTU


The are the directories that have the AVI files that I want to move to the root directory /mediadrive/tv/24/24_SEASON_2. I am running the script from this directory.

Thanks again for your help.


Go to the top of the page
 
+Quote Post

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

 



RSS Lo-Fi Version Time is now: 15th December 2017 - 08:19 PM