LinuxCommandLibrary

xzgrep

Search for patterns within compressed files

TLDR

Search for a pattern within a file

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

Search for an exact string (disables regex)
$ xzgrep [[-F|--fixed-strings]] "[exact_string]" [path/to/file]
copy

Search for a pattern in all files showing line numbers of matches
$ xzgrep [[-n|--line-number]] "[search_pattern]" [path/to/file]
copy

Use extended regex (supports ?, +, {}, () and |), in case-insensitive mode
$ xzgrep [[-E|--extended-regexp]] [[-i|--ignore-case]] "[search_pattern]" [path/to/file]
copy

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

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

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

SYNOPSIS

xzgrep [grep_options] PATTERN [FILE...]
xzfgrep [grep_options] PATTERN [FILE...]
xzegrep [grep_options] PATTERN [FILE...]

PARAMETERS

PATTERN
    The regular expression or fixed string to search for. This argument is required.

FILE...
    One or more .xz compressed files (or uncompressed files) to search. If no files are specified, xzgrep reads from standard input.

grep_options
    Any option valid for the grep command can be passed to xzgrep. These options control search behavior, output format, and more. Examples include -i (ignore case), -v (invert match), -r (recursive search in directories), -l (list matching files), and -n (show line numbers).

DESCRIPTION

The xzgrep command is a utility designed for searching patterns within files compressed with the XZ algorithm. It functions as a wrapper around the standard grep command, allowing users to search for text strings or regular expressions in .xz files without explicitly decompressing them first.

When executed, xzgrep intelligently pipes the decompressed content of the specified XZ file(s) to grep's standard input. This on-the-fly decompression and searching saves disk space by eliminating the need for temporary decompressed files and streamlines the workflow for handling compressed logs or archives.

It behaves almost identically to grep, accepting all of grep's standard options and arguments. This makes it intuitive for users familiar with grep. It also provides aliases: xzfgrep (for fixed string searches, equivalent to fgrep) and xzegrep (for extended regular expressions, equivalent to egrep).

CAVEATS

xzgrep relies on the xz utilities (specifically xzcat or xzless) and grep being available on the system. Performance may be impacted by the overhead of on-the-fly decompression, especially for very large files or on systems with limited CPU resources. While it primarily handles .xz files, it can also process uncompressed files directly.

ALIASES

xzfgrep is an alias that operates like xzgrep -F, interpreting the PATTERN as a list of fixed strings. xzegrep is an alias that operates like xzgrep -E, interpreting the PATTERN as an extended regular expression. These aliases provide direct access to grep's specific search modes.

ENVIRONMENT VARIABLES

Like grep, xzgrep may honor environment variables such as GREP_OPTIONS, GREP_COLOR, and GREP_COLORS. These variables can influence its default behavior or output formatting, allowing for customization without specifying options on the command line every time.

EXIT STATUS

The exit status of xzgrep is inherited directly from the grep command it invokes: 0 if lines were selected, 1 if no lines were selected, and 2 if an error occurred.

HISTORY

xzgrep is part of the xz utilities suite, which was developed to provide command-line tools for the XZ data compression format, a modern successor to LZMA. Following the established pattern of similar utilities like zgrep (for gzip files) and bzgrep (for bzip2 files), xzgrep was created to offer a convenient and consistent interface for searching within XZ compressed archives across Unix-like environments. Its design emphasizes seamless integration with the existing functionality of the grep command.

SEE ALSO

grep(1), xz(1), xzcat(1), zgrep(1), bzgrep(1)

Copied to clipboard