How can I find all files that do NOT contain a text string?
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 .
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.
I know that
grep -L *
will do this, but how can I use thefind
command in combination withgrep
to exclude files is what I really want to know
You could use find
and grep
like this:
find . -type f ! -exec grep -q 'PATTERN' {} \; -print
Here -print
is executed only if the previous expression: ! -exec ... {}
evaluates as true
.
So for each file found, grep -q 'PATTERN'
is exec
uted, if the result is false
then the entire expression ! -exec grep -q 'PATTERN'
evaluates as true
and the file name is print
ed.