Search zipped *and* non-zipped files in one hit

2

I have a folder with a mix of zipped and unzipped log files which i want to search.

I can search the non-zipped with

grep -r "my-search-string" /path/to/folder

and search the zipped files with

find /path/to/folder -name "*.gz" -exec zcat "{}" + | grep "my-search-string"

Is there a one-liner i can use to search all the zipped and unzipped files in /path/to/folder ?

thanks, Max

Max Williams

Posted 2014-02-14T12:04:48.567

Reputation: 2 237

Answers

4

zgrep is your friend!

   Zgrep  invokes grep on compressed or gzipped files.  All options speciā€
   fied are passed directly to grep.  If no file is  specified,  then  the
   standard input is decompressed if necessary and fed to grep.  Otherwise
   the given files are uncompressed if necessary and fed to grep.

I just noticed that some zgrep versions don't have a -r option. In that case you can use your find pattern to catch all files and use zgrep instead of grep, such as:

find . -type f -exec zgrep 'my search string' "{}" \;

fede.evol

Posted 2014-02-14T12:04:48.567

Reputation: 1 718

1

Export a few useful variables for 'my-search-string' and path, then search:

SEARCH='my-search-string' ; SEARCHPATH='/path/to/folder' ; grep -r "$SEARCH" $SEARCHPATH && find $SEARCHPATH -name "*.gz" -exec zcat "{}" + | grep "$SEARCH"

Smoothie

Posted 2014-02-14T12:04:48.567

Reputation: 351