0
0
Linux CLIscripting~10 mins

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

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