fgrep

text processingLinux/Unix
The fgrep command is one of the most frequently used commands in Linux/Unix-like operating systems. fgrep Search for fixed strings in files

Quick Reference

Command Name:

fgrep

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

fgrep [options] [arguments]

Common Use Cases

    Syntax

    fgrep [options] pattern [file...]

    Options

    Option Description
    -i, --ignore-case Ignore case distinctions in patterns and data
    -v, --invert-match Select non-matching lines
    -n, --line-number Display line number with output lines
    -c, --count Print only a count of matching lines per file
    -l, --files-with-matches Print only names of files containing matches
    -L, --files-without-match Print only names of files containing no match
    -f, --file=FILE Obtain patterns from FILE, one per line
    -r, --recursive Recursively search subdirectories
    -h, --no-filename Suppress the file name prefix on output
    -H, --with-filename Print file name with output lines

    Examples

    How to Use These Examples

    The examples below show common ways to use the fgrep command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    fgrep "search string" filename.txt
    Search for the exact string "search string" in filename.txt.
    fgrep -i "search string" filename.txt
    Search for "search string" case-insensitively in filename.txt.
    fgrep -n "search string" filename.txt
    Display line numbers along with matching lines. # Advanced Examples Advanced fgrep -l "search string" *.txt List only the names of files containing the pattern. fgrep -c "search string" *.txt Count the number of matching lines in each file. fgrep -v "search string" filename.txt Show lines that do NOT match the pattern. fgrep -r "search string" /path/to/directory Search recursively through all files in the directory. fgrep -f patterns.txt filename.txt Search for multiple patterns listed in patterns.txt.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The fgrep command (fixed grep) is a specialized version of grep that searches for fixed strings rather than regular expressions. This makes it faster for simple searches. Key features of fgrep: 1. Fast Simple Searches: fgrep is optimized for searching plain text strings without special pattern matching capabilities, making it faster than standard grep for basic searches. 2. Literal Interpretation: Unlike regular grep, fgrep treats all characters literally. Characters that would normally have special meaning in regular expressions (like ., *, $, ^) are treated as normal characters. 3. Multiple Pattern Searching: fgrep can efficiently search for multiple string patterns simultaneously when using the -f option to specify a file containing patterns. 4. Common Use Cases: It's particularly useful for searching logs, configuration files, or any text where you need to find exact strings rather than patterns. 5. Command Equivalence: In most modern systems, fgrep is equivalent to 'grep -F', as fgrep has been deprecated in favor of grep with the -F (or --fixed-strings) option. 6. Compatibility: While still available on most Unix-like systems for backward compatibility, it's generally recommended to use 'grep -F' in new scripts for better portability. Note that on many modern Linux distributions, fgrep is a symbolic link to grep, which implements the fixed-string functionality when invoked as fgrep or when given the -F option.

    Related Commands

    These commands are frequently used alongside fgrep or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the fgrep command works in different scenarios.

    $ fgrep
    View All Commands