Coincidencia negativa usando grep (coincide con líneas que no contienen foo)


He estado tratando de trabajar la sintaxis para este comando:

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

O

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

Necesito ver todos los archivos que han sido modificados excepto los llamados error_log.

He leído sobre ello aquí, pero solo he encontrado un not-patrón regex.

 695
Author: stites, 2010-08-23

3 answers

grep -v es su amigo:

grep --help | grep invert  

- v, inver invert-match seleccionar líneas no coincidentes

También echa un vistazo al relacionado -L (el complemento de -l).

-L, files files-without-match solo imprime nombres de ARCHIVO que no contengan coincidencias

 1295
Author: Motti,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-19 22:28:50

También puede usar awk para estos fines, ya que le permite realizar comprobaciones más complejas de una manera más clara:

Líneas que no contengan foo:

awk '!/foo/'

Líneas que no contienen ni foo ni bar:

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

Líneas que no contienen ni foo ni bar pero que contienen bien foo2 o bar2:

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

Y así sucesivamente.

 80
Author: fedorqui,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-07 09:49:33

En su caso, presumiblemente no desea usar grep, sino agregar una cláusula negativa al comando find, por ejemplo,

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

Si desea incluir comodines en el nombre, tendrá que escaparlos, por ejemplo, para excluir archivos con sufijo .log:

find /home/baumerf/public_html/ -mmin -60 -not -name \*.log
 9
Author: Papa Smurf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-02 13:33:11