Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible only using the command line to use something like the cp or rsync command to crawl a hard-drive seeking only image/video files? And maybe in the process of copying them over to another location preserving the folder names / paths in the new location?

I have a couple old harddrives that i want to make backups of pictures and videos that I have on them, but they are scattered at best over thousands of folders and I'd prefer the computer do most of the grunt work for me, if at all possible, but I am still learning about the command line and its impressive abilities.

share|improve this question

2 Answers 2

You can use find to look for files you are interested in, ie:

find /mnt/hd1 -name \*.jpg

will look for jpg files (well, for files with .jpg extension) in /mnt/hd1 directory. If you want to simply copy those files somewhere, you can:

find /mnt/hd1 -name \*.jpg -exec cp {} /mnt/dest

This will run cp command for every file it can find to /mnt/dest directory.

Hope it helps :)

share|improve this answer
1  
At least on my system I need a closing \; after the target directory. On GNU systems (which the OP doesn't have, so this is for reference only), you might save some overhead by doing … -exec cp -t /mnt/dest {} +. This copies multiple files in one go. –  MvG Aug 14 at 0:12

Another possibility, if you don't want to automate it into one script, is to use your find command to create a bash script. Then run the bash script. This way you can tell the origin of the file also.

Also, I would change the above slightly just in case you have duplicate file names if you might suspect this in your environment. You'd need some error checking in your script.

Also do you want to preserve the directory path?

i.e. if a file is here

/source/a/b/c/d/e.jpg

Do you want to copy it to

/dest/a/b/c/d/e.jpg ?

or

/dest/e.jpg ?

So an example could be

find /source -type f -name "*.jpg" -print | awk '{"cp -p "$1" /backup/}' > copy.sh

This would copy just to one directory /backup

To preserve, you could do

(cd /source; find . -type f -name "*.jpg" -print | awk '{"cp -p "$1" /backup/"$1}') > copy.sh

But then you'd want to create all the directories first

(cd /source; find . -type d -print | awk '{print "mkdir -p /backup/"$1}' > mkdir.sh

The power of doing this is you can also eyeball the scripts and make sure everything looks good before actually running the commands. I have burned quite a few times running the command with ...erm...unexpected results :)

Plus having the script is also giving you a record of what you will be doing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.