I have all my music in a folder /media/kalenpw/MyBook/Music/ABunchOfOtherFoldersWithFilesInside. I want to copy all of the mp3s to /media/kalenpw/HDD/Music so I used:

cp -R /media/kalenpw/MyBook/Music/*.mp3 /media/kalenpw/HDD/Music

however this only copied the mp3s in the root music folder and did not open any of the artist subdirectories and copy those files.

I was under the impression -R would recursively copy all the files. How can I achieve said goal?



find /media/kalenpw/MyBook/Music/ -name '*.mp3' -exec cp {} /media/kalenpw/HDD/Music \;

The reason for your command not working is that names containing wildcards (*.mp3) are expanded before the command is run, so if you had three files (01.mp3, 02.mp3, 03.mp3) your effective command was:

cp -R /media/kalenpw/MyBook/Music/01.mp3 /media/kalenpw/MyBook/Music/02.mp3 /media/kalenpw/MyBook/Music/03.mp3 /media/kalenpw/HDD/Music

As you can see -R has no effect in this case.

  • I recommend -iname instead of -name in case any files end in .MP3. – Olathe Jul 24 '16 at 4:12
  • Thank you this worked. Or at least is in the process of working. – kalenpw Jul 24 '16 at 4:18

You have specifically mentioned the files(s)/directory(ies) to be copied as using *.mp3 i.e. any file/directory name ending in .mp3.

So any file ending in .mp3 in /media/kalenpw/MyBook/Music/ directory and similarly, any directory ending in .mp3 in /media/kalenpw/MyBook/Music/ will be copied, recursively. If there is no such matched file/directory, nothing will be copied.

Now to copy all .mp3 files from /media/kalenpw/MyBook/Music/ recursivley to directory /media/kalenpw/HDD/Music/:

  • Using bash:

    shopt -s globstar
    cp -at /media/kalenpw/HDD/Music /media/kalenpw/MyBook/Music/**/*.mp3
  • Using find:

    find /media/kalenpw/MyBook/Music -type f -name '*.mp3' -exec cp -at /media/kalenpw/HDD/Music {} +
  • Thanks, techraf answered first so I accepted their answer, however. – kalenpw Jul 24 '16 at 4:18

You can also use cpr:

$ cd /media/kalenpw/HDD/Music
$ cpr -p '/\.mp3$/' '/media/kalenpw/MyBook/Music/'

Note that without -p cpr will preserve complete directory paths on copied filenames.
-r can be used (instead of -p) to preserve the path under '/media.../Music/'.
Also cpr will take care that no file is overwritten, appending a number starting at '0001' to any repeated filename copied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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