Remove files without match using bash grep :
------------------------------------------------------------------
I have already discussed in one of my earlier post how we can find the files which do not contain a particular pattern (string).
As I mentioned in that particular post:
"Well, when I first heard of this requirement; I just thought I will use "grep -vl pattern" piped with xargs to a regular find command (i.e. "find . -type f | xargs grep -vl pattern")
(-l : Suppress normal output; instead print the name of each input file from which output would normally have been printed.)
Then I realized that "grep -v" only works in line level, i.e. a file which contain the "pattern" in some line(s) may also contain some line(s) which "do not" contain that "pattern"; so as a whole that file will also be in the list of files which "do not" contain the "pattern" even though it contains the pattern."
From GREP(1) man pages:
-L, --files-without-match
Suppress normal output; instead print the name of each input file from which no out-put would normally have been printed. The scanning will stop on the first match.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.
So to remove or delete the files which do not contain the pattern or string "hello"
$ find . -type f -exec grep -L "hello" {} \; | xargs rm
or
$ find . -type f \! -exec grep -q "hello" {} \; -print | xargs rm
Remove files without match using bash grep:
-----------------------------------------------------------------
I have already discussed in one of my earlier post how we can find the files which do not contain a particular pattern (string).
As I mentioned in that particular post:
"Well, when I first heard of this requirement; I just thought I will use "grep -vl pattern" piped with xargs to a regular find command (i.e. "find . -type f | xargs grep -vl pattern")
(-l : Suppress normal output; instead print the name of each input file from which output would normally have been printed.)
Then I realized that "grep -v" only works in line level, i.e. a file which contain the "pattern" in some line(s) may also contain some line(s) which "do not" contain that "pattern"; so as a whole that file will also be in the list of files which "do not" contain the "pattern" even though it contains the pattern."
From GREP(1) man pages:
-L, --files-without-match
Suppress normal output; instead print the name of each input file from which no out-put would normally have been printed. The scanning will stop on the first match.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.
So to remove or delete the files which do not contain the pattern or string "hello"
$ find . -type f -exec grep -L "hello" {} \; | xargs rm
or
$ find . -type f \! -exec grep -q "hello" {} \; -print | xargs rm
No comments:
Post a Comment