fgrep Command

The fgrep command searches for fixed strings in text files. It's equivalent to grep -F and treats search patterns as literal text rather than regular expressions, making it faster for simple string searches.

Syntax

fgrep [OPTIONS] PATTERN [FILE...]

Description

The fgrep command performs fixed-string searches, treating the search pattern as literal text. It doesn't interpret regular expression metacharacters, making it ideal for searching for strings that contain special characters.

Key features:

  • Treats patterns as literal fixed strings
  • Faster than grep for simple string searches
  • No regular expression interpretation
  • Safe for patterns containing special characters
  • Case-sensitive and case-insensitive options

Common Options

Option Description
-i Case-insensitive matching
-v Invert match (show non-matching lines)
-n Show line numbers
-c Count matching lines
-l Show only filenames with matches
-r Recursive search in directories
-w Match whole words only
-x Match whole lines only

Examples

Basic string search

fgrep "hello world" file.txt

Searches for the exact string "hello world" in file.txt

Case-insensitive search

fgrep -i "ERROR" logfile.txt

Searches for "error" regardless of case

Show line numbers

fgrep -n "function" script.js

Shows line numbers along with matching lines

Count matches

fgrep -c "TODO" *.py

Counts occurrences of "TODO" in Python files

Search with special characters

fgrep "*.txt" config.ini

Searches for literal "*.txt" (not as a regex pattern)

Invert match

fgrep -v "comment" file.txt

Shows lines that don't contain "comment"

Whole word matching

fgrep -w "test" file.txt

Matches "test" as a whole word only

Whole line matching

fgrep -x "exact line" file.txt

Matches lines that are exactly "exact line"

Multiple files search

fgrep "pattern" *.log

Searches for pattern in all .log files

Recursive directory search

fgrep -r "config" /etc/

Recursively searches for "config" in /etc/ directory

Show only filenames

fgrep -l "import" *.py

Shows only filenames containing "import"

Search for patterns with brackets

fgrep "[ERROR]" application.log

Searches for literal "[ERROR]" string

fgrep vs grep vs egrep

Command Pattern Type Use Case
fgrep Fixed strings (literal) Exact string matches, special characters
grep Basic regular expressions Pattern matching with basic regex
egrep Extended regular expressions Advanced pattern matching

See also