Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's wrong with this bash command (find + xarg + grep)?

Tags:

linux

bash

i'm learning bash script by doing, and i had to find files which don't contain a certain string and the command i came up with didn't work. I solved the problem in the meantime by using grep -L (stackoverflow.com/questions/1748129/), but i would still want to know, what's wrong with my original command (so i can learn for the future).

the command is:

find path/ -name *.log -print0 | xargs -0 -i sh -c "if [ '1' == $(cat {} | grep -c 'string that should not occur') ]; then echo {}; fi"

and the error

cat: {}: No such file or directory

I also tried without 'sh -c' before, but it didn't work either.

edit: I also tried

find ./path -name *.log -print0 | xargs -0 -i bash -c "if [ '0' == $(cat $0 | grep -c \"ren0 \[RenderJob\] Render time:\") ]; then echo $0; fi" {}

which didn't work because of https://stackoverflow.com/a/1711985/4032670

like image 691
Adam Avatar asked Sep 19 '25 18:09

Adam


1 Answers

You can use find and xargs like this:

find path/ -name '*.log' -print0 |
xargs -r0 -I {} bash -c 'grep -q "string that should not occur" "{}" || echo "{}"'

Without bash -c you can do this using grep -L:

find path/ -name '*.log' -print0 |
xargs -r0 grep -L "string that should not occur"
like image 87
anubhava Avatar answered Sep 22 '25 08:09

anubhava