I am trying to find mp3 files in any subdirectories and to copy them, for example, in the current directory. Here is the command I tried:

find . -name *.mp3  -ls | gawk '{printf("cp %s .\n", $11);}'

The find command list all the files correctly escaped, but gawk seem to have problems with it. The find command lists for example the following file

8132326 7392 -rw-r--r--   1 alexander alexander  7566180 Nov 27 20:22 ./For\ The\ Kids\ EP\ [12_\ Vinyl\ EP\ -\ DP003]/01\ A1\ For\ the\ Kids.mp3

while the gawk command sees as $11 the string

./For

instead of

./For\ The\ Kids\ EP\ [12_\ Vinyl\ EP\ -\ DP003]/01\ A1\ For\ the\ Kids.mp3

Why is that? And how to handle this correctly (with or without gawk)?

  • I'm just curious, how did you come up with the original command? Did you find that in a tutorial somewhere? It's a little convoluted, and I'm just trying to find out how to make Bash examples on the Internet better as a whole. – slhck Dec 3 '13 at 7:46
up vote 3 down vote accepted

The correct command is

 find /folder/to/be/searched -type f -name '*.mp3' -exec cp "{}" /target/folder \;

It is simpler, hence fewer chances for errors. Only thing: remember to use absolute paths, not relative ones.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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