0
0
Linux CLIscripting~20 mins

grep -r for recursive search in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive Grep Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1: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 grep -r banana . when run inside this directory?
A
banana
banana
B
./file1.txt:banana
./file2.txt:banana
C./file1.txt:banana
DNo output
Attempts:
2 left
💡 Hint
Remember that -r searches inside files recursively and shows filename with matching lines.
💻 Command Output
intermediate
1:30remaining
What error does this grep command produce?
What error message will this command produce?

grep -r --include='*.txt' 'apple' /nonexistentdir
Agrep: invalid option -- 'r'
BNo output, command runs silently
Cgrep: missing argument after '--include='
Dgrep: /nonexistentdir: No such file or directory
Attempts:
2 left
💡 Hint
Check what happens when grep tries to search a directory that does not exist.
📝 Syntax
advanced
2: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?
Agrep -r error --include='*.log' .
Bgrep --include='*.log' -r error .
Cgrep -r --include='*.log' error .
Dgrep error -r --include='*.log' .
Attempts:
2 left
💡 Hint
The order of options matters for some commands; check the grep manual for option placement.
🚀 Application
advanced
2: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?
Agrep -r --exclude-dir=vendor TODO .
Bgrep -r --exclude=vendor TODO .
Cgrep -r --exclude-dir='vendor/*' TODO .
Dgrep -r --exclude-dir vendor TODO .
Attempts:
2 left
💡 Hint
Use the option that excludes directories by name, not files.
🧠 Conceptual
expert
2: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?
ABecause grep detects binary files and by default only reports matches without content; use <code>grep -r --binary-files=text</code> to force text output.
BBecause grep cannot search recursively in binary files; use <code>grep -r --text</code> to fix recursion.
CBecause grep treats all files as binary by default; use <code>grep -r --no-binary</code> to disable binary detection.
DBecause grep only searches filenames for binary files; use <code>grep -r --files-with-matches</code> to see lines.
Attempts:
2 left
💡 Hint
Check grep's handling of binary files and the option to treat binary files as text.