1

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.

4

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.

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.