0
0
Linux CLIscripting~5 mins

grep -r for recursive search in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to find a word or phrase inside many files in a folder and its subfolders. The grep -r command helps you search through all these files quickly without opening each one.
When you want to find a specific word inside all files in a project folder and its subfolders.
When you need to check if a configuration setting appears anywhere in a directory tree.
When you want to locate all files containing an error message in logs spread across multiple folders.
When you are debugging and want to find where a function name is used in your codebase.
When you want to search for a keyword inside all text files in a directory and its nested folders.
Commands
This command searches recursively for the word 'error' inside all files under the /var/log directory and its subdirectories. It helps find all occurrences of 'error' in logs.
Terminal
grep -r 'error' /var/log
Expected OutputExpected
/var/log/syslog:Apr 27 10:00:00 server error: disk full /var/log/auth.log:Apr 27 10:05:00 server error: failed login attempt
-r - Search recursively through all subdirectories
This command searches recursively for the word 'Listen' only inside files ending with .conf in the /etc/apache2 directory. It filters the search to configuration files.
Terminal
grep -r --include='*.conf' 'Listen' /etc/apache2
Expected OutputExpected
/etc/apache2/ports.conf:Listen 80 /etc/apache2/sites-enabled/000-default.conf:Listen 8080
--include='*.conf' - Search only files matching the pattern *.conf
-r - Search recursively
This command searches recursively for the word 'warning' in any case (uppercase or lowercase) inside all files under /home/user/projects. The -i flag makes the search case-insensitive.
Terminal
grep -r -i 'warning' /home/user/projects
Expected OutputExpected
/home/user/projects/log.txt:Warning: low disk space /home/user/projects/readme.md:This is a WARNING message
-r - Search recursively
-i - Ignore case when searching
Key Concept

If you remember nothing else from this pattern, remember: grep -r searches inside all files in a folder and its subfolders to find your keyword quickly.

Common Mistakes
Running grep without -r when expecting to search subfolders
grep without -r only searches files in the current directory, missing files in subfolders.
Always add the -r flag to search recursively through all subdirectories.
Using grep -r without quotes around the search term when it contains spaces
Without quotes, the shell splits the search term into separate words causing errors or wrong matches.
Put the search term in single or double quotes if it has spaces or special characters.
Not using --include or --exclude when you want to limit search to certain file types
grep -r searches all files, which can be slow and produce too many results.
Use --include='pattern' or --exclude='pattern' to focus the search on relevant files.
Summary
Use grep -r to search for a word inside all files in a directory and its subdirectories.
Add --include or --exclude to limit the search to specific file types.
Use -i to ignore case differences in your search term.