Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

In DOS there's the command

xcopy /s *.mp3 ..\TEMP2

How can I do this in Ubuntu?

cp -r *.mp3 ../TEMP2

will copy recursively all mp3 files, unfortunately without the path because of the *.mp3 filter.

cp -r *.* ../TEMP2

will copy the path but can not be limited to mp3 files

I had a look at rsync, but I don't get it.

share|improve this question
Do you want to copy the mp3 files WITHOUT the directory to the temp2 folder or do you want to copy them WITH the directory. – Luis Alvarado Aug 30 '11 at 15:47
@Luis: I guess he wants to preserve the directory structure, or else cp -r would have worked. – MestreLion Aug 30 '11 at 15:52
some nifty commands below. But what is wrong with good old tar, a move and an untar :D – Rinzwind Aug 30 '11 at 17:17
What shell are you using? The second command will only copy the mp3 files in the current directory (and any directories named .mp3 -- it will not recursively copy all mp3 files. – Marcel Stimberg Aug 30 '11 at 18:41
Indeed I want to preserve directory structure. – sleepless Aug 30 '11 at 18:47

The power of tools in Ubuntu is that you can combine them. The next command finds all .mp3 files in the current directory and its subdirectories, and copies them to the ../TEMP2/ folder, preserving paths:

find -iname '*.mp3' -exec install -D {} ../TEMP2/{} \;
  • find -iname '*.mp3' - finds all files ending with .mp3 (case-insensitive) and
    • -exec - executes a command for each match:
      • install -D {} ../TEMP2/{} - copies the matched file to ../TEMP/ preserving the path. ({} is replaced by the path including filename)
    • \; - ends the -exec command

If you want to get the progress, add -ls ("list") to the command before -exec. It can be put after \; too, but in that case the name is shown after being copied. Examples:

find -iname '*.mp3' -ls -exec install -D {} ../TEMP2/{} \;
find -iname '*.mp3' -exec install -D {} ../TEMP2/{} \; -ls
share|improve this answer
I dont understand why would this be any different than rsync -r, except using a much more complex syntax – MestreLion Aug 30 '11 at 16:03
I'm not that familiar with rsync, so this is the best command I could think of. – Lekensteyn Aug 30 '11 at 16:04
I suppose you need a -D option to install. – enzotib Aug 30 '11 at 16:32
@sleepless Please try again, I was missing the -D option which would create any directories if needed. – Lekensteyn Aug 30 '11 at 19:55
@sleepless: I cannot confirm your findings, find shows hidden files (like .hidden.mp3 as well as .hiddenfolder/file.mp3) for me. Perhaps your file browser hide it for you? If you're using the ls command, add the -A option to show dotfiles too. – Lekensteyn Aug 31 '11 at 8:08

There are several options, but none is really simple, I'm afraid…


rsync -r --include="*/" --include="*.mp3" --exclude="*" --prune-empty-dirs . ../TEMP2

This tells to exclude all files (exclude="*"), but to look into all directories (include="*/") and to include all mp3 files (include="*.mp3"). If you do not want to copy directories not containing any mp3 files, in addition the --prune-empty-dirs option is necessary.


zip -R "*.mp3"
unzip -d ../TEMP2 && rm

The first command creates an archive with all mp3 files, the second unzips the content to the target directory and deletes the archive file if it was successful.


find . -iname "*.mp3" -exec install -D {} ../TEMP2/{} ";"

This will find all mp3 files and copy them to the corresponding path in the ../TEMP2 directory, after creating the directory structure first (install -D).

copy all and delete the rest

This only makes sense if you have just a few files that you don't want to copy:

cp -r * ../TEMP2
find ../TEMP2 -type f \! -iname '*.mp3' -delete

This copies everything and then deletes all files that are not mp3 files

share|improve this answer
Yes your find construction works too! I like that one the most. It's a great learning experience. I have come to the right place. But the forum software is very user unfriendly. Rinus – sleepless Aug 30 '11 at 19:24
lekenstein's find -iname '*.mp3' -exec install -D {} ../TEMP2/{} \; with enzotib's "-D" got it going to work. Marcel Stimbergs work is excellent. That means after two days searching solved with your help. You are amazing. Thanks a lot even for the contribs that did not work. Solved! Rinus – sleepless Aug 31 '11 at 6:48

Your Answer


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.