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.
Incorrect. 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.
Incorrect. rm -r recursively deletes contents, which is not desired.
rmdir old-cacheCorrect. rmdir old-cache succeeds only when the directory is empty.
Correct. rmdir old-cache succeeds only when the directory is empty.
mkdir -p old-cacheIncorrect. mkdir creates a directory rather than removing it.
Incorrect. 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 / 一時ディレクトリ
- 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.