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.

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.

share|improve this question
    
This question might be better for unix.stackexchange.com –  Hemm Oct 27 '11 at 23:37
    
If you could narrow your search by a filename pattern, then you'd get a lot better efficiency from any scripts you get. –  Chris Ting Oct 27 '11 at 23:43

3 Answers 3

up vote 2 down vote accepted

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
share|improve this answer
    
@Hemm -- Actually I tested mine against files with spaces and it removes them just fine. You probably want to add -I to your grep or he'll be deleting binary files too. –  Garrett Oct 28 '11 at 0:24
    
@Hemm's example can also potentially run into argument limitations of grep if too many files are matched by the wildcard. –  Garrett Oct 28 '11 at 0:33
    
I tested mine on 100k+ files containing the string with no problems. And I already have -l in it. –  Hemm Oct 28 '11 at 1:54
1  
I as in India. man grep ;) –  Garrett Oct 28 '11 at 2:31
1  
-I- blame sans serif. –  Hemm Oct 28 '11 at 2:59
grep -rlI "meow" * | xargs -I{} rm -v {} 

Remove files containing meow!

This also handles cases where there are filenames with spaces and other characters. In the other answers, "xargs rm -f" alone will fail.

share|improve this answer
1  
That doesn't descend into subdirectories. –  Keith Thompson Oct 28 '11 at 0:07
    
Whoops. -r added. –  Hemm Oct 28 '11 at 0:14

Here's a one-liner.

grep -r "^File:" /path/to/directory | cut -d: -f1 | sort | uniq | xargs rm -f
share|improve this answer
    
Much more verbose than it needs to be. Use grep's -l option to list just the file names. The sort and uniq steps are unnecessary, since the files are just going to be deleted. grep -r -l "^File:" /path/to/directory | xargs rm -f. Or, if some of the file names might contain funny characters (particularly spaces): grep -r -l "^File:" /path/to/directory | tr '\n' '\0' | xargs -0 rm -f. (I have not tested these; try xargs echo rm -f first to make sure it's generating the right list of files.) –  Keith Thompson Oct 28 '11 at 0:06
    
@ChrisTing: Could you provide some explanation about what your code does? –  Tom Wijsman Nov 1 '11 at 1:34
    
@KeitThompson: Could you please consider to copy that in an own answer? :) –  Tom Wijsman Nov 1 '11 at 1:35

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.