grep Command in Linux | Explained

The grep is a short form of “global regular expression print” that is utilized to search string characters from specified text files. In Linux, the grep command has significant usage to emphasize/search the specified words, numbers, and characters after a matched pattern. The purpose of this guide is to explain the grep command with all possible examples in Linux systems.

Prerequisites: How to Install grep in Linux Distributions?

In the latest versions of the Linux distributions, the “grep” command line utility comes by default. However, if it is not available in your respective distribution. Then you can install it via the following commands:

For Debian/Ubuntu:

From the package manager, the grep command can be installed in Debian/Ubuntu system through the below syntax:

$ sudo apt install grep

For RHEL/CentOS/Fedora:

To install the grep command in RHEL/CentOS/Fedora systems, follow the below script:

$ sudo yum install grep           

Let’s explore the working of grep commands.

How Does grep Command Work in Linux?

Primarily, the working of the grep command relies on its syntax, which is described below:

Syntax:

$ grep <options> pattern [files]

The grep command requires the “pattern” that can be words, characters, numbers, lines, or regular expressions. Additionally, the “files” specify the file name in the “.txt”format. For this, the general syntax is provided here:

The most common options that are utilized with the grep command are enlisted in the table as below:

OptionsDescription
-lThis option displays the list of filenames.
-hIt displays the matched line.
-cThe option counts the specific word.
-vIt displays all the lines that do not match.
-wIt matches the whole word.

To explore the grep command with all extended options, execute the “grep – help” command as below:

$ grep --help

Let’s explore the practical usage of grep commands in Linux.

How to Use grep Command in Linux?

In Linux, most system administrators utilize the grep command to search specific application names in log files to prevent the installation process. The practical usages of the grep command are explained in this section:

Example 1: How to Search Specific Words?

An example is carried out to search for a specific word in an existing “.txt” file after navigating to a specific directory. The grep command is utilized in the below script to match a pattern “Linux” in the existing “file.txt”:

$ grep linux file.txt

The output shows that the matching pattern “Linux” has been highlighted in “file.txt”.

Example 2: How to Ignore Case Sensitivity in Grep Searches?

The grep command is used with the “-i” option to ignore the case sensitivity issue. In our case, the “SYSTEM” word is searched out in all the content with the “*” symbol as below:

$ grep -i SYSTEM *

After executing the above command, you can verify that the “system” word is highlighted in red color without any capitalization issue.

Example 3: How to List the File Names That Contain a Specific Word?

The grep utility can also display the list of those files where the specific word is available. To do so, the “-l” command is utilized that enlist the name of files:

$ grep -l linux *

The output returns the word “Linux” in three files: “file.txt”, “student.txt” and “teacher.txt”.

Example 4: How to List Down the Files and Matched Content Inside Them? 

If you want to search a word in all subdirectories, the “-r” option is utilized that extracts the specific keyword and is highlighted as below:

$ grep -r linux *

The output shows the matched word is highlighted in three files: “file.txt”, “student.txt”, and “teacher.txt”.

Example 5: How to Count Words in a File?

Let’s carry out an example for counting the word “Ubuntu” in the specified file “file.txt” to check the repetition:

$ grep -c 'Ubuntu' file.txt

The output confirms that the “Ubuntu” keyword is repeated only once in a file.

Example 6: How to Search Two Different Words?

If you want to search for two or more words in a file, you must separate these words through the“|” (pipeline) by utilizing the “-E” option to match the pattern:

$ grep -E "system|linux" file.txt

After executing the above command, you can authenticate that two words “system” and “Linux” is highlighted in “file.txt”.

Example 7: How to Use grep to Search in Bash?

An existing script file is carried out to visualize the usage of the “grep” command. For instance, the word “GOOD” is specified with “-e” to match the exact pattern and highlight it in the existing bash script.

$ grep -e 'GOOD' hello.sh

The output shows that the “GOOD” word has been successfully highlighted in the script “hello.sh” file.

We have learned all about the Linux grep command along with detailed examples.

Conclusion

In Linux, the grep command highlights the specific word, character, or string after searching the entire content in a file. This command matches the specific pattern which is provided by users. Different options are utilized with grep commands, including “-c” to count words, “-w” to match the whole word, “-i” to ignore match case, etc. This guide has explained the grep command with all possible examples and installation in all Linux based operating systems.