Search code examples
bashunixfindpipemv

Escape special characters while using cut along with find and exec


I have to perform the following action on multiple files within a single folder:

Remove "01 " like part from file names like "01 x y z.mp3".

To do that I have constructed following command:

find . -name '*.mp3' -exec sh -c "mv "{}" `echo "{}" | cut -d' ' -f2-`" \;

But on executing the above command I get the following error:

sh: 1: Syntax error: "(" unexpected
sh: 1: Up.mp3: not found

The first error is due to the '(' character present in the filenames and second error is due to the file name being "03 Up&Up.mp3". So in both the cases, the error is because sh cant escape the special characters.

Please suggest the suitable changes to correctly perform the above operation.


Solution

  • It should be:

    find -name '*.mp3' -exec bash -c 'mv  "{}" "$(cut -d" " -f2- <<< "{}")"' \;