Piping Find -Name to Xargs – Fix Filenames with Spaces Issue

command linefindrmxargs

Normally to remove files with spaces in their filename you would have to run:

$ rm "file name"

but if I want to remove multiple files, e.g.:

$ find . -name "*.txt" | xargs rm

This will not delete files with spaces in them.

Best Answer

You can tell find and xargs to both use null terminators

find . -name "*.txt" -print0 | xargs -0 rm

or (simpler) use the built-in -delete action of find

find . -name "*.txt" -delete

or (thanks @kos)

find . -name "*.txt" -exec rm {} +

either of which should respect the system's ARG_MAX limit without the need for xargs.