Unix 'grep' for a string within all gzip files in all subdirectories
How do I grep for a string recursively through all .gz
files in all directories and subdirectories?
@Steve Weet is almost there. The use of /dev/null as an additional argument is a nice way to force the filename to be shown (I'll remember that, thanks Steve) but it still runs the exec for every file found -- a huge overhead.
You want to run zgrep as few times as you can, getting the most out of each execution:
find . -iname '*.gz' -print0 | xargs -0 zgrep PATTERN
xargs
will supply as many args (filenames) as possible to zgrep, and repeatedly execute it until it has used all the files supplied by the find
command. Using the -print0
and -0
options allows it to work if there are spaces in any of the file or directory names.
On Mac OS X, you can achieve the same effect without xargs:
find . -iname '*.gz' -exec zgrep PATTERN {} +
$ zgrep --help
Usage: /bin/zgrep [OPTION]... [-e] PATTERN [FILE]...
Look for instances of PATTERN in the input FILEs, using their
uncompressed contents if they are compressed.
So something like
find . -iname "*.gz" -exec zgrep PATTERN {} \
@aioobe is almost there. The command will do the job but won't tell you the file name
The following should tell you the filename as well:
find . -iname "*.gz" -exec zgrep PATTERN {} /dev/null \;
The addition of /dev/null
will ensure that zgrep sees two filenames so it will show you the name of the file if it finds the string
EDIT
Further research reveals that for my machine (OS/X) the -exec
argument to find will add as many filenames as possible (similar to the way xargs
behaves).