I have many directories named "test" and I would like to remove them.
I know how to find them and print them using:
find . -name test -print
Now, how to do I remove the directories?
Please note that I have files in the directory as well and they must be removed as well.
Best Answer
xargs
does all the magic:xargs
executes the command passed as parameters, with the arguments passed to stdin.This is using
rm -r
to delete the directory and all its children.The
--
denotes the end of the arguments, to avoid a path starting with-
from being treated as an argument.-print0
tellsfind
to print\0
characters instead of newlines; and-0
tellsxargs
to treat only\0
as argument separator.This is calling
rm
with many directories at once, avoiding the overhead of callingrm
separately for each directory.As an alternative,
find
can also run a command for each selected file:And this one, with better performance, since it will call
rm
with multiple directories at once:(Note the
+
at the end; this one is equivalent to thexargs
solution.)