6

I'm having some trouble with this command under bash in Ubuntu:

rm $(find . -name "*.exe")

My aim is to remove, recursively, all files ending in .exe. But some files have white spaces and this breaks the command.

Any suggestions on how to approach this problem?

Jens Erat
  • 18,485
  • 14
  • 68
  • 80
DrBeco
  • 2,125

4 Answers4

17
find . -name "*.exe" -exec rm -f '{}' +

This has find format the command and arguments, and it carefully avoids mangling the names (by passing each one as a separate argument to rm). The '+' means "do as many as you can reasonably in one execution of rm".

3

You can pipe the output from find into xargs, specifying that only newlines should be considered as delimiters between filenames:

find -name '*.exe' | xargs -d \\n rm

The more portable way to do this is to use the null character as the delimiter:

find -name '*.exe' -print0 | xargs -0 /bin/rm

See find's manpage for an example that does this.

Another option is to use the command you used, but to set bash's internal argument delimiter to newlines-only:

IFS=$'\n'; rm $(find . -name "*.exe");

Here the $'...' quoting construct is used to create a newline character. This approach will be less resilient in the case of a long list of filenames than will using xargs.

intuited
  • 3,481
2

Simply pass the -delete option to find:

find . -name "*.exe" -delete

This saves you from quoting and any other filename hassles and is presumably faster than -exec because no new processes need to be spawned.

mainzelM
  • 174
0

Manually escaping the characters may help you.

find . -name "*.exe" | sed -s 's/\ /\\ /' | xargs rm -f