Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to remove all the files in a directory hierarchy which a certain string inside the file (not the file name, it is the file content).

I can list out all the file name which has a string in the file using 'grep -r -l mystringlooking for'.

But how can I remove all the files returned by the grep ? I am trying this on ubuntu.

Thank you.

share|improve this question
up vote 4 down vote accepted

grep -r -I -l 'yourstring' . | xargs rm

The -l flag outputs the path of the files matched. | pipes it to the next command, which is xargs. xargs executes its argument (rm) for every line it recieves via STDIN (provided by he pipe.)

be careful doing this. rm won't ask 'are you sure', and there's no undelete.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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