LinuxCommandLibrary

zgrep

Search compressed files for a pattern

TLDR

Grep a pattern in a compressed file (case-sensitive)

$ zgrep [pattern] [path/to/compressed/file]
copy

Grep a pattern in a compressed file (case-insensitive)
$ zgrep [[-i|--ignore-case]] [pattern] [path/to/compressed/file]
copy

Output count of lines containing matched pattern in a compressed file
$ zgrep [[-c|--count]] [pattern] [path/to/compressed/file]
copy

Display the lines which don’t have the pattern present (Invert the search function)
$ zgrep [[-v|--invert-match]] [pattern] [path/to/compressed/file]
copy

Grep a compressed file for multiple patterns
$ zgrep [[-e|--regexp]] "[pattern_1]" [[-e|--regexp]] "[pattern_2]" [path/to/compressed/file]
copy

Use extended regular expressions (supporting ?, +, {}, () and |)
$ zgrep [[-E|--extended-regexp]] [regular_expression] [path/to/file]
copy

Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match
$ zgrep --[context|before-context|after-context] 3 [pattern] [path/to/compressed/file]
copy

SYNOPSIS

zgrep [grep_options] pattern [file1 file2 ...]

PARAMETERS

-a, --text
    Treat all files as ASCII text files; this is useful for forcing grep to treat binary files as text.

-b, --byte-offset
    Print the byte offset within the input file before each line of output.

-c, --count
    Suppress normal output; instead print a count of matching lines for each input file.

-d action, --directories=action
    If an input file is a directory, use action to process it. Valid values for action are 'read', 'skip', and 'recurse'.

-e pattern, --regexp=pattern
    Use pattern as the pattern; useful to protect patterns beginning with '-'.
Equivalent to the simple pattern argument, but useful when pattern begins with `-`.

-f file, --file=file
    Obtain patterns from file, one per line.

-h, --no-filename
    Suppress the prefixing of filenames on output.

-i, --ignore-case
    Ignore case distinctions in both the pattern and the input files.

-l, --files-with-matches
    Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.

-L, --files-without-match
    Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.

-n, --line-number
    Prefix each line of output with the line number within its input file.

-o, --only-matching
    Print only the matching (non-empty) parts of a matching line, with each such part on a separate output line.

-q, --quiet, --silent
    Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.

-r, --recursive
    Recursively search subdirectories listed.

-s, --no-messages
    Suppress error messages about nonexistent or unreadable files.

-v, --invert-match
    Select non-matching lines.

-w, --word-regexp
    Select only those lines containing whole words that match the pattern.

-x, --line-regexp
    Select only those matches that exactly match the whole line.

-Z, --null
    Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, 'zgrep -lZ' outputs a zero byte after each file name instead of the usual newline.

--help
    Display help and exit.

--version
    Output version information and exit.

DESCRIPTION

The zgrep command is a powerful utility in Unix-like operating systems that allows you to search for patterns within compressed files. It combines the functionality of grep (for pattern searching) with the ability to decompress files on-the-fly. This eliminates the need to manually decompress files before searching, significantly streamlining the process of finding specific information within large archives. zgrep supports several compression formats including gzip (.gz), compress (.Z), and bzip2 (.bz2). It handles the decompression automatically, piping the uncompressed data to grep for pattern matching. This makes it ideal for analyzing log files, source code archives, or any other compressed data without the overhead of manual decompression. The exit status of zgrep is the same as that of grep, meaning that an exit status of 0 indicates that at least one line was selected, 1 indicates that no lines were selected, and 2 indicates an error occurred. This can be useful for scripting purposes.

CAVEATS

zgrep relies on specific decompression tools being available on the system (e.g., gzip, bzip2). If these tools are missing, zgrep will not be able to process the corresponding compressed files.

SUPPORTED COMPRESSION TYPES

zgrep typically supports gzip (.gz), compress (.Z), and bzip2 (.bz2) file types. It can also sometimes handle zip files (.zip) depending on the implementation and available utilities. Check your system's zgrep manual for a complete list of supported file extensions.

ENVIRONMENT VARIABLES

zgrep is affected by the GREP_OPTIONS environment variable, which can predefine options to be used every time grep is run. Other environment variables, like LC_ALL or LC_*, may affect how zgrep interprets the data and patterns, especially concerning character encoding.

HISTORY

zgrep has evolved as a crucial part of many Unix-like systems. Its genesis stems from the need to directly inspect compressed data archives without the cumbersome step of explicit decompression. The command built upon the functionality of grep adding automatic decompression capabilities for files compressed with gzip, and then expanding to include additional compression formats over time. It's widely adopted for analyzing log files, searching source code archives, and various other tasks where dealing with compressed data is commonplace. Its integration into standard system utilities highlights its usefulness in streamlined data analysis workflows.

SEE ALSO

grep(1), gzip(1), bzip2(1), zcat(1), bzgrep(1)

Copied to clipboard