brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: use cp --update=none instead of cp -n, which changed behaviour twice

Linux
~/tips/tip-linux-cp-update-none

cp -r --update=none /mnt/user/photos/import/* /mnt/user/photos/library/

Copies or moves files without overwriting existing ones, with an exit status you can predict.

cp -n looks like the obvious way to copy without overwriting anything. I’d stop using it in scripts, because GNU coreutils changed what it does twice in a row.

Up to 9.1, -n quietly skipped files that already existed and cp still exited with 0. Version 9.2 made it exit with a nonzero status when it skipped something, and 9.3 added an error message on top. Then 9.5 went back to the old silent behaviour and marked -n as deprecated in the man page. So the same script can pass on one server and fail on another, depending on the coreutils version. Ubuntu 24.04 sits on 9.4, right in the strict period.

The --update option says what you mean, and it works the same for cp and mv:

cp -r --update=none /mnt/user/photos/import/* /mnt/user/photos/library/

none never replaces a file in the destination and doesn’t treat a skipped file as an error, which is what -n did before 9.2. none-fail also replaces nothing, but reports each skipped file and makes the command fail, which is what you want when a skipped file means something went wrong. Plain -u means --update=older: only replace files that are older than the source.

The catch is that these values are newer than many servers. --update=none exists since 9.3. none-fail came in 9.5, and cp had a bug that rejected it until 9.6, so on Ubuntu 24.04 cp answers with “invalid argument”. Check cp --help | grep update on the machines your script runs on before you rely on it.

Further reading: cp(1) manual page, coreutils NEWS

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link