How to Use grep Command to Find Text in Files in Linux

In Linux, the grep command is used to find out different patterns in the files, and also, if you are supposed to find any specific line of the code, instead of reading the whole script, you can use the “grep” command to extract that specific line.

In this blog, the explanation of the grep command in Linux, as well as its usage, will be explained with the help of different examples. This blog will cover the below-mentioned sections:

Let’s start this guide!

What is the grep Command Utility in Linux?

The grep command in Linux is used to find the patterns in the text files. These patterns can be some specific characters or strings. The general syntax of using the grep command in Linux:

Syntax of the grep Command

$ grep [options] [pattern] filename

The grep command can be used with any of its options, pattern, and file name, in which it is required to find out.

Options of the grep Command

To get the supported options of the grep command, use the below-stated command in Linux:

$ grep --help

The usage of each option can also be observed in the output.

What is the Usage of the grep Command Utility in Linux?

As discussed earlier, the grep command is a useful utility for searching for specific content within the text/code file. This section uses the grep command in Linux:

Example 1: How to Find the Word?

To find the word in any specific file, use the grep command and find the specific word in the file. For example, in the file “myfile”, we find out the word “Hey” using the grep command:

$ grep ‘Hey’ myfile

If the word is found, the complete line containing that word will be printed on the terminal.

Example 2: How to Find the Word in Case Sensitivity Manner?

By default, the grep-based searches are case-sensitive. For instance, the word “hey” is searched in a file that contains “Hey” (not “hey”) using the following command:

$ grep ‘hey’ myfile

No output has been shown because there is no such word in the file named “myfile”. Now, if we use the “-i” option in the above command, the output will be different:

$ grep -i ‘hey’ myfile

Example 3: How to Find the Word in Current Directory/Sub-directories?

To find the word in the current directories and all its sub-directories, we will use the “R” option with the grep command:

$ grep -Ri ‘hey’ myfile

Note: The mechanism of searching a word inside a directory and its sub-directories is known as “recursive search”.

Example 4: How to Find the Two Words?

If it is required to find the two consecutive words, then we have to use the egrep option, which it the variant of the grep command with extended characters. For example, if we are supposed to find out the “Hey” and “Welcome”, use the command:

$ egrep -w ‘Hey|Welcome’ myfile

The output shows that lines containing the word “Hey” and “Welcome” are printed on the terminal.

Example 5: How to Find the Word With its Line Number?

If you need to know on which line of the file the word is present, then you have to use the “-n” option as well. Let’s run the command with the -w option of the grep command:

$ grep -inR ‘hey’ myfile

Example 6: How to Display the Anchor Expression?

The last example is to display the regular expressions of anchors using the grep command. For example, we have already copied the GNU-3 file on the home directory from /usr/share/common-licenses/GPL-3 and will find the “GNU” which is anchored:

$ grep “^GNU” GPL-3

The two lines which are anchored are displayed.

Bonus Tip: What are the Most Used grep Commands in Linux?

We have presented a table below that lists the most used grep commands in Linux.

PurposeCommand
To find the wordgrep ‘[word]’ [file]
To find word without the restriction of case sensitivegrep -i ‘[word]’ [file]
Use grep command to find anchor expressionsgrep “^[anchor expression]” [file name]
Use grep command to find words in directories and its subdirectoriesgrep -inR ‘[word]’ [file]

That’s all from this post!

Conclusion

The grep command can be used to find the words in the files of Linux, which could be in a textual format, code files, or the bash scripts. The grep command offers various options that can be used to search for the words as per the requirement of the user. This post has provided the usage, and the working of the grep command in Linux. Lastly, the user can get the list of the most used variants of the grep command in Linux.