The grep is a popular command used to search and match specified string in the given content or files. The zgrep is very similar to the grep where the compressed files can be easily grepped without any extra command to uncompress them. In this tutorial, we examine how to use zgrep to search in compressed single or multiple files easily without extracting them explicitly. The gz, tgz, gzip, 7zip, zip compressed files can be easily searched with the gzip command.
zgrep Command Syntax
The zgrep command has the following syntax.
zgrep OPTION TERM FILE
- OPTION is single or multiple options to enable different features of the zgrep command. By using OPTION case sensitivity, multiple searches, counts, etc. can be enabled. This option is not required.
- TERM is the term or string which is searched and matched.
- FILE is the file or content given TERM is searched and matched. FILE is a compressed file like gz, bzip, tgz etc.
Search and Match Specified String
The zgrep can be used to easily search the compressed file or files content just providing the search term. In the following example we search the term “ismail” inside the compressed file named auth.log2.gz file.
zgrep ismail auth.log.2.gz
Alternatively multiple compressed files can be easily search with a single zgrep command. Just add the file names we want to search for.
zgrep ismail auth.log.2.gz auth.log.3.gz auth.log.4.gz
Another way to search multiple compressed files is using the glob operator where some part of the file name is specified and other part is completed with the glob operator. The following zgrep command searches the term “ismail” in file names start with “auth.log.”.
zgrep ismail auth.log.*.gz
Count Matched Lines
The zgrep command can be used to count matched lines in compressed files. The -c option is used to count matched lines in compressed files. Only the count of matched lines is printed and the matched lines or term is not printed.
zgrep -c ismail auth.log.2.gz
Ignore Casesensitivity (Caseinsensitive Search)
By default the zgrep command searches and matches in case sensitive mode. This means upper case letters only match for upper case letters in the content or lower case letters match only lower case letters. This can be disable and given term can be searched and matched as case insensitive. The -i option is used to enable case insensitive search with the zgrep command.
zgrep -i ismail auth.log.2.gz
Display Matched Line Numbers
An compressed file contains multiple lines and the matched lines can be located in different locations of the file. The matched line numbers can be displayed or printed by using the -n option like below.
zgrep -n ismail auth.log.2.gz
Invert Search (Display Unmatched Lines)
By default the zgrep command displays matched lines. But if we need to make a invert search or match which means display unmatched lines the -v option can be used.
zgrep -v ismail auth.log.2.gz
Search Multiple Terms
Another powerful feature of the zgrep is the ability to search multiple terms in a single run. Two or more search terms can be specified by using the -e option for every search term. The multiple searhc terms are merged by using the OR logic where matching single term is ok.
zgrep -e "ismail" -e "ahmet" auth.log.2.gz
Display Only Matched Part
By default the zgrep command prints the whole line which contains the specified term. But if we want to only display the matched part the -o option can be used.
zgrep -o "ismail" auth.log.2.gz
Display Only Matched File Names
By default the zgrep command prints the matched file name and the complete line as the result. But if we only need to list the matched lines file names the -l option can be used only for the file names not the matched lines.
zgrep -l "ismail" auth.log.*.gz
Do Not Display File Name and Only Display Matched Lines
By default the zgrep command prints the matched file name and the complete line as the result. But if we only need to print the matched lines and do not display the file names the -h option can be used like below.
zgrep -h "ismail" auth.log.2.gz