Replacement for DOS xcopy command

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

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

rsync

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

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

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

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