Recursively Copy Files Between Directories – Command Line Guide

command linecopycpmusictransfer

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?

Best Answer

Use:

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.