Piping find -name to xargs results in filenames with spaces not being passed to the command
Solution 1
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
.
Solution 2
The xargs command uses tabs, spaces, and new lines as delimiters by default. You can tell it to only use newline characters ('\n') with the -d option:
find . -name "*.txt" | xargs -d '\n' rm
Solution 3
Incidentally, if you used something other than find, you can use tr to replace the newlines with null bytes.
Eg. the following one liner deletes the 10 last modified files in a directory, even if they have spaces in their names.
ls -tp | grep -v / | head -n 10 | tr "\n" "\0" | xargs -0 rm
Related videos on Youtube
Ashley
Updated on September 18, 2022Comments
-
Ashley over 1 year
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.
-
TheWanderer over 8 yearsComplete guess here: does
find -name "*\ *.txt" | xargs rm
work for two word files? -
Scott over 8 yearspossible duplicate of How to use find when there are spaces in the directory names? See also how to avoid space in filename? and Problem with spaces in file names.
-
-
kos over 8 yearsCan't upvote it twice tough :) since you mentioned
ARG_MAX
I'll also mention thatfind . -name "*.txt" -exec rm {} \;
would be a "safe shot" -
Joshua over 8 yearsThus sayeth the master: always remember xargs -0.
-
Kev over 5 yearsSuper important point:
-print0
must be the last option (or at least after-name "*.txt"
) otherwise this will hit files no longer limited to*.txt
...