- Unix Commands Reference
- Unix Commands - Home
fgrep Command in Linux
The fgrep command in Linux searches for fixed-character strings in a file. The fixed-character strings are literal text rather than patterns. Therefore, the pattern [a-z] will be treated as a string rather than as a pattern for matching lowercase letters from a to z. This means that regular expressions cannot be used with the fgrep command. The fixed-character string search makes fgrep faster because the system does not need to interpret complex patterns.
Table of Contents
Here is a comprehensive guide to the options available with the fgrep command −
Note − The fgrep command is considered deprecated in Unix/Linux distributions. It is replaced with grep -F as it offers the same functionality.
Syntax of fgrep Command
The syntax of the Linux fgrep command is as follows −
fgrep [options] [string] [file]
In the syntax, the [options] field is used to specify options to modify the command's behavior. The [string] signifies the string pattern, and [file] is used to specify the file in which the search will be made.
Options of fgrep Command
The options for the fgrep command are listed below −
Options | Description |
---|---|
-b | It is used to display the byte offset of the line on which the string was found (the first block is 0) |
-c | It is used to display the count of the matching lines |
-e list | It is used to search for a string or multiple strings |
-f file | It is used to take a list of strings from a file |
-h | It is used to suppress the file names in multi-file search |
-i | It is used to ignore the case |
-l | It is used to display the file names in which the pattern is found |
-n | It is used to display the line number on which the string is found |
-s | It is used to suppress error messages |
-v | It is used to display all lines except the one that contains the string |
-x | It is used to print the line that is matched completely |
Examples of fgrep Command in Linux
This section demonstrates the usage of the fgrep command in Linux through examples −
- Searching a String in a File or Files
- Displaying Block Number
- Displaying Matching Line Count
- Searching a List of Strings
- Searching a List of Strings from a File
- Suppressing Filenames
- Ignoring the Case
- Displaying Filenames that Contain the String
- Displaying the Line Number
- Suppressing Errors Messages
- Displaying Lines without the Specified String
- Displaying Lines that Completely Match the String
Searching a String in a File or Files
To search for a string in a file, use the fgrep command with the string and file name. For example, to search lines that contain Linux, use −
fgrep Linux file.txt
The matched characters will be highlighted in red as shown in the output image.
To search a string from multiple files, simply mention the filenames −
fgrep Linux file1.txt file2.txt
The lines are displayed with the filenames from which they belong.
Displaying Block Number
To display the block number of the line on which the string pattern is found, use the -b option −
fgrep -b Linux file.txt
The block number is referred to as the byte offset of the matching line within the file. It essentially indicates the position in bytes from the beginning of the file.
Displaying Matching Line Count
To display the number of lines in which the specified string pattern is found, use the -c option −
fgrep -c Linux file.txt
The output shows that the Linux is present in 4 lines of the file.
Searching a List of Strings
To search a list of strings from a file or list of files, use the -e option.
fgrep -e Linux -e distributions file.txt
This option is also useful to search a string that starts with a dash (-). Linux may interpret a dash with a string as an option, to prevent it, use the -e option.
fgrep -e -Linux file.txt
Searching a List of Strings from a File
If the list of strings is in a file, then using the -f option the file can be mentioned for search.
fgrep -f pattern.txt file.txt
In the above example, the strings in the pattern.txt file are searched in the "file.txt" file.
Suppressing Filenames
If the search occurs in multiple files, the fgrep command displays the name of the file where the pattern is found. To suppress the filename, use the -h option.
fgrep -h Linux file1.txt file2.txt
Ignoring the Case
To make the search case-insensitive, use the -i option −
fgrep -i linux file.txt
Displaying Filenames that Contain the String
To display only the filenames that contain the specified string without repeating, use the -l option −
fgrep -l Linux file1.txt file2.txt file3.txt
Displaying the Line Number
To display the line number on which the string is found, use the -n option −
fgrep -n Linux file.txt
The Linux appears on lines 1, 2, 3, and 5 as shown in the output image.
Suppressing Errors Messages
To suppress the error messages, use the -s option −
fgrep -s Linux file4.txt
Displaying Lines without the Specified String
To display all the lines that do not contain the specified string, use the -v option −
fgrep -v Linux file.txt
All the lines are displayed except the lines that contain the Linux string.
Displaying Lines that Completely Match the String
To display a line that completely matches the specified string, use the -x option −
fgrep -x "Linux is open-source." file.txt
The specified string should exactly match, even if you miss a full stop the line will not be displayed.
Conclusion
The fgrep command in Linux is used to search a string from a file or a list of files. This is a handy tool for quick searches. Note that it cannot be used for searching complex patterns like regular expressions.
The fgrep command line tool is deprecated and replaced with the grep -F command. It is recommended to use grep -F instead of fgrep.
In this tutorial, we explained the fgrep command, its syntax, options, and usage through examples.