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;
Coming in a bit late on this, had a similar problem and was able to resolve using;
As detailed here;
http://manpages.ubuntu.com/manpages/quantal/man1/gzip.1.html
However, this does not show the original file that the result matched from, instead showing "(standard input)" as it's coming in from a pipe. zcat does not seem to support outputting a name either.
In terms of performance, this is what we got;
As you can see, using the
find|zcat
method is significantly slower than usingzcat -r
when dealing with even a small volume of files. I was also unable to make zcat output the file name (using-v
will apparently output the filename, but not on every single line). It would appear that there isn't currently a tool that will provide both speed and name consistency with grep (i.e. the-H
option).If you need to identify the name of the file that the result belongs to, then you'll need to either write your own tool (could be done in 50 lines of Python code) or use the slower method. If you do not need to identify the name, then use
zcat -r
.Hope this helps
use the find command
or try using the recursive option (-r) of zcat
zgrep will look in gzipped files, has a -R recursive option, and a -H show me the filename option:
zgrep "string" ./*/*
You can use above command to search for
string
in .gz files ofdir
directory wheredir
has following sub-directories structure:find . -name "*.gz"|xargs zcat | grep "pattern"
should do.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:
And also “
ls *.gz
” is useless in for and you should just use “*.gz” in the future.