How to Recursively Grep all Directories and Subdirectories in Linux
Last Updated :
24 Sep, 2024
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.
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).
While in command 3, the grep treats “hope” and “Hope” differently, thus no match is found and it thus returns nothing.
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.

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 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 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
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!
Similar Reads
How to Recursively Grep all Directories and Subdirectories in Linux
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 recurs
5 min read
Python List All Files In Directory And Subdirectories
Listing all files in a directory and its subdirectories is a common task in Python, often encountered in file management, data processing, or system administration. As developers, having multiple approaches at our disposal allows us to choose the most suitable method based on our specific requiremen
5 min read
How to Count Files in Directory Recursively in Linux
When exploring directories on your server, you may have come across folders with a large number of files in them. You might wish to know how many files exist in a certain directory or across many directories at times. To put it another way, you wish to count the number of files saved in a directory
5 min read
Renaming All Files and Directory Names to Lowercase in Linux
Here we will see how to rename all files and directory names to lowercase in Linux. We can easily do it with some basic commands in Linux. The below command can be used to view the contents of your directory. $ find [directory_name] -depth Here 'priyanshu' directory which has some sub-files/folders,
1 min read
How to Recursively Find all Files in Current and Subfolders Based on Wildcard Matching in Linux
Traversing through a directory tree to locate files is a common operation performed by most filesystem management software. This utility is in the form of command-line commands in most Operating Systems. In this article, you will learn how to find files using Wildcard Matching in Linux Operating Sys
5 min read
How to List all Files and Directories in FTP Server using Python?
FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network. It is possible using pyth
2 min read
Automated Recursive Encryption in a Directory Using Shell Script
This script would encrypt file provided as an argument or a directory and its constituent files and sub-directories recursively. It would be very useful for automating the encryption of multiple files in a directory all together. How it works? If no arguments are given, throw an error and exit the p
3 min read
Shell Script To Show Names of All Sub-Directories Present in Current Directory
In this given program, we are required to write a shell script to print the list of all the sub-directories present in the current directory. Currently, when we are running this program, the terminal is opened at the root directory location and hence, we are getting a list of sub-directories present
2 min read
How to Scan All Mp3 Files In the Given Directory using Java?
In this article, we will discuss how to scan all mp3 files and not only from a specific path for an android media player. We will use a recursive method for scanning files. Scan all folders and sort out all mp3 files using the File and FileFilter class. Implementation Java Code import java.io.File;
1 min read
How to Use the grep Command in Linux with Examples?
Grep is a very powerful utility in Linux that is used for searching patterns within files or a stream of text. It's one of those essential tools that system administrators and developers use for parsing logs, cleaning up data, or otherwise dealing with large text apa. This tutorial will walk you thr
4 min read
How to remove a directory in R?
In this article, we will discuss how to remove a directory using R programming language. To remove a directory in R we use unlink(). This function deletes the named directory. Syntax: unlink(directory-name, recursive = BOOLEAN) Parameter: directory-name: a character vector with the names of the dire
1 min read
How to Recursively Build a JSON Tree Structure in JavaScript ?
To create a JSON tree structure recursively in JavaScript we need to create a recursive function that traverses the data and constructs the tree. We can create a JSON tree structure recursively in JavaScript using the below methods. Table of Content Using Arrays to build a JSON tree structure Using
4 min read
How to automatically delete/remove files older than x days in Linux
Linux operating systems are widely used for web servers and other data-heavy tasks such as penetration testing etc. In these applications, there can be an accumulation of files that are not useful after a certain period. Now Linux, being an operating system used mostly for its automation functionali
9 min read
Remove all empty files within a folder and subfolders in Python
In this article, we will see how to remove all empty files within a folder and its subfolders in Python. We occasionally produce some empty files that are not needed. Here, we will use the below functions/methods to remove all empty files within a folder and its subfolder imported from the OS module
4 min read
C# Program to Search Directories and List Files
Given files and directories, now our task is to search these files and directories using C#. So to do this task we use the following methods: 1. SearchOption: This method is used to specify whether to search the current directory or the current directory with all subdirectories. Syntax: public enum
2 min read
C# Program For Listing the Files in a Directory
Given files, now our task is to list all these files in the directory using C#. So to do this task we use the following function and class: DirectoryInfo: It is a class that provides different types of methods for moving, creating, and enumerating through directories and their subdirectories. You ca
2 min read
How to Find all Files Containing Specific Text (string) on Linux
Suppose you are looking for a file in Linux, but you have forgotten its name. You only remember the contents of the file. How will you find the file in this case? Well, there are some useful methods that will help you find a file containing a specific text (or string) in Linux. The string needs to b
4 min read
How to print all files within a directory using Python?
The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to
3 min read
Get and Set Working Directory in R
In this article, we are going to see how to get and set a working directory in R Programming Language. How to Get Working directory: getwd(): The getwd() method is used to gather information about the current working pathname or default working directory. This function has no arguments. It returns a
2 min read