3

I want to remove all texts that include string foo.

I can list all files by ack-grep foo, but I couldn't find a way to remove all files like -exec rm {} option like find.

How can I delete all files that contains particular string?

5

With GNU xargs:

ack -l --print0 foo | xargs -r0 rm --

ack's --print0 and xargs' -0 cause ack and xargs to write and read using NUL as the delimiter, which guarantees proper filename handling. Without it, xargs will accept a far more wide range of characters as a delimiter.

2

You can use option -l with grep and ack which lists only filename:

grep -l --null foo ./* | xargs -r0 rm

or:

ack -l --print0 foo ./* | xargs -r0 rm --
2
  • This is dangerous, any filenames containing whitespace will be handled improperly, and could cause unexpected files to be removed.
    – Chris Down
    Apr 1 '15 at 9:44
  • There could be problems with grep and rm if a directory is included in the search directory
    – A.B.
    Apr 1 '15 at 9:46

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.