LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Recursively move files of one type and create destination sub-folders (https://www.linuxquestions.org/questions/linux-newbie-8/recursively-move-files-of-one-type-and-create-destination-sub-folders-4175542848/)

bicyclerepairman 05-17-2015 11:52 AM

Recursively move files of one type and create destination sub-folders
 
I'm just getting into Bash scripting, and would appreciate some help with this question. My music collection is split into a smaller, "active" set, kept on my laptop, and a much larger collection on an external hard drive. I've just converted some of the larger filetypes on my "active" set to *.mp3, and now want to move all the original files (*.flac) to the external hard drive. I need some help putting together a command or script that will recursively search my active music set for *.flac and then move them, but keeping the source directory structure. Some or all of these subdirectories may not exist on the destination.

eg. On the active music set, I may have:

/Music/artist1/album1/(a mix of *.mp3 and *.flac files)
/Music/artist2/album1/(a mix of *.mp3 and *.flac files)

and on the hard drive

/Music 2/artist1/album2/(the contents of the album)

So when copying, it'll need to create "/album1/" in "artist1" on the destination, and also "/artist2/album1/"

Thanks in advance!

Keith Hedger 05-18-2015 09:41 AM

Grab a copy of the advanced bash scripting guide, don't be put off by the name it covers simple stuff as well, your distro may have it in its repos ( abs ) if not google it.

You are probaly going to need to use the 'dirname' and 'basename' commands on the source file to get the folder hierachy ( dirname ) then strip any leading path items you don't want, use 'mkdir -p' to create folder hierarchy on the destination and finally copy the source file to the destination folder.

so for instance
Code:

keithhedger@LFSCerebro:/media/SkyNet/Videos/Sherlock 3-> srcfile='/media/SkyNet/Videos/Sherlock 3/dvd_out.mpg'
keithhedger@LFSCerebro:/media/SkyNet/Videos/Sherlock 3-> dirpath=$(dirname "$srcfile")
keithhedger@LFSCerebro:/media/SkyNet/Videos/Sherlock 3-> echo $dirpath
/media/SkyNet/Videos/Sherlock 3
keithhedger@LFSCerebro:/media/SkyNet/Videos/Sherlock 3-> stripped="${dirpath/\/media\/SkyNet/}"
keithhedger@LFSCerebro:/media/SkyNet/Videos/Sherlock 3-> echo $stripped
/Videos/Sherlock 3
keithhedger@LFSCerebro:/media/SkyNet/Videos/Sherlock 3-> mkdir -p "/tmp/videos/$stripped"
keithhedger@LFSCerebro:/media/SkyNet/Videos/Sherlock 3-> cp "$srcfile" "/tmp/videos/$stripped"
keithhedger@LFSCerebro:/media/SkyNet/Videos/Sherlock 3-> ls "/tmp/videos/$stripped"
dvd_out.mpg



All times are GMT -5. The time now is 04:38 PM.