LinuxCommandLibrary

lzgrep

Search compressed files for a pattern

TLDR

View documentation for the original command

$ tldr xzgrep
copy

SYNOPSIS

lzgrep [grep_options] PATTERN [FILE...]

PARAMETERS

grep_options
    lzgrep is primarily a wrapper around grep. All options provided to lzgrep (except those handled by the wrapper script itself, which are rare) are passed directly to the underlying grep command. This means you can use most standard grep options, such as:

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

-v, --invert-match
    Invert the sense of matching, to select non-matching lines.

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

-n, --line-number
    Print the 1-based line number with each output line.

-r, --recursive
    Read all files under each directory, recursively. This option is typically handled by grep itself.

-A NUM, --after-context=NUM
    Print NUM lines of trailing context after matching lines.

-B NUM, --before-context=NUM
    Print NUM lines of leading context before matching lines.

-C NUM, --context=NUM
    Print NUM lines of context (NUM lines before and NUM lines after) around matching lines.

-E, --extended-regexp
    Interpret PATTERN as an extended regular expression (ERE).

-F, --fixed-strings
    Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

-a, --text
    Process a binary file as if it were text. This is particularly relevant for lzgrep, as compressed files are inherently binary.

PATTERN
    The regular expression or fixed string to search for.

FILE...
    One or more .xz or .lzma compressed files (or plain text files) to search. If no FILE is specified, lzgrep reads from standard input.

DESCRIPTION

lzgrep allows users to search for patterns within files compressed with the LZMA or XZ compression algorithms (typically with .xz or .lzma extensions) without explicitly decompressing them first. It functions as a convenient wrapper around the standard grep utility, piping the decompressed output of a utility like xzcat or lzcat directly into grep. This eliminates the need for manual decompression and re-compression, streamlining the process of searching through compressed logs, archives, and other data. lzgrep supports most of grep's standard options, making it a familiar and powerful tool for regular expression searching on compressed data.

CAVEATS

lzgrep's performance depends on the decompression speed of the underlying utility (xzcat/lzcat) and the processing speed of grep. Searching very large compressed files can be slower than searching uncompressed versions.

It relies on the presence and correct functioning of the xz utilities suite. If a compressed file is corrupted, lzgrep might fail or produce incomplete output, as the decompression utility will encounter an error.

While primarily designed for .xz and .lzma files, some xzgrep implementations (which lzgrep often points to) might attempt to handle other compressed formats like .gz or .bz2 if the appropriate decompression tools are available and detected based on file extensions.

HOW IT WORKS

At its core, lzgrep operates by piping the output of a decompression command to grep. For example, when you run lzgrep 'pattern' file.xz, it internally executes something similar to xzcat file.xz | grep 'pattern'. This on-the-fly decompression and piping allow seamless searching without creating temporary uncompressed files.

SUPPORTED FILE TYPES

lzgrep is specifically designed for files compressed with the LZMA and XZ algorithms, typically identified by .xz or .lzma file extensions. While some xzgrep implementations might have logic to detect and handle other compression types (like .gz or .bz2) if the corresponding decompression utilities are present, its primary and guaranteed function is with XZ-compressed data.

HISTORY

lzgrep emerged as part of the xz utilities package, which provides command-line tools for LZMA and XZ compression. Its development was driven by the need to offer functionality analogous to zgrep (for gzip files) and bzgrep (for bzip2 files), but for the more modern and often more efficient XZ compression format. It typically exists as a shell script wrapper or a symlink to xzgrep, embodying a common Unix philosophy of combining existing, robust tools (like grep and xzcat) to create new, specialized functionalities without reinventing the wheel.

SEE ALSO

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

Copied to clipboard