zgrep command in Linux with Examples

The zgrep command is used to search out expressions from a given a file even if it is compressed. All the options that applies to the grep command also applies to the zgrep command.

Syntax:

zgrep [grep options] Expression File name

Options:

  • -c : This option is used to display the number of matching lines for each file.

    Example:

    zgrep -c "linux" GFG.txt.gz

  • -i : This option is used to ignore case sensitivity.

    Example:

    zgrep -i "LINUX" GFG.txt.gz

  • -n : This option is used to display the line number of file if the given expression is present in the line.

    Example:

    zgrep -n "linux" GFG.txt.gz

  • -v : This option is used to display the lines which doesn’t have the expression present in it. Basically invert the search function.

    Example:

    zgrep -v "linux" GFG.txt.gz

  • -e : This option is used to specify the expression but can be used multiple times.

    Example:

    zgrep -e "linux" -e "Linux" GFG.txt.gz

  • -o : This option is used to display only the mathced section of the line from the given expression.

    Example:

    zgrep -o "linux" GFG.txt.gz

  • -l : This option is used to display the names of the files with the expression present in it.
    Example:

    zgrep -l "linux" *

  • -w : By default, zgrep command displays lines even if the expression is found as a sub-string. This option only displays lines only if the whole expression is found.

    Example:

    zgrep -w "linux" GFG.txt.gz

  • -h : This option is used to display the matched lines but doesn’t display the file names.

    Example:

    zgrep -h "linux" GFG.txt.gz



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.