LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-17-2015, 11:52 AM   #1
bicyclerepairman
LQ Newbie
 
Registered: May 2015
Distribution: lubuntu 14.10
Posts: 3

Rep: Reputation: Disabled
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!
 
Old 05-18-2015, 09:41 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursively move files michaelsanford Linux - General 5 02-17-2016 01:24 AM
Rsync create folders recursively? Toasterman Linux - Software 6 08-15-2014 05:41 AM
Recursively move files one directory up FLX Linux - Newbie 8 10-06-2009 01:41 PM
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 04:35 PM
How to unzip files recursively inside folders? jessdog9001 Linux - Software 2 03-22-2006 12:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration