Copy source.conf to backup/source.conf.
If the destination exists, overwrite it only when the source has a newer modification time.
Which GNU cp command is correct?
cp -u copies when the destination is missing or the source is newer. It is useful for incremental updates.
Detailed explanation
cp -f source.conf backup/source.confIncorrect. -f forces overwriting instead of comparing timestamps.
Incorrect. -f forces overwriting instead of comparing timestamps.
cp -n source.conf backup/source.confIncorrect. -n skips an existing destination even when the source is newer.
Incorrect. -n skips an existing destination even when the source is newer.
cp -u source.conf backup/source.confCorrect. -u updates the destination only when the source is newer or the destination is absent.
Correct. -u updates the destination only when the source is newer or the destination is absent.
cp -i source.conf backup/source.confIncorrect. -i requests an interactive decision but does not implement the timestamp rule.
Incorrect. -i requests an interactive decision but does not implement the timestamp rule.
Try it yourself
An example you can run in a temporary verification environment.
LAB_DIR=$(mktemp -d)
mkdir "$LAB_DIR/backup"
printf old > "$LAB_DIR/backup/source.conf"
sleep 1
printf new > "$LAB_DIR/source.conf"
cp -u "$LAB_DIR/source.conf" "$LAB_DIR/backup/source.conf"
cat "$LAB_DIR/backup/source.conf"
rm -r "$LAB_DIR"Expected result
newKey points
- -u compares modification times
- -n only prevents overwrites when a destination exists
- -i asks for confirmation
Notes
- Environment: GNU coreutils 9.x
- 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.