Open In App

How to Recursively Grep all Directories and Subdirectories in Linux

In this article, we will demonstrate how to grep recursively through all directories and subdirectories. But before I do that, let me define what the term “grep” means. Basically, grep means searching or fetching. The grep is one of the basic utility commands of Linux systems. When one needs to determine whether a specific string is present in a text file, one utilises grep. The grep command is a fantastic resource for browsing the contents of all directories and subdirectories. The grep command’s recursive option is used to search all paths and subdirectories for the specified term in all files and child files. 

Recursively Grep all Directories and Subdirectories in Linux

We have created a directory and a few subdirectories in advance in order to demonstrate how to use the grep command. 



A few .txt files(files with extensions can be used too) with content were also included.

 

Syntax of grep command:

The basic syntax of the command for grep is given below



grep "string_to_be_searched" "file_to_be_searched"

The grep command looks for the string_to_be_searched into the file_to_be_searched and if the match is found it returns the complete line in the terminal. 

Also, the grep command is case-sensitive i.e. it will treat string_to_be searched differently for “bugsyy” and “BUGSYY” 

cd Desktop/grep_article
grep bugsyy file1.txt
grep hope file1.txt
grep Hope file.txt

In the below terminal, it is clearly visible that in commands 2 & 4, after the string match is found it returns the complete line(check-in file1.txt).

 

While in command 3, the grep treats “hope” and “Hope” differently, thus no match is found and it thus returns nothing.

 

Example 1: Search Current Working Directory Recursively with grep Command

Grep can be used recursively if we need to search for a string pattern across multiple files in a directory.

In order to use grep recursively, we must add the –R tag after grep and changefile_to_be_searched” to “directory_path“.

Syntax: 

grep -R “string_to_be_searched” “directory_path”

Note:  If the “directory_path” is not mentioned with grep -R it will consider current directory or working directory as “directory_path”.

Terminal:

grep -R bugsyy

Here the directory where the string “bugsyy” is to be searched is not mentioned, so the grep command looks for the string in the current directory.

 

Example 2: Search Specified Relative Path Recursively with grep Command

Here grep searches for the pattern or string in the specified relative path.

Terminal:

grep -R bugsyy subdir1/subd1_file.txt

The relative path is specified by referencing the current working directory.

 

Example 3: Search Specified Absolute Path Recursively with grep Command 

Here grep searches for the pattern or string using the specified absolute path.

Terminal:

grep -R BUGSYY /home/bugsyy/Desktop/grep_article/

The absolute path is specified from the root directory or basically, it is the complete path of the file.

 

Here, the grep searched for the string “BUGSYY” in the /home/bugsyy/Desktop/grep_article/” recursively (-R), and if the match is found it returns the path of the file and also the complete line in which string is present.

Example 4: Search Recursively and Case-insensitive

The case-sensitive nature of the grep command can be handled by using the grep command with the -Ri tag.

Syntax: 

grep -Ri “string_to_be_searched” “directory_path”

Terminal:

grep -Ri BUGSYY /home/bugsyy/Desktop/grep_article/

After using grep with tag -Ri it returns additional two paths compared to that of without using -Ri tag. Here it returns the path of files and lines containing both “BUGSYY” and “bugsyy“.

 

Article Tags :