Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What concise command can I use to find all files that do NOT contain a text string?

I tried this (using -v to invert grep's parameters) with no luck:

find . -exec grep -v -l shared.php {} \;

Someone said this would work:

find . ! -exec grep -l shared.php {} \;

But it does not seem to work for me.

This page has this example:

find ./logs -size +1c  > t._tmp
while read filename
do
     grep -q "Process Complete" $filename
     if [ $? -ne 0 ] ; then
             echo $filename
     fi
done < t._tmp
rm -f t_tmp

But that's cumbersome and not at all concise.

ps: I know that grep -L * will do this, but how can I use the find command in combination with grep to excluded files is what i really want to know.

pss: Also I'm not sure how to have grep include subdirectories with the grep -L * syntax, but I still want to know how to use it with find :)

share|improve this question
    
The first command you wrote worked fine on my computer. What version of find are you using? And since we're at it, maybe specify which unix you're running.. –  rahmu Dec 14 '11 at 15:47

3 Answers 3

up vote 3 down vote accepted
find . -type f | xargs grep -H -c 'shared.php' | grep 0$ | cut -d':' -f1    

OR

find . -type f -exec grep -H -c 'shared.php' {} \; | grep 0$ | cut -d':' -f1

Here we are calculating number of matching lines(using -c) in a file if the count is 0 then its the required file, so we cut the first column i.e. filename from the output.

share|improve this answer
2  
This is awfully complicated. See Kevin's answer. –  Gilles Dec 16 '11 at 7:49
    
@Gilles yes I agree with you. I have +1 his answer. –  Sachin Divekar Dec 16 '11 at 8:40

I think you are looking for a command like

find . -type f -execdir grep -q -v shared.php {} \; -print

The option -q makes grep quite and after using -execdir you need -print to print found files.

share|improve this answer

Your find should work if you change -v -l (files that have any line not matching) to -L (files with no lines matching), but you could also use grep's recursive (-r) option:

grep -rL shared.php .
share|improve this answer
    
which find should work? –  cwd Dec 14 '11 at 15:13
    
@Kevin - sorry, find . -exec grep -v -l shared.php {} \; (and all the other variants I tried) is not working for me. I'm on OS X but I don't think that should matter. I gave you a +1 because I had forgotten about the -r option. –  cwd Dec 14 '11 at 15:49
    
@SachinDivekar You're right, I've updated the answer. –  Kevin Dec 14 '11 at 15:54
    
@SachinDivekar - looks to be fixed. maybe not -1 to Kevin anymore? –  cwd Dec 14 '11 at 16:52
    
cwd, I have +1 @Kevin answer. –  Sachin Divekar Dec 16 '11 at 8:41

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.