cp vs mv: which to use when replacing a file
cp overwrites by default and writes into the same inode. mv swaps in a new one. See what that means for hardlinks and single-file Docker bind mounts.
Does cp overwrite an existing file?
Yes. When you replace a file with cp, it overwrites an existing target without asking, unless something adds -i, such as a shell alias. The GNU manual describes replacing every existing destination file as the default when you do not pass --update. Most answers stop there. They skip the part that matters on a server: cp and mv replace a file in two different ways, and that decides what other programs see during and after the swap.
cp writes the new bytes into the file that is already there. mv puts a different file in its place. This guide shows the difference with stat, so you can watch it on your own machine instead of trusting a summary.
First, check whether your shell adds anything to cp:
type cpIf type reports an alias that includes -i, cp will prompt in this shell. If it reports a plain path to the binary, nothing is added, and cp overwrites silently.
Which cp and mv does your server run?
Some flags changed behavior between releases, so print the version before you test anything.
cp --version | head -n 1
mv --version | head -n 1Ubuntu 24.04 ships GNU coreutils 9.4. Ubuntu 26.04 LTS, released on 23 April 2026, made rust-coreutils (the Rust rewrite from the uutils project) its default core utilities. The release notes make one exception that matters here: "because of unresolved bugs", cp, mv and rm are still the GNU programs. The GNU coreutils package in 26.04 is version 9.7. So on both releases, every command in this guide is GNU cp and GNU mv. The Rust rewrites in Ubuntu's base system covers which tools did change. If your cp --version line names a different implementation, your results for the flags section may differ, so trust what your terminal prints.
File names point at inodes
A file name is a directory entry. It points at an inode, and the inode holds the data, the owner and the permissions. Two names that point at the same inode are hard links: change the data through one name and the other name sees it. If this is new to you, read what an inode is and why it matters first.
stat -c %i prints the inode number of a file. stat -c %h prints how many names point at it. Those two numbers are all you need to see the difference between cp and mv.
Set up a test directory with one file and one hard link to it:
mkdir -p ~/replace-test && cd ~/replace-test
printf 'version 1\n' > app.conf
ln app.conf app.conf.hardlink
stat -c '%i %h %n' app.conf app.conf.hardlinkBoth lines should show the same inode number and a link count of 2. That is one file with two names.
What cp does to the existing file
printf 'version 2\n' > new.conf
stat -c '%i' app.conf
cp new.conf app.conf
stat -c '%i' app.conf
cat app.conf.hardlinkThe two inode numbers match, and the hard link now prints version 2. This happens because cp opens the existing destination for writing. The GNU manual states it from the other side: without -f, if an existing destination cannot be opened for writing, the copy fails. cp does not make a new file. It reuses the one it finds.
When a program opens an existing regular file for writing with truncation, the kernel cuts the file to length 0 first (the open(2) man page describes this). Then cp writes the new data. Between those two moments the file is empty or partly written. Any program that reads the file in that window, such as a web server serving it or a service reloading its config, can read a half-written file.
The inode also keeps its metadata, so the owner and mode of app.conf stay what they were. Check with stat -c '%a %U %G' app.conf.
What mv does instead
printf 'version 3\n' > app.conf.tmp
stat -c '%i %n' app.conf.tmp app.conf
mv app.conf.tmp app.conf
stat -c '%i %h %n' app.conf app.conf.hardlink
cat app.conf app.conf.hardlinkNow app.conf shows the inode number that app.conf.tmp had a moment ago. The hard link shows the old inode, with a link count of 1, and it still prints version 2. The GNU manual says that to move a file, mv ordinarily just renames it. The rename(2) man page says what that rename does to an existing target: it is "atomically replaced, so that there is no point at which another process attempting to access newpath will find it missing." A reader opening app.conf gets the old file or the new file, never half of one.
The old inode does not vanish while something still holds it open. The unlink(2) man page says a file whose last name is gone stays in existence "until the last file descriptor referring to it is closed." You can watch a program keep the old content after the swap:
exec 3< app.conf
printf 'version 4\n' > app.conf.tmp
mv app.conf.tmp app.conf
cat <&3
cat app.conf
exec 3<&-The first cat reads through descriptor 3, which was opened before the swap, so it prints version 3. The second cat opens the name again and prints version 4. This is also why a long-running process can keep disk space busy after you replace or delete its log file, which explains why df and du disagree on a full disk.
The new file keeps its own owner and mode, because it is a different inode. Whatever mode the temporary file had is the mode the live file now has. Check with stat -c '%a %U %G' app.conf.
Why the swap is only atomic on one filesystem
A rename only works inside one filesystem. The GNU manual says that when the destination is on a different filesystem, mv falls back to copying "as if by cp -a" and then removes the original. That is a copy followed by a delete, so the half-written window from the cp section comes back. The rename(2) man page adds a stricter rule: rename does not work across two different mount points, even when the same filesystem is mounted at both.
The practical rule is simple. Create the temporary file in the same directory as the file you replace. Then both names are on the same mount, and mv can rename.
Does sed -i change the inode?
The sed man page describes -i only as "edit files in place". It does not say whether the result is the same inode or a new one. So measure it:
stat -c '%i' app.conf
sed -i 's/version 4/version 5/' app.conf
stat -c '%i' app.confIf the two numbers are different, sed -i replaced the file the way mv does, which means hard links and open handles keep the old content. If they are the same, it wrote into the existing file the way cp does. Write down what your server printed, because the next section depends on it. If the target of sed -i is a symbolic link, read about the --follow-symlinks option in man sed before you run it, and see how symlinks differ from hard links.
Why a single-file Docker bind mount keeps serving the old file
A single-file bind mount looks like -v "$PWD/nginx.conf:/etc/nginx/nginx.conf". The mount(2) man page describes a bind mount as making "a file or a directory subtree visible at another point". Docker sets the mount up when the container starts, from the file the host path names at that moment.
A rename-style replace does not change that file. It points the host name at a new inode. So the host shows the new content while the container can keep showing the old file. Docker's bind mount documentation does not describe this case, so test it on your own Docker version:
printf 'one\n' > mounted.txt
docker run -d --name bindtest -v "$PWD/mounted.txt:/data/mounted.txt:ro" alpine:3.21 sleep 600
printf 'two\n' > two.txt
cp two.txt mounted.txt
docker exec bindtest cat /data/mounted.txt
printf 'three\n' > mounted.tmp
mv mounted.tmp mounted.txt
docker exec bindtest cat /data/mounted.txt
cat mounted.txt
docker rm -f bindtestThe first docker exec runs after a cp, which writes into the mounted inode. The second runs after a mv, which swaps the host name to a new inode. Compare the second output with the final cat on the host. If they differ, the container is still reading the old inode. Editors that save by writing a new file and renaming it trigger the same problem, and so does sed -i if your measurement above showed a new inode.
Two fixes work. Mount the directory instead of the single file, so the container looks up the name on each open. Or recreate the container after each change, for example with docker compose up -d --force-recreate. One more trap from Docker's own documentation: with -v, a host path that does not exist is created as a directory, so a typo in a single-file mount gives you an empty directory where the file should be. Bind mounts versus named volumes in Docker Compose covers the directory approach.
The cp flags that change overwrite behavior
-i prompts before overwriting an existing file, and the copy fails if the answer is not yes:
cp -i new.conf app.conf-n skips an existing destination silently. The current GNU manual marks it as deprecated "due to having a different exit status from other platforms". -i overrides an earlier -n, and -n overrides an earlier -i. Run it and read the exit status yourself:
printf 'keep me\n' > existing.txt
printf 'replace\n' > incoming.txt
cp -n incoming.txt existing.txt; echo "exit status: $?"
cat existing.txtThe exit status of cp -n changed more than once upstream. The coreutils changelog says version 9.2 (March 2023) made it nonzero when a file was skipped, 9.3 added an error message, and 9.5 (March 2024) went back to silent skipping with no failure status. The version number alone does not settle what your server does, because distributions patch their packages. On Ubuntu 24.04, whose cp --version reports 9.4, our run kept existing.txt unchanged, printed cp: warning: behavior of -n is non-portable and may change in future; use --update=none instead, and then printed exit status: 0. Run the same commands on 26.04 and compare. A script that tests $? after cp -n depends on how each package was built, which is reason enough to avoid it.
For scripts, use --update with an explicit value instead:
--update=noneskips existing files and does not fail. It arrived in 9.3, so both releases have it.--update=none-failskips existing files, prints a message for each one and fails. It arrived in 9.5, and a bug made 9.5 reject it until 9.6 fixed it. Ubuntu 24.04's 9.4 does not have it.--update=older, or--updatewith no value, replaces a file only when the destination is older than the source.-fonly matters when the destination cannot be opened for writing. Thencpremoves it and tries again. With-n,-fis ignored.
--remove-destination removes each existing destination before opening it, so the result is a new inode. It is not an atomic swap: between the removal and the new file, the name does not exist, and a reader in that moment gets "No such file or directory".
--backup=numbered makes a backup of each file that would otherwise be overwritten. Run it twice and list the directory to see the backup names your version uses:
cp --backup=numbered new.conf app.conf
cp --backup=numbered new.conf app.conf
ls app.conf*Simple, non-numbered backups use the suffix ~ unless you set --suffix.
mv takes the same -i, -f, -n and --update options. When you give more than one of -i, -f and -n, only the last one counts. mv -n carries the same deprecation note as cp -n.
The safe way to replace a file on a live server
Write the new content to a temporary file in the same directory, set its mode, then rename it over the original:
cd ~/replace-test
tmp=$(mktemp ./app.conf.XXXXXX)
printf 'version 6\n' > "$tmp"
chmod 644 "$tmp"
mv "$tmp" app.conf
stat -c '%i %a %n' app.confThe chmod step is required, because after the rename the live file is the temporary file, with its mode and owner. If a service reads the file as another user, set the owner too with chown. Numeric and symbolic chmod modes explains the 644. This pattern stops readers from seeing a half-written file. It does not promise that the data reached the disk before a power cut, which is a separate problem that this guide does not cover.
install can set the mode in the same step: install -m 644 new.conf app.conf. Its man page does not say whether it writes into the existing file or creates a new one, so check with stat -c %i before and after on your server. If you need the atomic swap, use install to create a temporary name, then rename:
install -m 644 new.conf ./app.conf.new
mv app.conf.new app.confSo which one should you use?
- Use
mvfrom a temporary file in the same directory when a running program reads the file. Readers see the old file or the new one, never a partial one. - Use
cpwhen other names must see the change: a hard link, or a single-file bind mount you cannot recreate. Accept the short window where the file is incomplete. - Use
--update=noneinstead of-nin scripts that must not overwrite, so the exit status does not depend on the coreutils version. - Check with
stat -c %iwhenever you are not sure which kind of replace a tool does.
FAQ
Does cp overwrite a file without asking?
Yes. GNU cp replaces an existing destination by default. It only asks if you pass -i, or if your shell has an alias that adds -i. Run type cp to see whether an alias is active. To skip existing files instead, use cp --update=none.
Why does cp -n give a different exit status on different servers?
The behavior changed across GNU coreutils releases. Upstream, version 9.2 made a skipped file return a nonzero status, and 9.5 reverted to silent skipping with no failure. Distributions can also patch it: on Ubuntu 24.04, whose package reports 9.4, our run of cp -n printed a deprecation warning and exited 0. Run cp --version and echo $? on each server, and use --update=none in scripts.
Why does my Docker container still see the old config after I edited the file?
A single-file bind mount is set up from the file the host path named when the container started. If your editor, mv or another tool replaced the file by writing a new one and renaming it, the host path now names a new inode, and the container can keep reading the old one. Compare stat -c %i before and after your edit. Mount the parent directory instead, or recreate the container after each change.
Is mv atomic when it replaces a file?
Within one filesystem, yes. mv renames the file, and rename(2) replaces the target so that no other process ever finds the name missing. Across filesystems or mount points, mv copies the file and then deletes the original, which is not atomic. Keep the temporary file in the same directory as the target.