Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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' "{}" \;

share|improve this answer
    
That's perfect - thanks! –  Max Williams Feb 14 at 13:50

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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.