0
0
Linux CLIscripting~15 mins

grep -r for recursive search in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Recursive Search with grep -r
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

The command grep -r "$search_word" project prints matching lines with filenames.