Having a thousand of data files (same extension) in a directory, how to delete those which contain a specific parameter statement?

For example, if I wented to delete all files containing the line

Key = 0

could I do something like

grep -i "Key = 0" * | rm *
  • 1
    You mean the file would contain the literal string Key = 0? – jesse_b Aug 11 at 19:22
  • 4
    The output of grep would include the filename and the line containing the string so instead of passing it to rm, it would just run the rm command on * which is everything in the directory and it would delete the files and give an error for directories because the -r switch isn't used. Do you want to delete the files that have that string in their contents? If so, provide an example of what it might look like. Do you also want to delete files that have Key = 01 or MyKey = 05? – Nasir Riley Aug 11 at 19:28

You can use the following command in the shell. This should do the job if pattern to be searched is Key = 0

grep -lrIZ "Key = 0" . | xargs -0 rm -f --

Generic command:

grep -lrIZ "<Pattern to be searched in the file>" . | xargs -0 rm -f --
| improve this answer | |

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.