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 only adds line numbers and does not request recursive traversal.
grep -r 'listen' conf.dCorrect. grep -r searches conf.d and its descendants.
Correct. grep -r searches conf.d and its descendants.
find conf.d -name listenIncorrect. find -name tests filenames and does not search file contents.
Incorrect. find -name tests filenames and does not search file contents.
grep 'listen' conf.d/*Incorrect. The shell glob expands only one level and misses deeper directories.
Incorrect. The shell glob expands only one level and misses deeper directories.
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 / 一時ディレクトリ
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.