Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.