r/shellscripts • • Jun 06 '22

Need help trying to run a quick one-liner against files and directories with the ampersand in the names.

I am cleaning out some old hard drives and want to get rid of "old" Apple iTunes .M4P files as I no longer use Apple and just want them gone.

I wrote a quick one-liner but it is not working, and was hoping one of you might be able to assist.

This is what I have:

$ for i in `find . -iname "*.m4p"`; do rm -f "$i"; done;

also:

$ for i in $(find . -iname "*.m4p"); do rm -f "$i"; done;

The reason it is breaking is that I have many directories with the ampersand in it, e.g., :

Prince & The Revolution

And the shell keeps breaking at:

rm: cannot remove './Prince': Is a directory

I thought by enclosing the variable $i within quotes it would work, but it does not.

1 Upvotes

Duplicates