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.
find /srv/app -type d -name '*.log'Incorrect. -type d selects directories.
find /srv/app -type f -path /srv/appIncorrect. -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.
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 / 一時ディレクトリ
- Use disposable files and directories for destructive or timestamp-related checks.
- Command details can vary slightly between GNU and non-GNU implementations.
Foundation review
Place the operation
Adjacent find tests are normally combined as an AND, so both the file type and name condition apply.
Check the side effect
Place a same-named directory beside a regular file and verify that only the file is returned.