Remove all files containing a pattern

bashregex

Suppose I have a directory with files and subdirectories underneath it. Some of the files contain a pattern "^File:" in their contents.

Is there some way I can use grep and a bash command to remove files that contain this pattern? Note I am talking about contents of the file, not the filenames.

Best Answer

A simple piped find should suffice (caveat emptor, I haven't tested this against a large set of data -- backup anything important):

find /path/to/dir -type f -exec egrep -Il '^File:' {} \;|xargs rm -fv