3

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)

4

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
  • Thankyou @Arrow , In the end I used -delete, it was the easiest and shortest method. – uhhhhhhhhhh_ Jun 23 '17 at 0:09
  • 1
    Thanks to you @ElliottHawkins for your comment. Maybe you should also read What should I do when someone answer my question? – Arrow Jun 23 '17 at 0:14
  • By the way, Arrow, I've seen enough people skimming SO answers and using the first solution given that I recommend editing to put the best solution at the top. :) – Wildcard Jun 23 '17 at 0:18
  • Yes, and that is a very bad habit IMhO. But, yes, I'll thinks about your recommendation, maybe in this fast world i should reduce everything to a "one line answer". As Einstein once said: "Everything Should Be Made as Simple as Possible, But Not Simpler". – Arrow Jun 23 '17 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.