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
)
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
)
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
-exec
or-delete
primaries offind
. Also see Why is looping over find's output bad practice? – Wildcard Jun 22 '17 at 02:32