LinuxCommandLibrary

zegrep

Search compressed files for a pattern

TLDR

Search for extended regex (supporting ?, +, {}, () and |) in a compressed file (case-sensitive)

$ zegrep "[search_pattern]" [path/to/file]
copy

Search for extended regex (supporting ?, +, {}, () and |) in a compressed file (case-insensitive)
$ zegrep [[-i|--ignore-case]] "[search_pattern]" [path/to/file]
copy

Search for lines that do not match a pattern
$ zegrep [[-v|--invert-match]] "[search_pattern]" [path/to/file]
copy

Print file name and line number for each match
$ zegrep [[-H|--with-filename]] [[-n|--line-number]] "[search_pattern]" [path/to/file]
copy

Search for lines matching a pattern, printing only the matched text
$ zegrep [[-o|--only-matching]] "[search_pattern]" [path/to/file]
copy

Recursively search files in a compressed file for a pattern
$ zegrep [[-r|--recursive]] "[search_pattern]" [path/to/file]
copy

SYNOPSIS

zegrep [grep_options] PATTERN [FILE...]

PARAMETERS

grep_options
    Any valid options supported by the standard grep command (e.g., -i for case-insensitive search, -v for inverted match, -l for listing filenames only). These options are passed directly to grep.

PATTERN
    The regular expression pattern to search for within the content of the files.

FILE...
    One or more files to search. These can be uncompressed or compressed files (e.g., .gz, .bz2, .xz, .Z). If no FILE is specified, zegrep reads from standard input, expecting compressed or uncompressed data.

DESCRIPTION

zegrep (often symlinked from zgrep) is a utility for searching text patterns within compressed files. It acts as a convenient wrapper around the standard grep command, automatically decompressing files on-the-fly before piping their content to grep. This eliminates the need for users to manually decompress files with tools like gunzip or bunzip2 prior to searching.

zegrep supports various compression formats, including those created by gzip, compress, bzip2, and xz, by dynamically detecting the file type and invoking the appropriate decompression utility (e.g., gunzip -c, bunzip2 -c, xz -dc). It passes all provided arguments, including grep's specific options and the search pattern, directly to the underlying grep command, making its usage largely identical to grep itself, but extended to compressed data.

CAVEATS

  • Performance Impact: Searching compressed files involves on-the-fly decompression, which can be slower than searching uncompressed files directly, especially for very large files or slow CPUs.
  • Dependency: zegrep relies on the presence of grep and the corresponding decompression utilities (like gunzip, bunzip2, unxz) to function correctly.
  • Memory Usage: While it streams content, processing very large compressed files might still consume noticeable resources during decompression.

<B>ENVIRONMENT VARIABLES</B>

zegrep's behavior can be influenced by environment variables.
GREP: Specifies the grep program to use instead of the default.
COMPRESS_PROGRAM: Can be used to override the default decompression program (e.g., to force gunzip even if a .bz2 file is encountered, though this might lead to errors if the file is not in gzip format).

<B>EXIT STATUS</B>

The exit status of zegrep is typically the same as the exit status of the underlying grep command it invokes.
0: One or more lines were selected.
1: No lines were selected.
2: An error occurred.

HISTORY

zegrep (and its more common alias zgrep) is part of the gzip package, which was developed to provide tools for handling compressed files in a Unix-like environment. Its creation was motivated by the common need to search log files or other data archives that are frequently stored in compressed formats to save disk space. By automating the decompression step, zegrep significantly streamlined workflows for system administrators and developers who regularly interact with compressed data. It has been a standard utility in most Linux distributions for many years.

SEE ALSO

grep(1), zcat(1), gunzip(1), bzip2(1), xz(1), zless(1), zdiff(1)

Copied to clipboard