LinuxCommandLibrary

zfgrep

Search compressed files for a regular expression

TLDR

Search for an exact string in a file

$ zfgrep [search_string] [path/to/file]
copy

Count the number of lines that match the given string in a file
$ zfgrep [[-c|--count]] [search_string] [path/to/file]
copy

Show the line number in the file along with the matching lines
$ zfgrep [[-n|--line-number]] [search_string] [path/to/file]
copy

Display all lines except those that contain the search string
$ zfgrep [[-v|--invert-match]] [search_string] [path/to/file]
copy

List only filenames whose content matches the search string at least once
$ zfgrep [[-l|--files-with-matches]] [search_string] [path/to/file1 path/to/file2 ...]
copy

SYNOPSIS

zfgrep [grep_options] pattern [file ...]

PARAMETERS

grep_options
    zfgrep passes all command-line options directly to the underlying grep command (or zgrep, which in turn passes them to grep). Therefore, it supports the full range of grep options to control search behavior, output format, and file processing. Some commonly used options include:
-i, --ignore-case: Ignore case distinctions in the pattern and input data.
-v, --invert-match: Select non-matching lines.
-l, --files-with-matches: Suppress normal output; instead print the name of each input file from which output would normally have been printed.
-n, --line-number: Prefix each line of output with the 1-based line number within its input file.
-r, --recursive: Read all files under each directory, recursively.
-E, --extended-regexp: Interpret pattern as an extended regular expression (normally zfgrep uses fixed strings, but this overrides it).
-P, --perl-regexp: Interpret pattern as a Perl regular expression.
-c, --count: Suppress normal output; instead print a count of matching lines for each input file.

pattern
    The fixed string to search for within the files. By default, zfgrep treats this as a literal string (no regular expression interpretation).

file ...
    One or more compressed or uncompressed files to search. If no files are specified, zfgrep reads from standard input. If a file ends with a known compression suffix (.gz, .bz2, .xz, etc.), it is automatically decompressed before searching.

DESCRIPTION

The zfgrep command is a utility that allows users to search for fixed strings within compressed files. It functions as a wrapper around the standard grep command, automatically decompressing files on-the-fly and piping their content to grep. It is typically implemented as an alias or hard link to zgrep -F, meaning it performs a fixed string search (non-regular expression) by default. zfgrep can handle various common compression formats, including gzip (.gz), bzip2 (.bz2), xz (.xz), lzma (.lzma), compress (.Z), and often zstd (.zst), making it a versatile tool for querying logs and data archives without prior manual decompression.

Its primary advantage lies in saving disk space by allowing direct search on compressed data, while its main distinction from zgrep is its default behavior of treating the pattern as a literal string rather than a regular expression.

CAVEATS

Performance can be impacted as files must be decompressed on the fly, which consumes CPU and I/O resources, especially for very large compressed files.
Requires the corresponding decompression utilities (e.g., gzip, bzip2, xz) to be installed on the system for each compressed file type it encounters.
By default, it performs a fixed string search (-F). If regular expression matching is desired, you must explicitly use -E or -P (or other regex options), or use the zgrep command directly.

DEFAULT BEHAVIOR

zfgrep is functionally equivalent to executing zgrep -F. This means that, unlike plain grep or zgrep which default to basic regular expressions, zfgrep treats the search pattern as a literal string. This is particularly useful when searching for text that might contain special regular expression characters (e.g., '.', '*', '[', ']') which you want to match literally without needing to escape them.

SUPPORTED COMPRESSION FORMATS

zfgrep dynamically detects and handles files compressed with common utilities based on their filename suffixes. This includes .gz (gzip), .bz2 (bzip2), .xz (xz), .lzma (lzma), .Z (compress), and sometimes .zst (zstd). It transparently pipes the decompressed output of these files into grep, allowing seamless searching across various compressed file types.

HISTORY

zfgrep is typically distributed as part of the gzip package (or sometimes zutils) along with other z* utilities like zcat, zless, and zgrep. The zgrep utility was developed to extend grep's functionality to compressed files. zfgrep was later introduced as a convenience wrapper, specifically often implemented as a hard link or script calling zgrep -F. This provides a dedicated command for fixed-string searches on compressed data without needing to explicitly remember the -F option, which is particularly useful for literal text searches in log files and data archives.

SEE ALSO

grep(1): The general purpose text search utility for plain files., zgrep(1): Search compressed files for regular expressions (default is regex, not fixed string)., gzcat(1): Concatenate compressed files to standard output (often used for simple decompression)., bzip2(1): A block-sorting file compressor., xz(1): A command-line tool for compressing or decompressing .xz files.

Copied to clipboard