LinuxCommandLibrary

fgrep

Search files for fixed string patterns

TLDR

Search for an exact string in a file

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

Search only lines that match entirely in one or more files
$ fgrep [[-x|--line-regexp]] [search_string] [path/to/file1 path/to/file2 ...]
copy

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

Show the line number in the file along with the line matched
$ fgrep [[-n|--line-number]] [search_string] [path/to/file]
copy

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

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

SYNOPSIS

fgrep [OPTION...] PATTERN [FILE...]

PARAMETERS

PATTERN
    The fixed string pattern to search for. All characters are interpreted literally.

FILE...
    One or more files to search. If no files are specified, fgrep reads from standard input.

-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.

-x, --line-regexp
    Select only those matches that exactly match the whole line.

-w, --word-regexp
    Select only those matches that form whole words.

-r, --recursive
    Recursively search subdirectories listed.

-R, --dereference-recursive
    Same as -r, but follows symbolic links.

-l, --files-with-matches
    Suppress normal output; instead, print the name of each input file from which output would normally have been printed.

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

-n, --line-number
    Prefix each line of output with the 1-based line number within its input file.

-q, --quiet, --silent
    Suppress all normal output. Exit immediately with zero status if any match is found, non-zero otherwise.

-f FILE, --file=FILE
    Obtain PATTERNs from FILE, one per line. Empty lines are ignored.

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

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

-C NUM, --context=NUM
    Print NUM lines of context (both leading and trailing) around each match.

DESCRIPTION

fgrep, which stands for "fixed grep," is a variant of the grep command designed specifically for searching for fixed strings or literal patterns within files. Unlike the standard grep command, fgrep does not interpret special characters as regular expressions; instead, it treats every character in the search pattern literally. This makes it particularly useful when you need to find exact matches of text that might contain characters that would otherwise have special meaning in regular expressions (e.g., '.', '*', '[', ']').

Historically, fgrep was often faster than grep for literal searches because it could use simpler string matching algorithms without the overhead of regex parsing. In modern Unix-like systems, fgrep is typically implemented as a symlink or hardlink to grep -F. This means that invoking fgrep is functionally equivalent to running grep with the -F (or --fixed-strings) option. While it's still available for backward compatibility and convenience, grep -F is the more explicit and recommended way to perform fixed-string searches.

CAVEATS

The primary caveat of fgrep is that it does not support regular expressions. Any character in the search pattern, including those typically special in regex (like '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '^', '$', '\'), will be treated as a literal character. This is its defining characteristic and its limitation for complex pattern matching.

While still widely available, fgrep is considered largely deprecated in favor of grep -F. Users should be aware that fgrep might behave slightly differently or be absent on highly stripped-down or older systems compared to modern grep implementations.

DEPRECATION AND MODERN USAGE

While fgrep still functions on most systems, it's generally recommended to use grep -F instead. This approach is more explicit about the search type and ensures consistent behavior across different grep versions. For example, instead of `fgrep 'my.file' *`, it's better to use `grep -F 'my.file' *`.

HISTORY

fgrep emerged as a distinct command alongside grep and egrep in early Unix systems. Its creation was driven by the need for an efficient way to search for literal strings without the overhead of a regular expression engine. For many years, it was a separate executable optimized for this specific task. As grep implementations evolved and became more sophisticated, the functionality of fgrep was integrated into the main grep command via the -F (or --fixed-strings) option. In most contemporary Unix-like operating systems, fgrep is no longer a standalone program but rather a hardlink or symbolic link to grep, with an implied -F option. This ensures backward compatibility for scripts and users accustomed to the original command, while leveraging the single, powerful grep executable.

SEE ALSO

grep(1), egrep(1)

Copied to clipboard