Challenge - 5 Problems
Recursive Grep Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
What is the output of this recursive grep command?
You have a directory with two files:
file1.txt contains:
apple
banana
cherry
file2.txt contains:
banana
date
fig
What will be the output of the command
file1.txt contains:
apple
banana
cherry
file2.txt contains:
banana
date
fig
What will be the output of the command
grep -r banana . when run inside this directory?Attempts:
2 left
💡 Hint
Remember that
-r searches inside files recursively and shows filename with matching lines.✗ Incorrect
The
-r option makes grep search all files recursively and print matching lines with filenames. Both files contain 'banana', so both lines are shown with filenames.💻 Command Output
intermediate1:30remaining
What error does this grep command produce?
What error message will this command produce?
grep -r --include='*.txt' 'apple' /nonexistentdirAttempts:
2 left
💡 Hint
Check what happens when grep tries to search a directory that does not exist.
✗ Incorrect
The directory /nonexistentdir does not exist, so grep reports 'No such file or directory' error.
📝 Syntax
advanced2:00remaining
Which grep command correctly searches recursively only in .log files?
You want to search for the word 'error' recursively but only inside files ending with .log. Which command does this correctly?
Attempts:
2 left
💡 Hint
The order of options matters for some commands; check the grep manual for option placement.
✗ Incorrect
Option C is the standard and recommended way: options first, then pattern, then path. Others may work but can cause unexpected behavior or errors.
🚀 Application
advanced2:00remaining
How to exclude a directory from recursive grep search?
You want to search recursively for 'TODO' but exclude the directory named 'vendor'. Which command achieves this?
Attempts:
2 left
💡 Hint
Use the option that excludes directories by name, not files.
✗ Incorrect
The option
--exclude-dir=vendor excludes the directory named 'vendor' from the recursive search. Option A excludes files named 'vendor', not directories. Option A uses a wrong pattern. Option A misses the equal sign which is required.🧠 Conceptual
expert2:30remaining
Why does 'grep -r' sometimes show binary file matches and how to avoid it?
When running
grep -r 'pattern' ., sometimes grep shows 'Binary file matches' messages instead of matching lines. Why does this happen and how can you make grep show matching lines only for text files?Attempts:
2 left
💡 Hint
Check grep's handling of binary files and the option to treat binary files as text.
✗ Incorrect
Grep detects binary files and by default shows 'Binary file matches' without printing matching lines. Using
--binary-files=text forces grep to treat binary files as text and print matching lines.