Copy src/report.csv to dest/report.csv.
If the destination exists and is newer than the source, do not overwrite it.
Which GNU cp command is appropriate?
cp -u copies only when the destination is missing or older than the source.
Detailed explanation
cp -f src/report.csv dest/report.csvIncorrect. -f forces overwriting and is the opposite of the requirement.
cp -i src/report.csv dest/report.csvIncorrect. -i prompts but does not compare timestamps automatically.
cp -n src/report.csv dest/report.csvIncorrect. -n skips an existing destination regardless of age.
cp -u src/report.csv dest/report.csvCorrect. cp -u copies only a missing or older destination.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp -d); printf old >"$tmp/src"; sleep 1; printf newer >"$tmp/dst"; cp -u "$tmp/src" "$tmp/dst"; cat "$tmp/dst"; rm -rf "$tmp"Expected result
newerKey points
- -u updates only
- -n skips existing destinations
- -i asks for confirmation
Notes
- Environment: GNU coreutils 9.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
The update option makes a one-way freshness decision; it is not a complete bidirectional synchronization tool.
Check the side effect
Set known timestamps on temporary files and verify which destination content remains.