The Linux terminal is famous for having hundreds if not thousands of small tools, utilities and commands that give users complete control of their system. On the flip side, this also means that users will have to deal with an exhaustive list of commands they need to know.
In this article, we’re talking about the zgrep command in Linux, what they do, how are they used and everything that you need to know.
Also read: How to exclude in Grep?
What is zgrep?
The zgrep command is used to search specific expressions in files that are in a compressed state. If this sounds rather similar to grep, that’s because all the parameters that apply to grep apply to zgrep as well.
The general syntax for the command is as follows:
zgrep [grep options] Expression File name
The syntax mentioned above is divided into the following three parts:
- Grep options: Flags that enable or control additional search features.
- Expression: The term or string that you want to search.
- File: The file you want to search. Since we’re using zgrep, this is a compressed file like gz, bzip, zip, tgz, or tar among others.
Also read: Vi Linux editor cheatsheet
Here are some of the most commonly used zgrep options.
|i||Used to ignore case sensitivity.|
|n||Shows the line number of the matched expression|
|v||Used to show lines that don’t have the search term/expression|
|e||Used to add multiple search expressions|
|o||Only shows the matched section of the line with the search expression|
|l||Shows the name of the file that contains the search expression|
|w||Only displays lines with the whole search expression|
(zgrep shows results even if the search expression partially matches by default)
|h||Shows the matched lines without the file name(s)|