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
Difference between Program and Command
1. Program : Program, as name suggest, are simply set of instructions developed by programmers in programming languages that consist of compiled code and run directly from computers operating system. 2. Command : Command, as name suggests, are instruction given by user to computer to perform specifi
2 min read
Difference between MRP and ERP
1. Material Requirement Planning (MRP) : Developed in 1970s, raw material whenever required by any organization is managed i.e, which materials are required by company gets stored in a database. Also, it tells about the shortage of any material. Material Requirement Planning is widely used approach
2 min read
Difference between MRP and DRP
1. Material Requirement Planning (MRP) : Developed in 1970s, raw material whenever required by any organization is managed i.e, which materials are required by company gets stored in a database. Also, it tells about shortage of any material. Material Requirement Planning is widely used approach for
2 min read
Difference between vi Editor and cat Command
Both vi editor and cat command are used to input contents in a file. 1. vi Editor : vi editor stands for the visual editor which is an intelligent editing tool in UNIX Operating System used for reading, creating files, editing them, and more. It is the default editing tool in UNIX. Command to open a
3 min read
Difference between OSPF and IGRP
1. Open Shortest Path First (OSPF): OSPF uses a link-state routing algorithm. Using the link state information which is available in routers, it constructs the topology in which topology determines the routing table for routing decisions. It supports both variable-length subnet masking and classless
3 min read
Difference between FTP and TFTP
FTP is used in transferring files over a network while TFTP also works in the transfer of files only that it is used differently than FTP. FTP is advanced, secured, and contains many features than TFTP, TFTP on the other hand is a small transfer that is used for simple things like booting devices or
6 min read
Difference between FTP and HTTP
HyperText Transfer Protocol (HTTP) and File Transfer Protocol(FTP) are the protocols used for file transfer between client and server. There is a lot of difference between FTP and HTTP. In this article, we will learn what are the differences between HTTP and FTP.File Transfer Protocol (FTP)It stands
6 min read
Difference between Program and File
1. Program : Program, as name suggest, are simple executable files that contain set or collection of instructions used by computer to execute or complete particular tasks as well as produce results you want. 2. File : File, as name suggests, is basic concept in computer that is designed to store dat
2 min read
Difference between FTPS and SFTP
FTPS (formerly known as FTP over TLS/SSL) and SFTP (technically named the SSH2 File Transfer Protocol) are two common methods for securely transferring files over the internet. FTPS is actually an extension of the FTP, and it adds support for protecting information through SSL/TLS authentication. Wh
5 min read
Difference between Gradle and Maven
Software is a program or set of programs containing instructions that provide the desired functionality and Engineering is the process of designing and building something that serves a particular purpose and finds a cost-effective solution to problems. Gradle and Maven are different tools used to bu
4 min read