Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Difference between grep and fgrep command
Next article icon

Difference between grep and fgrep command

Last Updated : 27 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 grep CommandUsing fgrep Command:

$fgrep -f word para

Output:

Hi, are you using geeksforgeeks for learning computer science concepts. Geeksforgeeks is best for learning.

Using fgrep Command

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.


Next Article
Difference between grep and fgrep command

E

everythingispossible
Improve
Article Tags :
  • Technical Scripter
  • Difference Between
  • Linux-Unix
  • Technical Scripter 2018
  • linux-command

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 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 FTP and SSH
    1. File Transfer Protocol (FTP) : It is a web standard that lets in the technique of document downloading and uploading on distinct pc structures from the internet. It includes unique sorts of record types. File Transfer Protocol or FTP, is a protocol that used to be created for the transferring of
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences