How to grep All Files in a Directory Recursively

In this Linux tutorial we are going to learn how to grep all files in a directory Recursively in Linux using the grep command.

Most of the time we use grep command to search string in a Text File. But what if you want to search a string in all files in a Directory ?

To grep All Files in a Directory Recursively, we need to use -R option.

grep -R string /directory

When -R options is used, The Linux grep command will search given string in the specified directory and subdirectories inside that directory.

If no folder name is given, grep command will search the string inside the current working directory.

Example

grep -R error /var/log/

In the Above example, Linux grep command will search for the string 'error' inside the /var/log/ folder and subfolders of the /var/log/ folder.

grep recursive search - grep all files in folder

Case Insensitive Recursive Search

The -R option can combined with -i option to make the grep search case insensitive.

grep -Ri error /var/log/

The above command will grep all files in the /var/log/ directory Recursively, But this time the grep command will ignore the case.

Return Filename only in the grep recursive search

When you grep All Files in a Directory Recursively, Both Filename and the matching lines are returned as the output. But if the -l option i used, only the filename will return.

Example

grep -R -l error /var/log

In the above example, We used -l option in the grep recursive search. The grep command will search for the string 'error' and will return the files which contains the string 'error'.

How to grep All Files in a Directory Recursively

Exclude Directories From the grep recursive search

The --exclude-dir option use to exclude folders from the search when search Files in a Directory Recursively.

Example 1

grep -R -l --exclude-dir=journal error /var/log/

As per the above example grep command will exclude the folder journal from the recursive search.

Example 2

grep -R -l --exclude-dir=httpd --exclude-dir=journal error /var/log/

The above command will grep all files in /var/log/ directory, but both journal and httpd folders will exclude from the search.

Files without match - Inverse Recursive Search in grep

One other useful option when grep All Files in a Directory is to return all files which do not match the given text pattern.

This is Done by using either -L or --files-without-match option in the grep recursive search.

Example

grep -R -L error /var/log/

As per the Above Example, the grep command will return all files inside the /var/log/ folder which are not contains the text 'error'.

So that is how we can grep all files in a folder recursively in Linux Operating System.

Summary - Linux grep recursive Search

In This tutorial we learned, How to grep All Files in a Directory Recursively in Linux Operating System using the grep command.

  • The -R option is use to grep all files in a folder Recursively.
  • When the -l option is used, Only the filename will return in the output.
  • The --exclude-dir options use to exclude directories from the grep recursive search.
  • The -L or --files-without-match option use to return all files do not match the given text pattern.