Shell – Using find with -exec gzip and grep

aixfindgrepshellunix

I have a bunch of zipped up log files and I want to search them all for a string. I tried this but it's not working:

find ./ -name "*.log.zip" -exec gzip -dc {} | grep ERROR \;

It's giving me:

find: incomplete statement
grep: can't open ;

What I want is, for each .log.zip file, unzip it and grep the output for "ERROR". Doing this on AIX, for what it's worth.

Best Answer

There is an error in your syntax. Find is looking for \; or \+, but reads |. Grep is trying to open a file called ";". The difference between terminating -exec with a semicolon or a plus is running the command once for all files (+) and running the command once for every file (;).

Try this:

find ./ -name "*.log.zip" -exec zcat {} \+ | grep ERROR
# or
find ./ -name "*.log.zip" -exec sh -c 'zcat {} | grep ERROR' \;
Related Question