Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I copy all files with a certain extension from one location to another?

For example, I want to copy all my music files onto my mp3 player, but my music folder on my hard drive contains cover art as well as music. I am aware that I could just copy everything across and then delete all the images, but I am hoping for a better solution than that. I have seen a solution using rsync, like

rsync -av --exclude "*/" --include "*.mp3" --include "*.ogg" --include "*.wma" --include "*.flac" ~/Music/. /media/E100/Music

but this complains lots about not being able to change the group of the files, and for some reason didn't copy all the files over (it left some directories blank). Also, I would prefer that the creation date on the files is the current time, since this would probably work better with the mp3 player's internal database.

Other things that I would really like: It would be nice if it doesn't overwrite the files, but only copies the latest files over, however, if I have to erase all the files and then copy the whole directory across, I can handle that as well.

share|improve this question
up vote 4 down vote accepted

It sounds like you've already got the solution you want with rsync.

If it's complaining about permissions and unable to copy certain things, try running it with sudo

sudo rsync -auv etc etc

EDIT: The -a flag is what is causing it to preserve times. The -a flag is an alias for -rlptgoD and the -t flag is what causes the times to be preserved. You may prefer to use rsync like this:

sudo rsync -rlpgoDuv etc etc

also: for particularly long copies, you may want to use the --progress flag rather than -v.

Seems this works:

sudo rsync -rlpuv --progress --include "*/" --include "*.mp3" --include "*.ogg" --include "*.wma" --include "*.flac" --exclude "*" ~/Music/ /media/E100/Music
share|improve this answer
The problem with rsync -av is that it won't set the file times to the current time - any way to fix that? – a_m0d Sep 4 '09 at 14:47
when using the -a flag on rsync you specify that you want to 'preserve' or 'archive' the files in their state. Take a careful look at the rsync man page on how to override this. – jweede Sep 4 '09 at 14:53
Turns out I had some of the include / exclude stuff the wrong way round, and changing the other options the way you showed me also helped. – a_m0d Sep 4 '09 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .