Linux Commands

How to Find Text in Files Using the Linux Grep Command?

grep command in Ubuntu Linux

Grep (Global Regular Expression Print) is a powerful Unix/Linux command line tool widely used by network administrators, network engineers, etc. for searching string patterns in log files. In this article, we will demonstrate different ways of searching and filtering string patterns in files using the grep command in Ubuntu 22.04 LTS.

Prerequisites

To follow this article, we will require

  • System with Ubuntu 22.04 LTS Installed.
  • Basic knowledge of command line usage.

What is the grep Command?

Grep is a Unix/Linux command line tool used to search and filter out string patterns in a file or streams, etc. “grep” is very useful in searching through large log files and streams.

How to Find/Search Text in Files via grep Command?

We will discuss different ways to filter out string patterns using the Linux grep command in the following sections:

How to Find/Search a String Pattern in a File?

If we would like to search for a string pattern in a file we can use the following syntax:

$ grep <string> file

We can replace the string with the desired pattern and mention either the file name or the path of the file in the syntax. For example, the following command filters out the string pattern “congestion” in “samplefile1.txt”:

$ grep Congestion Downloads/samplefile1.txt

From the above image, it can be seen that the grep command has filtered out the desired string pattern, i.e., “Congestion” by highlighting all the occurrences. Similarly, we can navigate to a specific directory, if we would like to search using the file name. An example is as follows:

$ cd Downloads

Now that we have navigated to the directory containing samplefile1, we can use the below command to search “Congestion”:

$ grep Congestion samplefile1.txt

Additionally, if we would like to search multiple words, we can search by enclosing them in double quotation marks, i.e., “ “. For example, if we would like to search “Network Congestion” in samplefile1.txt, we will run the following command:

$ grep "Network Congestion" samplefile1.txt

From the above output, it can be seen that all the lines in the file containing the string “Network Congestion” are filtered out.

How to Find/Search a String Pattern in Multiple Files?

We can search a string pattern in multiple files using the below syntax:

$ grep <string> file1 file2 file3

The below example shows the occurrences of the string “Congestion” in three files, i.e., samplefile1, samplefile2, and samplefile3:

$ grep Congestion samplefile1.txt samplefile2.txt samplefile3.txt

We can see from the above image that all occurrences of the string “Congestion” in selected three files are highlighted.

How to Find/Search a String Pattern in Sub-directories?

We can search for a string pattern in all subdirectories by adding the “-r” flag followed by the string to search:

$ grep -rw congestion *

From the above image, it can be seen that occurrences of a string in all sub-directories are highlighted. Further “-w” flag is added to find whole world occurrences.

How to Find Whole Words Only in Files?

In order to find the whole word, we can use the flag “-w” with the grep command:

$ grep -w Congestion *

From the above image, it can be seen that grep has highlighted all occurrences of the searched string, i.e., Congestion.

How to Ignore Case in Grep Searches?

Grep searches are case-sensitive by default. In case, we would like to search for a string and ignore the case, we can search by adding the flag “-i” followed by a string to search:

$ grep -i congestion *

The above image shows all occurrences of string “congestion” irrespective of the case.

How to Identify Lines that Exactly Match the Searched String?

The grep command searches the entire line that completely matches a string pattern by adding a “-x” flag. An example of a search of the “Network Congestion” string in a file is shown below:

$ grep "Network Congestion" *
$ grep -x "Network Congestion" *

In the above image, we can see that if we search “Network Congestion”, we find few matches, but when we search the same for a complete line match using the “-x” flag, there is no match in the file.

How to List the Name of Matching Files that Contains the Searched String?

grep enables the searching of files containing a certain string pattern. An example is as follows:

$ grep -l throughput *

From the above image, we can see that samplefile1.txt and samplefile2.txt contain the string “throughput”.

The Count of Number of Matches

The grep command allows one to count the number of matches in a file. The below example shows the count of occurrences of the string “network” in a directory:

$ grep -c network *

From the above image, we can see that the word “network” has appeared 11, 23, and 11 times in samplefile1.txt, samplefile2.txt, and samplefile3.txt respectively.

Conclusion

The Linux grep command is used extensively to search and filter out string patterns in a file, multiple files, sub-directories, etc. For example, searching part of a word, whole word, exact matching string, case-sensitive search, etc. Additionally, the grep command can filter out all the files that contain a particular string pattern and count occurrences of a string in files. In this article, different ways of searching string patterns in file(s) using the grep command were discussed.

Similar Posts