Rename draft.conf to service.conf in the same directory.
Which command is appropriate?
mv changes a directory entry's name when source and destination are in the same directory; the old name disappears.
Detailed explanation
mv draft.conf service.confCorrect. mv draft.conf service.conf renames the file.
cp draft.conf service.confIncorrect. cp leaves the original and creates a copy.
rm draft.conf service.confIncorrect. rm removes files and does not rename them.
mkdir service.confIncorrect. mkdir creates a directory rather than renaming a file.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp -d); touch "$tmp/draft.conf"; mv "$tmp/draft.conf" "$tmp/service.conf"; test -e "$tmp/service.conf" && test ! -e "$tmp/draft.conf" && echo renamed; rm -rf "$tmp"Expected result
renamedKey points
- mv moves and renames
- cp duplicates
- The old name is removed
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
A rename changes the directory entry association, while copying creates a second entry for the same content.
Check the side effect
After a test rename, confirm that the destination exists and the original name does not.