The “grep” Command in Linux [10+ Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The grep command is very useful for searching files and directories containing matching words or characters lines. You will also be able to know the pathname of the files, and line numbers with it. In this article, I will describe all the features and uses of the grep command in Linux with multiple examples.

A. Description

The grep command can be used to search patterns in specific files or each file. Then prints the entire line containing the match. It is the shortcut for “Global Regular Expression Print

B. Syntax

The syntax for the grep command is pretty simple which is the command itself followed by some specific options and the pattern you want to grep.

grep [OPTION]... PATTERNS [FILE…]
Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time. Then some PATTERN you want to search for. Moreover, 3 dots after FILE suggest that you can specify multiple files to search through.

C. Options

Many useful options are available for the grep command. I have listed some of the most used ones here. However, if you want to know more about options for the grep command you can always check the man page for the grep command.

man grep

Useful Options

  • -c, –count (Does not print any matches but rather the total occurring number)
  • -i, –ignore-case(Ignores the sensitivity of cases)
  • -v, –invert-match (Invert the sense of matching,  to select non-matching lines.)
  • -V, –version (Output the version number of grep and exit.)
  • -w, –word-regexp (search for a whole word)
NB: The options in Linux CLI(Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “grep” Command in Linux

The grep command in Linux is an easy-to-use command that is used to search for words or characters matching lines in files. Some of the most useful applications of the grep command have been illustrated below.

Example 1: Search Inside a File Using the “grep” Command in Linux

You can search for a specific word or character inside a file using the grep command. To search for any word inside a file you should follow the below procedure. Here, I have searched for the word “Linux” in a file named file1.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep Linux file1.txt

➌ Press the ENTER button.

Output >

You can see in the below picture that all the lines are printed containing the matching word Linux.Search Inside a File Using the “grep” Command in Linux


Similar Readings


Example 2: Search Inside Multiple Files Using the “grep” Command in Linux

You can search for a specific word or character inside multiple files using the grep command. To search for any word inside multiple files you should follow the below procedure. Here, I have searched for the word “Linux” inside 2 files named file1.txt & file2.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep Linux file1.txt file2.txt

➌ Press the ENTER button.

Output >

You can see in the below picture that all the lines are printed containing the matching word “Linux” inside both files.Search Inside Multiple Files Using the “grep” Command in Linux

Example 3: Search Multiple Words Inside a File Using the “grep” Command in Linux

You can search for multiple words or characters inside a file using the grep command. To search for multiple words inside a file you should follow the below procedure. Here, I have searched for the words “Linux” & “love”  inside a file named file2.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep -e Linux -e love file2.txt

➌ Press the ENTER button.

Output >

You can see in the below picture that all the lines are printed containing both the matching words “Linux” & “love” inside the file.Search Multiple Words Inside a File Using the “grep” Command in Linux

Example 4: Search All Files Inside a Directory

You can search for a specific word or character inside all the files in a directory using the grep command. To search for any specific word inside all files in a directory you should follow the below procedure. Here, I have searched for the word “Linux” in all the files in the Desktop directory using the asterisk (*) sign.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep Linux *

➌ Press the ENTER button.

Output >

You can see in the below picture that all the lines are printed containing the matching word Linux inside to files of the Desktop directory.Search All Files Inside a Directory


Similar Readings


Example 5: Finding Whole Words Only Using the “grep” Command in Linux

Normally grep gives you the matching words as well as the substring of the matching words. You can search for only the whole word or character inside a file using the grep command. To search for the whole word only inside a file you should follow the below procedure. Here, I have searched for the whole word “Linux” only inside my home directory.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep -w Linux *

➌ Press the ENTER button.

Output >

You can see in the below picture that all the lines are printed containing the matching of the whole word Linux only. It has not highlighted the word LinuxSimply as here Linux is a substring of the word.Finding Whole Words Only Using the “grep” Command in Linux

Example 6: Searching Inside Sub-directories Using the “grep” Command in Linux

You can search for a specific word or character inside sub-directories using the grep command. To search for any word inside a subdirectory you should follow the below procedure. Here, I have searched for the word “Linux” in my home directory.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep -r Linux *

➌ Press the ENTER button.

Output >

You can see in the below picture that all the lines are printed containing the matching word Linux. All the files in the sub-directory and their pathname is also printed here.Searching Inside Sub-directories Using the “grep” Command in Linux

Example 7: Display Line Numbers Using the “grep” Command in Linux

You can search for a specific word or character inside a file and also show the line numbers using the grep command. To search for any word inside a file and show the line numbers you should follow the below procedure. Here, I have searched for the word “Linux” in a file named file1.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep -n Linux file1.txt

➌ Press the ENTER button.

Output >

You can see in the below picture that all the lines are printed containing the matching word Linux. And their line numbers are also printed here.Display Line Numbers Using the “grep” Command in Linux

Example 8: Display the Number of Lines Before and After

You can search for a specific word or character inside a file and also show the number of lines before, after and both using the grep command. To search for any word inside a file and show the number of lines, you should follow the below procedure. Here, I have searched for the word “Linux” in a file named file1.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep -B2 Linux file1.txt

➌ Press the ENTER button.Display two Lines Before using the grep ➍ Type the following command in the command prompt:

grep -A2 Linux file1.txt

➎  Press the ENTER button.Display two Lines After using the grep ➏ Type the following command in the command prompt:

grep -C2 Linux file1.txt

➐ Press the ENTER button.

Output >

You can see in the first picture 2 lines are printed before the matching word line for the first command.

For the second command, 2 lines after the matching word line are printed in the second image.

And for the last command, 2 lines after and before both are printed in the third picture.Display two Lines Before and After


Similar Readings


Example 9: Ignore Case-Sensitivity

The grep command by default doesn’t show the matching words if the name’s cases are wrong. To ignore the case sensitivity you should follow the below procedure. Here, I have used the file2.txt file to show you the example.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep -i Linux file2.txt

➌ Press the ENTER button.

Output >

You can see in the below picture that all the lines are printed containing the matching word Linux although the case in the command line is not correct.Ignoring Case-Sensitivity

Example 10: Inverse grep Search

You can search inside a file where the line containing the given word or character will not be printed. To do so you should follow the below procedure. Here, I have given the word “Linux” in a file named file1.txt, where I do not want to see the lines containing the given word.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep -v Linux file1.txt

➌ Press the ENTER button.

Output >

You can see in the below picture that all the lines are printed except the lines containing the matching word Linux.Inverse grep Search

Conclusion

After completing this article, you will acquire all the knowledge about the grep command in Linux. With the given practical examples, you will be able to execute many commands to grep the location of different matches on your machine.


Similar Readings

5/5 - (7 votes)
Borhan Uddin

Hello, I am Borhan Uddin, a resident of Dhaka, Bangladesh. I have completed my undergraduate degree in Electrical and Electronic Engineering (EEE) from Chittagong University of Engineering and Technology (CUET). I love to spend my leisure by playing and watching various movies & dramas. Currently, I am working as a Linux Content Developer Executive here. I would like to learn more about Linux every day of the week and would be keen to share it with you rapidly. Read Full Bio

Leave a Comment