Recursively Search all Files for a String in Linux

The grep command used to find a particular string in one or multiple files in Linux. By using grep command you can search text files for specific words or string patterns. In this tutorial, you are going to learn how to Recursively Search all Files for a String in Linux.

Recursively Search all Files for a String Using grep Command

1. Basic Syntax for recursive search

To recursively search all subdirectories using grep command we can use -r option with it. Following is the basic syntax for recursive search using the grep command.

grep -r "WORD_TO_SEARCH" FILES

2. Search a word in the current directory using grep command

To search a word or string inside current working directory files and subdirectories you can use a simple grep command with -r option.

For example, you can search a word server inside all files in the current working directory using the following command:

grep -r "server"

3. Search a word in the specific directory

To search all files for a string in a specific directory you can specify a particular directory with grep command.

For example, to search all configuration files with string server inside the /etc directory you can use the following command:

grep -r "server" /etc/*.conf

The output should be:

How to Recursively Search all Files for a String in Linux – search etc directory

4. Search all files with a string with match words

For match word case -w option is used. So you will get output with only matching specific word files.

For example to search all configuration files with word match command type following in the terminal:

grep -rw "server" /etc/*.conf

5. Search all files with a string case-insensitively

To all files with string case-insensitively, you can use -i flag with it, like given below.

grep -r -i "server" /etc/*.conf

6. List all files with matching string

You can list all files with matching string using -l option:

grep -r -l "server" /etc/*.conf

The output should be:

How to Recursively Search all Files for a String in Linux – list all files with string

To list all files without matching string use -L option with it:

grep -r -L "server" /etc/*.conf

Recursively Search all Files for a String Using find Command

You can also use find command with grep the command to search recursively for the string in Linux.