Delete files containing certain string in linux

Posted: April 8, 2011 in Linux
Tags:

For instance, you want to delete all files containing F@@K in folder DIR

$cd DIR
$grep -Rli F@@K * | xargs rm

-R: Read all files under each directory, recursively; this is equivalent to the -d recurse option.
-l: Suppress normal output; instead print the name of each input file from whichoutput would normally have been printed. The scanning will stop on the first match.
-i: Ignore case distinctions in both the PATTERN and the input files

grep will search for all files containing F@@K and feed the input of xargs with a list of file names, xargs then breaks the list returned from grep into sublists small enough to be acceptable by rm and calls rm once for every sublist.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s