Difference between grep and fgrep command (original) (raw)

Last Updated : 11 Jul, 2025

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:

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 Command**Using 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 geeksforgeeks for learni\ng computer science con/cepts. Geeksforgeeks 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 geeksforgeeks for learni\ng computer science **con/cepts.

**Using fgrep Command:

fgrep -f word para

**Output:

Hi, ****@re** you **usin.g *geeksforgeeks for **learni\ng computer science *con/cepts. Geeksforgeeks 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.