Skip to content

fgrep Linux command

fgrep (or fast grep) is a Linux command used to search for specific text patterns in files. It is faster than the grep command as it does not support regular expressions. With fgrep, users can quickly search for literal strings in one or multiple files, making it a useful tool for searching and filtering large datasets efficiently.

fgrep Syntax:

Terminal window
fgrep [options] pattern [file...]

Options:

OptionDescription
-vInvert the sense of matching, to select non-matching lines.
-iIgnore case distinctions in both the pattern and input files.
-wMatch only whole words.
-FTreat the pattern as a list of fixed strings.
-nShow the line number of each matching line.
-hSuppress the display of filenames in output.
-cCount and display the number of matching lines.
-lDisplay only the names of files with matching lines.
-qQuiet mode, do not display any output.

Parameters:

ParameterDescription
patternThe string or regular expression to search for.
fileThe path to the file(s) to search within. Multiple file paths can be provided.

fgrep bash Examples:

Search for a Specific Word in a File

Terminal window
fgrep "example" file.txt

Searches for the word “example” in the file.txt and displays the lines containing it.

Display Lines with Multiple Words

Terminal window
fgrep -e "word1" -e "word2" file.txt

Displays lines containing either “word1” or “word2” in the file.txt.

Ignore Case Sensitivity

Terminal window
fgrep -i "Hello" file.txt

Searches for the word “Hello” in a case-insensitive manner in the file.txt.

Display Line Numbers

Terminal window
fgrep -n "error" logfile.txt

Searches for the word “error” in logfile.txt and displays the line numbers.

Show Count of Matches

Terminal window
fgrep -c "warning" file.txt

Counts and displays the number of occurrences of the word “warning” in file.txt.

Search for Whole Words Only

Terminal window
fgrep -w "exact" file.txt

Locates lines in file.txt with the word “exact” as a whole word.

fgrep Command Help Center:

How do I use fgrep in Linux?

To use the fgrep command in Linux, execute the following command:

Terminal window
fgrep --option <value>

What is the difference between grep and fgrep in Linux?

The main difference between grep and fgrep in Linux is that fgrep interprets search patterns as fixed strings, while grep allows the use of regular expressions for pattern matching.

How can I search for a string in multiple files using fgrep in Linux?

To search for a string in multiple files using fgrep in Linux, you can use the following command:

Terminal window
fgrep "search_string" file1.txt file2.txt

Can I use fgrep to search for multiple strings in a file in Linux?

Yes, you can use fgrep to search for multiple strings in a file in Linux by specifying each string separated by a pipe (|) within double quotes.

Terminal window
fgrep "string1|string2" file.txt

How can I make fgrep case-insensitive in Linux?

To make fgrep case-insensitive in Linux, you can use the -i option in the command. Here is an example:

Terminal window
fgrep -i "search_string" file.txt

Is there a way to display line numbers with fgrep in Linux?

Yes, you can display line numbers with fgrep in Linux by using the -n option in the command. Here is an example:

Terminal window
fgrep -n "search_string" file.txt

Can fgrep be used to search for a whole word in a file in Linux?

Yes, fgrep can be used to search for a whole word in a file in Linux by using the -w option in the command. Here is an example:

Terminal window
fgrep -w "word" file.txt

How can I search for a string recursively in directories with fgrep in Linux?

To search for a string recursively in directories with fgrep in Linux, you can use the -r option in the command. Here is an example:

Terminal window
fgrep -r "search_string" /path/to/directory

Applications of the fgrep command

  • Efficiently searching for fixed strings in files
  • Scripting to quickly find specific text patterns
  • Finding specific strings in large text files
  • Processing log files to extract relevant information