Piping find results into another command
10,152
One way is to expand the list of files and give it to rm as arguments:
$ rm $(find . -iregex '.*.new.*' -regex '.*.pdf*')
**That will fail with file names that have spaces or new lines.
You may use xargs to build the rm command, like this:
$ find . … … | xargs rm
** Will also fail on newlines or spaces
Or better, ask find
to execute the command rm:
$ find . … … --exec rm {} \;
But the best solution is to use the delete option directly in find:
$ find . -iregex '.*.new.*' -regex '.*.pdf*' -delete
Related videos on Youtube
Author by
uhhhhhhhhhh_
Updated on September 18, 2022Comments
-
uhhhhhhhhhh_ over 1 year
I'm trying to scan a file system for files matching specific keywords, and then remove them. I have this so far:
find . -iregex '.*.new.*' -regex '.*.pdf*'
How do I then pipe the result for this command into a remove command (such as
rm
)-
Stephen Rauch almost 7 years
-
Wildcard almost 7 yearsYou don't need a pipe, you need the
-exec
or-delete
primaries offind
. Also see Why is looping over find's output bad practice? -
uhhhhhhhhhh_ almost 7 years@Wildcard , Thankyou, I realised that find had a -delete primary when I was reading through the man find command.
-