The Wayback Machine - https://web.archive.org/web/20250114161550/https://www.geeksforgeeks.org/how-to-recursively-grep-all-directories-and-subdirectories-in-linux/
Open In App

How to Recursively Grep all Directories and Subdirectories in Linux

Last Updated : 24 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The grep command is one of the most frequently used utilities in Linux for searching through text files. It allows users to search for specific strings within files and directories, offering a wide range of options to customize the search process. In this article, we’ll explore how to use the recursive option of grep to search through directories and subdirectories, making it easier to find specific terms across multiple files.

What is grep?

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 utilizes 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 Using Grep in Linux

We have created a directory and a few subdirectories in advance to demonstrate how to use the grep command. A few .txt files(files with extensions can be used too) with content were also included.

Displaying Directories and Subdirectories

 

Syntax of grep command

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. 

Example of a Basic grep Search

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).

Grep command example

 

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

Contents of file1.txt

 

Recursively Grep Through Directories

When you want to search for a specific pattern across multiple files within a directory, grep can be used recursively. The recursive option (-R) allows the command to traverse through subdirectories as well.

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 change “file_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.

Searching Current Working Direcotory Recursively

Search Current Working Directory Recursively with grep Command

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.

Search Specified Relative Path Recursively

Search Specified Relative Path Recursively with grep Command

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.

Search Specified Absolute Path Recursively

Search Specified Absolute Path Recursively with grep Command

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“.

Search Recursively and Case-insensitive

Search Recursively and Case-insensitive

Conclusion

The grep command in Linux helps for searching through files and directories. Its recursive feature allows you to search through entire directory structures, making it indispensable for anyone working with large file systems. With advanced options like case-insensitivity or file inclusion/exclusion, grep has the flexibility to meet your needs.

How to Recursively Grep all Directories and Subdirectories in Linux – FAQs

What is the grep command used for in Linux?

The grep command is used to search for specific patterns or strings within files or directories. It returns matching lines from the file where the pattern occurs.

How can I search for a string in a directory and its subdirectories using grep?

You can use the -R option with grep to search recursively through all directories and subdirectories.

grep -R "string_to_be_searched" "directory_path"

If no directory is specified, it searches in the current directory by default.

Is grep case-sensitive by default?

Yes, grep is case-sensitive by default. It treats lowercase and uppercase characters as different. For example, searching for “hello” will not match “Hello” unless the -i option is used for case insensitivity.

How can I perform a case-insensitive search with grep?

You can perform a case-insensitive search using the -i option with grep. Combine it with -R for recursive search:

grep -Ri "string_to_be_searched" "directory_path"

Master DevOps and also get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

The most sought-after Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Next Article

Similar Reads

three90RightbarBannerImg