📖 Scenario: You are working on a project folder with many text files and subfolders. You want to find all files that contain a specific word inside this folder and its subfolders.
🎯 Goal: Learn how to use the grep -r command to search recursively for a word inside all files in a folder and its subfolders.
📋 What You'll Learn
Use the grep -r command to search recursively
Search for the exact word TODO
Search inside the folder named project
Display the matching lines with filenames
💡 Why This Matters
🌍 Real World
Searching for keywords like TODO, FIXME, or error messages inside large code or document folders.
💼 Career
Helps in debugging, code review, and managing large projects by quickly finding relevant information.
Progress0 / 4 steps
1
Create a folder named project with some text files
Create a folder called project and inside it create two text files named file1.txt and file2.txt. Put the exact text TODO: fix bug inside file1.txt and Done inside file2.txt.
Linux CLI
Hint
Use mkdir to create the folder and echo with redirection > to create files with content.
2
Add a variable for the search word
Create a variable called search_word and set it to the exact value TODO.
Linux CLI
Hint
Use search_word=TODO without spaces around =.
3
Use grep -r to search recursively for the word
Use the command grep -r "$search_word" project to search recursively inside the project folder for the word stored in search_word.
Linux CLI
Hint
Use double quotes around $search_word to expand the variable.
4
Display the search results
Run the script so it prints the lines containing TODO with the filename. The output should show project/file1.txt:TODO: fix bug.
Linux CLI
Hint
The command grep -r "$search_word" project prints matching lines with filenames.
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
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.
Step 2: Analyze the command arguments
The command searches for the text 'error' inside all files under the /var/log directory and its subfolders.
Final Answer:
Searches recursively for the word 'error' inside all files in /var/log and its subfolders. -> Option A
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
Step 1: Recall grep syntax
The correct syntax is grep [options] pattern path. The -r option comes before the pattern.
Step 2: Match the correct order
grep -r config . uses grep -r config . which means search recursively for 'config' in current directory ('.').
Final Answer:
grep -r config . -> Option D
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
Step 1: Understand recursive search
The command searches all files under current folder (./) and subfolders for the word 'install'.
Step 2: Identify matching files
Both readme.txt and app.py contain 'install', so their lines are printed with filenames.
Final Answer:
./docs/readme.txt:install
./src/app.py:install -> Option A
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
Step 1: Analyze the error message
The error says grep treats /project as a file, but it is a directory.
Step 2: Understand grep behavior
Without -r, grep cannot search inside directories and errors out when given a directory path.
Final Answer:
You used grep without the -r option but gave a directory path. -> Option C
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
Step 1: Understand the requirement
You want recursive search for 'fixme' but skip directories named 'node_modules'.
Step 2: Use correct grep option to exclude directories
The --exclude-dir=node_modules option tells grep to skip those folders during recursive search.
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.
Final Answer:
grep -r --exclude-dir=node_modules 'fixme' /home/user/projects -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.