Search under /srv/app for regular files named *.log.
Do not include directories with the same name.
Which find command is appropriate?
find -type f selects regular files and -name matches the basename. Quote the pattern so the shell does not expand it first.
Detailed explanation
find /srv/app -name *.logIncorrect. The unquoted pattern may be expanded by the shell before find receives it.
Incorrect. The unquoted pattern may be expanded by the shell before find receives it.
find /srv/app -type d -name '*.log'Incorrect. -type d selects directories.
Incorrect. -type d selects directories.
find /srv/app -type f -path /srv/appIncorrect. -path /srv/app does not test for a .log basename.
Incorrect. -path /srv/app does not test for a .log basename.
find /srv/app -type f -name '*.log'Correct. find /srv/app -type f -name '*.log' applies both conditions.
Correct. find /srv/app -type f -name '*.log' applies both conditions.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp -d); touch "$tmp/a.log" "$tmp/a.txt"; mkdir "$tmp/dir.log"; find "$tmp" -type f -name '*.log' -printf '%f\n'; rm -rf "$tmp"Expected result
a.logKey points
- -type f selects regular files
- -name tests the basename
- Quote globs
Notes
- Environment: GNU findutils 4.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.