LinuxCommandLibrary

lzegrep

Search compressed files for a pattern

TLDR

View documentation for the original command

$ tldr xzgrep
copy

SYNOPSIS

lzegrep [grep_options] PATTERN [FILE...]

PARAMETERS

PATTERN
    The regular expression or fixed string to search for within the compressed files.

FILE...
    One or more paths to lzip-compressed files (.lz) to be searched. If no files are specified, lzegrep reads from standard input, which must be lzip-compressed data.

[grep_options]
    Any valid option supported by the grep command. lzegrep passes these options directly to the underlying grep process. Common options include:
-i, --ignore-case: Ignore case distinctions.
-v, --invert-match: Select non-matching lines.
-n, --line-number: Prefix output lines with line numbers.
-l, --files-with-matches: Print only names of files containing matches.
-c, --count: Print only a count of matching lines.
-E, --extended-regexp: Interpret PATTERN as an extended regular expression (like egrep).
-F, --fixed-strings: Interpret PATTERN as a list of fixed strings (like fgrep).

DESCRIPTION

lzegrep is a utility program provided as part of the lzip compression suite. It enables users to search for regular expression patterns within files compressed using the lzip format (typically identified by the .lz extension) without needing to decompress them first. Functionally, it acts as a wrapper around the lzip decompressor and the standard grep command. When invoked, lzegrep pipes the decompressed output of the specified .lz files to grep, which then performs the pattern matching. This significantly streamlines the workflow for handling compressed logs, archives, or data, saving disk space and time by avoiding explicit decompression. It supports most standard grep options, passing them directly to the underlying grep command, allowing for flexible and powerful searches.

CAVEATS

Dependency: lzegrep requires both lzip and grep to be installed and accessible in the system's PATH.
Performance: While convenient, searching compressed files involves on-the-fly decompression, which can be slower than searching uncompressed files directly, especially for very large files or on systems with limited CPU resources.
No Native Directory Recursion: Unlike some grep implementations with -r, lzegrep itself does not natively traverse directories to find .lz files. Users typically combine it with find to search across multiple directories (e.g., find . -name "*.lz" -print0 | xargs -0 lzegrep "pattern").
Archive Content Limitation: It searches individual .lz files, not contents of archives like tar.lz directly. To search such archives, they would need to be first extracted or piped via tar -xO or similar commands.

UNDERLYING MECHANISM

lzegrep is typically a shell script that acts as a simple wrapper. It executes the command lzip -cd (decompress to standard output) on the specified .lz files and pipes the resulting decompressed stream to the standard input of the grep command. All arguments provided to lzegrep after the command name are passed directly to grep once the decompression pipeline is established. This simple yet effective approach allows it to leverage the full power and options of the standard grep utility on compressed data.

USAGE EXAMPLES

Search for a pattern in a single lzip file:
lzegrep "error message" log.lz
Search case-insensitively across multiple lzip files:
lzegrep -i "keyword" report_*.lz
Find files containing a specific phrase without showing the lines:
lzegrep -l "critical failure" data/*.lz
Search all lzip files recursively in current directory for a pattern:
find . -name "*.lz" -print0 | xargs -0 lzegrep "target string"

HISTORY

lzegrep is part of the lzip utilities, a compression program developed by Antonio Diaz Diaz. lzip utilizes the LZMA algorithm, known for its high compression ratios and data integrity features. Conceived as a modern alternative to existing compression formats, lzip introduced a suite of complementary tools, including lzegrep, to facilitate common operations on .lz files. Its inclusion mirrors the functionality provided by zgrep for gzip files, acknowledging the common need to search through compressed data without manual decompression steps. Its development is inherently linked to the evolution of the lzip format itself, providing essential usability for its users.

SEE ALSO

grep(1), lzip(1), lzcat(1), zgrep(1), bzgrep(1), xzgrep(1), find(1)

Copied to clipboard