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.

I want to delete all files that contain the string foo. How can I do this using bash in Linux?

share|improve this question
    
that string is its file name ? –  AgentCool Apr 20 '13 at 15:53
2  
Please clarify: do you want to delete files whose name contains foo (e.g. myfoo.jpg), or files that contain the byte sequence foo (which may include binary files which just so happen to contain that sequence of bytes)? –  hammar Apr 20 '13 at 21:32

3 Answers 3

up vote 15 down vote accepted

Here is a safe way:

grep -lrIZ foo . | xargs -0 rm -f --
  • -l prints file names of files matching the search pattern.
  • -r performs a recursive search for the pattern foo in the given directory ..  If this doesn't work, try -R.
  • -I (capital i) causes binary files like PDFs to be skipped.
  • -Z ensures that file names are zero- (i.e., nul-)terminated so that a name containing white space does not get interpreted in the wrong way (i.e., as multiple names instead of one).
  • xargs -0 feeds the file names from grep to rm -f, separating words by zero (nul) bytes (remember the -Z option from grep).
  • -- is often forgotten but it is very important to mark the end of options and allow for removal of files whose names begin with -.

If you would like to see which files are about to be deleted, simply remove the | xargs -0 rm -f -- part, and leave off the Z option to grep.

Another user suggested something like the following, which you should not run because it is unsafe:

files=`grep foo * | cut -d: -f1`
rm -f $files         # unsafe, do not run it!

If I have files ImportantStuff that I do not want to delete and obsolete ImportantStuff containing foo, then I lose ImportantStuff (and not obsolete ImportantStuff!) when I run this command, because $files gets broken apart at spaces when it is interpreted. It is dangerous to put a list of filenames into a scalar shell variable in this way.

share|improve this answer
$ find -type f -exec grep -q "foo" {} \; -exec echo rm -- {} \;

This recursively searches for files containing foo. The second -exec is only run if the first exec exits successfully, i.e. if grep matches. Dryrun and remove the echo if the output seems correct.

Or alternatively

$ find -type f -exec grep -q "foo" {} \; -print

and

$ find -type f -exec grep -q "foo" {} \; -delete

as suggested by Lekensteyn.

share|improve this answer
4  
... which can be shortened to: find -type f -exec grep -q 'foo' {} \; -delete (the quotes around {} are redundant, find is also able to delete files.). Note that grep accepts a regular expression. If you want to search for foo.bar, either escape this or pass the -F option to treat the pattern as literal. –  Lekensteyn Apr 20 '13 at 17:06
    
any reason to use -exec echo rm "{}" \; and not -delete? –  vartec Apr 20 '13 at 20:47
1  
@vartec No, I'd use -delete. It's just easier to demonstrate with the second -exec and echo in place. –  Adrian Frühwirth Apr 20 '13 at 20:53
1  
It is easier to type -print (or -ls for more verbosity) than -exec echo rm "{}" \'. No need for external utilities. –  Lekensteyn Apr 20 '13 at 21:07
1  
if you use rm {}, use rm -- {}, so that any special characters in the file name itself don't get interpolated. For example, touch '-rf /' in any directory where you have write permissions... –  atk Apr 21 '13 at 0:15

I'd do what Adrian says,

But I'd add word boundries arond the foo so as to not accidentaly delete files containing "food" (unless it is deleting everything containing food is what you want.)

 $ find -type f -exec grep -q "\<foo\>" {} \; -delete

acutally, I'd redirect the output before deleteing so I could review before deleting:

 $ find -type f -exec grep -q "\<foo\>" > /tmp/list_of_files_to_delete
share|improve this answer
    
As soon as you are able to post comments, you should post things like that as comments. –  FSMaxB Apr 21 '13 at 8:05
    
yes I tried first but then realised that I'm not allowed to post comments yet . –  Petter H Apr 21 '13 at 14:29

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.