Remove old-cache only if it is empty.
If it contains files, leave it in place and report an error.
Which command is appropriate?
rmdir removes only empty directories, making it safer when recursive deletion is not intended.
Detailed explanation
rm -f old-cacheIncorrect. rm -f does not remove a directory in this way and would not express the empty-only condition.
rm -r old-cacheIncorrect. rm -r recursively deletes contents, which is not desired.
rmdir old-cacheCorrect. rmdir old-cache succeeds only when the directory is empty.
mkdir -p old-cacheIncorrect. mkdir creates a directory rather than removing it.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp -d); mkdir "$tmp/empty"; rmdir "$tmp/empty"; test ! -e "$tmp/empty" && echo removed; rmdir "$tmp"Expected result
removedKey points
- rmdir requires empty
- rm -r is recursive
- The safety condition is explicit
Notes
- Environment: POSIX shell / 一時ディレクトリ
- 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
An empty-only command expresses the requirement directly; recursive removal has a much wider effect.
Check the side effect
Try an empty and a non-empty temporary directory and compare the outcomes.