Negative matching using grep (match lines that do not contain foo)

RegexGrep

Regex Problem Overview


I have been trying to work out the syntax for this command:

grep ! error_log | find /home/foo/public_html/ -mmin -60

OR:

grep '[^error_log]' | find /home/baumerf/public_html/ -mmin -60

I need to see all files that have been modified except for those named error_log.

I've read about it here, but only found one not-regex pattern.

Regex Solutions


Solution 1 - Regex

grep -v is your friend:

grep --help | grep invert  

> -v, --invert-match select non-matching lines

Also check out the related -L (the complement of -l).

> -L, --files-without-match only print FILE names containing no match

Solution 2 - Regex

You can also use awk for these purposes, since it allows you to perform more complex checks in a clearer way:

Lines not containing foo:

awk '!/foo/'

Lines containing neither foo nor bar:

awk '!/foo/ && !/bar/'

Lines containing neither foo nor bar which contain either foo2 or bar2:

awk '!/foo/ && !/bar/ && (/foo2/ || /bar2/)'

And so on.

Solution 3 - Regex

In your case, you presumably don't want to use grep, but add instead a negative clause to the find command, e.g.

find /home/baumerf/public_html/ -mmin -60 -not -name error_log

If you want to include wildcards in the name, you'll have to escape them, e.g. to exclude files with suffix .log:

find /home/baumerf/public_html/ -mmin -60 -not -name \*.log

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjerrygarciuhView Question on Stackoverflow
Solution 1 - RegexMottiView Answer on Stackoverflow
Solution 2 - RegexfedorquiView Answer on Stackoverflow
Solution 3 - RegexPapa SmurfView Answer on Stackoverflow