Difference between grep and fgrep command

Both grep and fgrep are Linux commands used to search for strings in files, directories, or command output. The key difference is that grep supports regular expressions, while fgrep treats the search pattern as a fixed (literal) string.

grep (Global Regular Expression Print)

grep searches for strings or regular expressions in files. It interprets special characters like ., *, ^, $, [ ] as regex metacharacters. It uses the Boyer-Moore algorithm for fast searching.

fgrep (Fixed grep / grep -F)

fgrep (equivalent to grep -F) searches for fixed strings only. It does not recognize regular expressions or metacharacters − all characters are treated as literals. It uses the Aho-Corasick algorithm with O(m+n) worst-case complexity, making it efficient for searching multiple fixed patterns.

Example

Consider a file sample.txt containing −

hello world
hello.world
h*llo

The difference in behavior between grep and fgrep ?

# grep treats "." as regex (matches ANY character)
grep "hello.world" sample.txt
# Output: hello world
#         hello.world

# fgrep treats "." as literal dot
fgrep "hello.world" sample.txt
# Output: hello.world

# grep treats "*" as regex quantifier
grep "h*llo" sample.txt
# Output: hello world
#         hello.world
#         h*llo

# fgrep treats "*" as literal asterisk
fgrep "h*llo" sample.txt
# Output: h*llo

Key Differences

Feature grep fgrep (grep -F)
Full Form Global Regular Expression Print Fixed grep
Regular Expressions Supported (metacharacters interpreted) Not supported (all characters are literal)
Search Algorithm Boyer-Moore Aho-Corasick − O(m+n)
Metacharacters Treated as special (. * ^ $ [ ]) Treated as normal characters
Best For Pattern matching with regex Exact literal string searching

Conclusion

Use grep when you need regular expression pattern matching. Use fgrep (or grep -F) when searching for exact literal strings, especially when the search pattern contains special characters that should not be interpreted as regex.

Updated on: 2026-03-14T18:43:41+05:30

865 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements