Bird
Raised Fist0
Linux CLIscripting~5 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the command grep -r 'error' /var/log do?
easy
A. Searches recursively for the word 'error' inside all files in /var/log and its subfolders.
B. Searches only the /var/log folder for files named 'error'.
C. Deletes all files containing the word 'error' in /var/log.
D. Lists all files in /var/log that have the word 'error' in their name.

Solution

  1. Step 1: Understand the grep -r option

    The -r flag tells grep to search recursively inside all files in the given directory and its subdirectories.
  2. Step 2: Analyze the command arguments

    The command searches for the text 'error' inside all files under the /var/log directory and its subfolders.
  3. Final Answer:

    Searches recursively for the word 'error' inside all files in /var/log and its subfolders. -> Option A
  4. Quick Check:

    grep -r means recursive search [OK]
Hint: Remember: -r means search inside folders and subfolders [OK]
Common Mistakes:
  • Thinking -r searches only filenames, not file contents
  • Confusing grep with delete or list commands
  • Ignoring the folder path argument
2. Which of the following is the correct syntax to recursively search for the word 'config' in the current directory?
easy
A. grep . -r config
B. grep config -r .
C. grep -r . config
D. grep -r config .

Solution

  1. Step 1: Recall grep syntax

    The correct syntax is grep [options] pattern path. The -r option comes before the pattern.
  2. Step 2: Match the correct order

    grep -r config . uses grep -r config . which means search recursively for 'config' in current directory ('.').
  3. Final Answer:

    grep -r config . -> Option D
  4. Quick Check:

    Options order: grep -r pattern path [OK]
Hint: Options come before pattern and path in grep command [OK]
Common Mistakes:
  • Placing -r after the pattern or path
  • Swapping pattern and path arguments
  • Using dot (.) in wrong position
3. Given this folder structure and files:
./docs/readme.txt (contains 'install')
./src/app.py (contains 'install')
./src/utils.py (no 'install')

What is the output of grep -r 'install' ./?
medium
A. ./docs/readme.txt:install ./src/app.py:install
B. ./docs/readme.txt:install
C. ./src/utils.py:install
D. No output

Solution

  1. Step 1: Understand recursive search

    The command searches all files under current folder (./) and subfolders for the word 'install'.
  2. Step 2: Identify matching files

    Both readme.txt and app.py contain 'install', so their lines are printed with filenames.
  3. Final Answer:

    ./docs/readme.txt:install ./src/app.py:install -> Option A
  4. Quick Check:

    grep -r finds all matching lines with filenames [OK]
Hint: grep -r shows filename:line for all matches [OK]
Common Mistakes:
  • Expecting only filenames without matching lines
  • Assuming files without the word appear in output
  • Ignoring recursive search in subfolders
4. You run grep 'TODO' /project but get an error: grep: /project: Is a directory. What is the likely cause?
medium
A. You forgot to add the -r option for recursive search.
B. You used the wrong quotes around TODO.
C. You used grep without the -r option but gave a directory path.
D. You ran grep without specifying the directory path.

Solution

  1. Step 1: Analyze the error message

    The error says grep treats /project as a file, but it is a directory.
  2. Step 2: Understand grep behavior

    Without -r, grep cannot search inside directories and errors out when given a directory path.
  3. Final Answer:

    You used grep without the -r option but gave a directory path. -> Option C
  4. Quick Check:

    grep needs -r to search directories [OK]
Hint: Use -r to avoid directory errors when searching folders [OK]
Common Mistakes:
  • Assuming quotes cause the error
  • Thinking -r was forgotten when it was used
  • Not specifying directory path at all
5. You want to find all files under /home/user/projects containing the word 'fixme' but exclude any files in node_modules subfolders. Which command achieves this?
hard
A. grep -r 'fixme' /home/user/projects | grep -v node_modules
B. grep -r --exclude-dir=node_modules 'fixme' /home/user/projects
C. grep -r --exclude='node_modules' 'fixme' /home/user/projects
D. grep -r --exclude-dir='fixme' /home/user/projects

Solution

  1. Step 1: Understand the requirement

    You want recursive search for 'fixme' but skip directories named 'node_modules'.
  2. Step 2: Use correct grep option to exclude directories

    The --exclude-dir=node_modules option tells grep to skip those folders during recursive search.
  3. Step 3: Check other options

    grep -r 'fixme' /home/user/projects | grep -v node_modules filters output but still searches inside node_modules, which is inefficient. grep -r --exclude='node_modules' 'fixme' /home/user/projects excludes files named 'node_modules' not directories. grep -r --exclude-dir='fixme' /home/user/projects excludes directories named 'fixme' which is wrong.
  4. Final Answer:

    grep -r --exclude-dir=node_modules 'fixme' /home/user/projects -> Option B
  5. Quick Check:

    Use --exclude-dir to skip folders in recursive grep [OK]
Hint: Use --exclude-dir to skip folders during recursive grep [OK]
Common Mistakes:
  • Using --exclude instead of --exclude-dir for directories
  • Filtering output instead of excluding directories
  • Wrong option names or misplaced quotes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard