Difference between grep and fgrep command
Last Updated :
27 Sep, 2024
This is the powerful grep and fgrep commands used to search text in a Linux-based system. Despite their similarity in use, which is for matching patterns in file strings, they still appear to be different in their own approach and capabilities. Grep supports regular expressions, including flexible and complex patterns of searching, whereas fgrep, short for "fixed grep," focuses on the search for strings without an interpretation of regular expressions.
The grep filter searches a file for a particular pattern of characters and displays all lines that contain that pattern. The fgrep filter searches for fixed-character strings in a file or files.
Syntax of Grep and Fgrep Commands
Here are the basic syntaxes for grep and fgrep:
Grep Command Syntax:
grep [options] pattern [files]
Fgrep Command Syntax:
fgrep [options] pattern [files]
Difference Between grep and fgrep Command
Feature | 'grep' | 'fgrep' (Deprecated, now grep -F) |
---|
Pattern Type | Supports regular expressions for pattern matching. | Searches for fixed strings only, without interpreting patterns. |
---|
Special Characters | Interprets special characters (like *, .) as part of the regex. | Treats all characters literally, including special characters. |
---|
Performance | Slightly slower when processing complex regular expressions. | Faster for exact string matching, as it skips regex parsing. |
---|
Command Usage | Example: grep "error" file.txt | Example: fgrep "error" file.txt or grep -F "error" file.txt |
---|
Use Case | Used for flexible, pattern-based searches. | Used for simple, exact string searches. |
---|
Recursive Search | Supports recursive search with -r option. | Does not directly support recursive search (use grep -F -r). |
---|
Deprecated | No, still widely used. | Yes, fgrep is deprecated; use grep -F instead. |
---|
Options | Supports various options like -i, -v, -n, etc. | Supports similar options like -i, -v, -n, but no regex support. |
---|
The main difference between both commands is:
- String matching algorithm used by them.
- fgrep always uses the Aho-Corasick algorithm that worst O(m+n) complexity.
- grep command always uses the modified version of Commentz-Walter algorithm which has worst case O(mn) complexity.
- fgrep command interprets the PATTERN as a list of fixed strings separated by newlines. But grep always interpreted as regular expressions.
Similarity between both the commands
Consider the below file named as para2
Hi, are you using geeksforgeeks for learning computer science concepts.
Geeksforgeeks is best for learning.
Consider the below Words:
are
using
geeksforgeeks
learning
concepts
Using grep Command:
$grep -f word para
Output:
Hi, are you using geeksforgeeks for learning computer science concepts.
Geeksforgeeks is best for learning.
Using fgrep Command:
$fgrep -f word para
Output:
Hi, are you using geeksforgeeks for learning computer science concepts.
Geeksforgeeks is best for learning.

Difference between both the commands
Consider the below File :
Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.
Geeks*forgeeks is best for learni\ng.
Consider the below Words :
@re
usin.g
geeks*forgeeks
learni\ng
con/cepts
Using grep Command:
grep -f word para
Output:
Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.
Using fgrep Command:
fgrep -f word para
Output:
Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.
Geeks*forgeeks is best for learni\ng.

Key Options Commonly Used with the Command
Option | Description |
---|
-i | Ignore case while matching patterns. |
---|
-v | Invert the match; show lines that do not match the pattern. |
---|
-r | Recursively search files in directories (for grep, but not supported in fgrep). |
---|
-n | Show line numbers with output. |
---|
-l | List file names where matches occur instead of displaying the actual lines. |
---|
1. -i: This option ignores case sensitivity.
grep -i 'error' /var/log/syslog
fgrep -i 'error' /var/log/syslog
2. -v: This option inverts the match, displaying lines that do not match the pattern.
grep -v 'error' /var/log/syslog
fgrep -v 'error' /var/log/syslog
3. -n: Displays the line number along with each matching line.
grep -n 'error' /var/log/syslog
fgrep -n 'error' /var/log/syslog
4. -l: This option lists the files that contain the pattern but does not show the actual lines.
grep -l 'error' *.log
fgrep -l 'error' *.log
Conclusion
In conclusion, both grep and fgrep commands may be used while doing file searches, though they do differ in functionality, the grep command supports complex regular expressions. It is, therefore highly useful for flexible pattern matching. fgrep is optimized for fixed-character strings where the search could, most probably, be performed significantly faster if no regular expressions were required.
If you need to use pattern-matching capabilities that are greater than the basic capability, then grep is a better choice. But if you're doing an exact string match, the speed would improve for fgrep since it wouldn't have to process regex.
Similar Reads
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
25 Basic Linux Commands For Beginners [2025] While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an o
13 min read
Difference Between IPv4 and IPv6 IPv4 and IPv6 are two versions of the system that gives devices a unique address on the internet, known as the Internet Protocol (IP). IP is like a set of rules that helps devices send and receive data online. Since the internet is made up of billions of connected devices, each one needs its own spe
7 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Balance Factor = left subtree height - right subtree heightFor a Balanced Tree(for every node): -1 ⤠Balance Factor ⤠1Example of an
4 min read
grep command in Unix/Linux The grep command is one of the most useful tools in Linux and Unix systems. It is used to search for specific words, phrases, or patterns inside text files, and shows the matching lines on your screen. Syntax of grep Command in Unix/LinuxThe basic syntax of the `grep` command is as follows:grep [opt
6 min read
What is a Neural Network? Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental
12 min read
Sed Command in Linux/Unix With Examples The SED command (short for Stream Editor) is one of the most powerful tools for text processing in Linux and Unix systems. It's commonly used for tasks like search and replace, text transformation, and stream editing.With SED, you can manipulate text files without opening them in an editor. This mak
8 min read
AWK command in Unix/Linux with examples Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read