Bird
Raised Fist0
Linux CLIscripting~10 mins

grep -r for recursive search in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - grep -r for recursive search
Start grep command with -r
↓
Read directory
↓
For each item in directory
↓
Is directory?
↓
Print matching lines
↓
Next item
↓
End when all items searched
The grep -r command starts at a directory, reads each item, recurses into subdirectories, and searches files for the pattern, printing matches.
Execution Sample
Linux CLI
grep -r 'hello' ./myfolder
Searches recursively in 'myfolder' for the word 'hello' and prints matching lines with filenames.
Execution Table
StepDirectory/FileIs Directory?ActionOutput
1./myfolderYesRead contents: file1.txt, subdir
2file1.txtNoSearch for 'hello'file1.txt: hello world
3subdirYesRecurse into subdir
4subdir/file2.txtNoSearch for 'hello'subdir/file2.txt: say hello again
5subdir/file3.txtNoSearch for 'hello'
6All items processedExitNo more files to search
💡 All files and subdirectories have been searched recursively.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Current Itemgrep -r command start./myfolderfile1.txtsubdirsubdir/file2.txtsubdir/file3.txtNone
Output Linesemptyemptyfile1.txt: hello worldfile1.txt: hello worldfile1.txt: hello world subdir/file2.txt: say hello againfile1.txt: hello world subdir/file2.txt: say hello againfinal output printed
Key Moments - 3 Insights
Why does grep -r search inside subdirectories automatically?
Because the -r option tells grep to treat directories by going inside them and searching all files recursively, as shown in steps 3 and 4 of the execution_table.
What happens if a file does not contain the search pattern?
No output is printed for that file, as seen in step 5 where subdir/file3.txt has no matching lines, so output is empty.
Does grep -r print the filename with the matching line?
Yes, grep -r prints the filename before the matching line to show where the match was found, as shown in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 2?
Afile1.txt: hello world
Bsubdir/file2.txt: say hello again
CNo output yet
Dsubdir/file3.txt: no match
💡 Hint
Check the Output column at step 2 in the execution_table.
At which step does grep enter the subdirectory to continue searching?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the Action column where recursion into subdir happens.
If the file subdir/file3.txt contained 'hello', how would the output change at step 5?
ANo change, output stays empty
BOutput would include 'subdir/file3.txt: hello'
COutput would remove previous matches
Dgrep would stop searching
💡 Hint
Refer to how output accumulates in variable_tracker and execution_table outputs.
Concept Snapshot
grep -r PATTERN DIRECTORY

- Searches recursively inside DIRECTORY and all subdirectories
- Prints matching lines with filenames
- Automatically descends into directories
- Stops when all files are searched
- Useful to find text in many files quickly
Full Transcript
The grep -r command starts by reading the given directory. It checks each item inside. If the item is a file, it searches for the pattern and prints matching lines with the filename. If the item is a directory, grep -r goes inside and repeats the process. This continues until all files and subdirectories are searched. If a file has no matches, nothing is printed for it. The output shows all matches found with their file paths. This recursive search helps find text across many files easily.

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