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
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
Searches for the exact string "hello world" in file.txt
Case-insensitive search
Searches for "error" regardless of case
Show line numbers
Shows line numbers along with matching lines
Count matches
Counts occurrences of "TODO" in Python files
Search with special characters
Searches for literal "*.txt" (not as a regex pattern)
Invert match
Shows lines that don't contain "comment"
Whole word matching
Matches "test" as a whole word only
Whole line matching
Matches lines that are exactly "exact line"
Multiple files search
Searches for pattern in all .log files
Recursive directory search
Recursively searches for "config" in /etc/ directory
Show only filenames
Shows only filenames containing "import"
Search for patterns with brackets
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 |