[NTLUG:Discuss] Find Files NOT containting 'string'

Robert Citek rwcitek at alum.calberkeley.org
Fri Nov 7 00:02:51 CST 2003


At 07:01 PM 11/6/2003 -0600, Stuart Johnston wrote:
>I need to search for files that contain one string but not another.  Can 
>anyone suggest a linux command to do this?

At 10:32 PM 11/6/2003 -0600, Rusty Haddock wrote:
>grep -l 'iwantthisstring' {list of files} | xargs grep -L 'idontwantthis'

Sample input files:

echo -e "abc\ndef" > x-abc\ def
echo -e "def\nghi" > x-def\ ghi
echo -e "ghi\njkl" > x-ghi\ jkl
echo -e "jkl\nabc" > x-jkl\ abc

Question: List all files that contain "abc" but not "def".
Answer: "x-jkl abc"

$ grep "abc" x-*
x-abc def:abc
x-jkl abc:abc

$ grep -l "abc" x-*
x-abc def
x-jkl abc

$ grep -L "def" x-*
x-ghi jkl
x-jkl abc

$ grep -l "abc" x-* | xargs grep -L "def"
grep: x-abc: No such file or directory
grep: def: No such file or directory
grep: x-jkl: No such file or directory
grep: abc: No such file or directory

$ grep -Z -l "abc" x-* | xargs -0 grep -L "def"
x-jkl abc

>    >As I've said a number of times before, 'xargs(1)' is your friend!  :-)
>
>Still true though.

Yup.

Regards,
- Robert




More information about the Discuss mailing list