Linux and Unix fgrep Command With Examples

thelinuxterminal

Author - Sanjay

Introduction

Most of us knows grep command in linux . We have discussed about grep command in our recent post . But we have two more variants of grep i.e fgrep and egrep . We will talk about fgrep in this post.

GREP stangs for Group Regular Expression Pattern .

Table of Contents

Pre-Requisistes

A CentOS/RHEL 7 production server or a Linux VPS .

Installing fgrep on linux

If you have grep instaled on your system , then fgrep is also present by default . fgrep is nothing but grep command with -F options.

Grep is mostly inbuild in 90% of Unix and Linux OS .

man grep

If you see any output etaling usage then you already hae it installed .

What is fgrep command in Linux?

The fgrep command in Linux is a command line utility for searching plain text files for lines that match a given regular expression. The fgrep command searches for matches to the expression in the input files and prints them to the output.

How to use fgrep command in Linux with examples?

The fgrep command has the following syntax:

fgrep [OPTION]... PATTERN [FILE]...

And here are some common options you can use with the fgrep command:

-i : Ignore case.

-v : Invert the match.

-w : Match whole words only.

Have a Look at Common Use Cases where we can use fgrep command .

  1. The fgrep command is a powerful tool for searching for text patterns in files.

  2. fgrep can search for text patterns that are fixed strings, or that use regular expressions.

  3. fgrep can be used to search for text in multiple files at once, or to search for text in a specific file or files.

  4. fgrep can be used to search for text in compressed files, and can also search for text in binary files.

  5. fgrep can be used to search for text in a directory tree, and can also search for text in a specific file or files.

  6. fgrep can be used to search for text in a file or files, and can also search for text in a specific directory or directories.

  7. fgrep can be used to search for text in a file or files, and can also search for text in a specific file or files, and can also search for text in a specific directory or directories.

Here are some examples of using the fgrep command in Linux:

To search for the word "Linux" in a file named file.txt, you would use the following command:

fgrep Linux file.txt

To search for the word "Linux" in all files in the current directory, you would use the following command:

fgrep Linux *

To search for the word "Linux" in all files in the current directory and its subdirectories, you would use the following command:

fgrep -r Linux *

To invert the match and print all lines that do NOT contain the word "Linux", you would use the following command:

fgrep -v Linux file.txt .

To search for a string in all files in the current directory and its subdirectories, use the -r (recursive) option:

fgrep -r "string" *

To search for a string in a specific file, use the -f (file) option:

fgrep -f "string" filename

To search for a string in a list of files, use the -l (list) option:

fgrep -l "string" file1 file2 file3

To invert the search and print all lines that do not contain the search string, use the -v (invert) option:

fgrep -v "string" *

Conclusion

We have discussed on most of the common use cases for fgrep command in linux and unix . This command along with grep command that we have discussed earlier is one of the most commonly used commands while working on porojects or in roduction support where we need to check the server logs .