Search the contents of regular files below conf.d for lines containing listen.
Include nested subdirectories.
Which command is appropriate?
grep -r recursively traverses the directory and applies the pattern to file contents below it.
Detailed explanation
grep -n 'listen' conf.dIncorrect. -n only adds line numbers and does not request recursive traversal.
Incorrect. -n changes output formatting but does not recurse into a directory.
grep -r 'listen' conf.dCorrect. grep -r searches conf.d and its descendants.
Correct. -r walks the directory tree and applies the pattern to file contents.
find conf.d -name listenIncorrect. find -name tests filenames and does not search file contents.
Incorrect. find -name tests entry names, not the text inside configuration files.
grep 'listen' conf.d/*Incorrect. The shell glob expands only one level and misses deeper directories.
Incorrect. The shell expands only the listed level, so deeper files can be missed.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp -d); mkdir -p "$tmp/sub"; printf 'listen 80\n' >"$tmp/sub/web.conf"; grep -r 'listen' "$tmp"; rm -rf "$tmp"Expected result
一時パス/sub/web.conf:listen 80Key points
- -r means recursive
- grep searches contents
- find -name searches names
Notes
- Environment: GNU grep 3.x / 一時ディレクトリ
- Use temporary files and harmless commands for pattern experiments.
- Regular-expression details should be checked against the target GNU implementation.
Foundation review
Place the condition
Recursive grep handles nested content; a one-level glob or find name test does not provide the same operation.
Check the result
Put the target text in a nested temporary directory and verify the reported path.