views:

450

answers:

3

My current problem is that I have around 10 folders, which contain gzipped files (around on an average 5 each). This makes it 50 files to open and look at.

Is there a simpler method to find out if a gzipped file inside a folder has a particular pattern or not?

zcat ABC/myzippedfile1.txt.gz | grep "pattern match"
zcat ABC/myzippedfile2.txt.gz | grep "pattern match"

Instead of writing a script, can I do the same in a single line, for all the folders and sub folders?

for f in `ls *.gz`; do echo $f; zcat $f | grep <pattern>; done;
A: 

use the find command

find . -name "*.gz" -exec zcat "{}" + |grep "test"

or try using the recursive option (-r) of zcat

ghostdog74
-bash-3.00$ find . -name "*.gz" -exec zcat "{}" + | grep "NOT OK"find: missing argument to `-exec'something seems to be missing after exec?
gagneet
it works for me.
ghostdog74
maybe try changing to find ... +; | grep ... and see
ghostdog74
You must terminate the "-exec" option with ";"
Aaron Digulla
"find -exec cmd {} +" is relatively new, so if you have an older find it may not support "+". It is similar to "find -print0 | xargs -0 cmd".
mark4o
+2  A: 

You don't need zcat here because there is zgrep and zegrep.

If you want to run a command over a directory hierarchy, you use find:

find . -name "*.gz" -exec zgrep ⟨pattern⟩ \{\} \;

And also “ls *.gz” is useless in for and you should just use “*.gz” in the future.

Cirno de Bergerac
I get the lines which contain this pattern, but not the name of the file by this method. Is there some way to get that also listed?
gagneet
`find . -name '*.gz' -print0 | xargs -0 zgrep pattern`?
Hasturkun
Old grep trick: find . -name "*.gz" -exec zgrep ⟨pattern⟩ /dev/null \{\} \; # That will make grep think that there is more than a single file and print the filename.
Aaron Digulla
+4  A: 

zgrep will look in gzipped files, has a -R recursive option, and a -H show me the filename option:

zgrep -R --include=*.gz -H "pattern match" .
Ned Batchelder