What mv does when the names are on one filesystem
When you run mv on two names inside one filesystem, the command calls the rename system call with the old and new paths. The kernel looks up both directories, removes the entry for the old name, and sets the entry for the new name to refer to the same inode. If the new name already existed, the old inode loses that name, and its link count drops. The data is not copied and not touched. The operation is a change to two directory tables.
The important property is atomicity. From the point of view of any other process, the directory change happens in one step. A process that opens the new name sees the complete old file or the complete new file, and never a mixture. The rename does not create a moment when the name is missing, which would be the case if the old file were deleted first and then written.
The atomic replace pattern
A program that updates a configuration file safely writes the new version to a temporary file in the same directory, flushes it to disk, and then renames it over the old name. Readers always find a complete file. The temporary file must be in the same filesystem, because a rename cannot cross filesystems. That is why editors and package managers often create their temporary files next to the target, not in /tmp.
cd /workspace
echo "version 1" > config.txt
echo "version 2" > config.txt.new
mv config.txt.new config.txt
cat config.txtThe final cat shows version 2. Any reader that opened config.txt before the move keeps reading the old inode, which still exists until its descriptor is closed.
Why the cross-filesystem case is different
An inode belongs to one filesystem, and a directory entry cannot point at an inode on another. So when the source and destination are on different filesystems, mv must copy the data to a new inode on the destination, and then remove the source. The copy is not atomic. Another process may see a partial file, and an interruption leaves a copy that may be incomplete. The source is removed only after the copy finishes, so the data is not lost, but the destination can be in a bad state.
Attributes also change. A cross-filesystem move copies the mode, the owner where permitted, and the timestamps, but the inode number changes, and any hard links to the original are not carried across. These consequences are why the cheap rename is a valuable property, and why tools that rely on it should keep their temporary files on the same filesystem as the target.
Durability is a separate question
Atomicity means the change is all or nothing as seen by other processes. Durability means the change survives a crash. A rename can be atomic and still be lost after a power failure if the directory change has not reached the disk. Careful programs call fsync on the new file before the rename and on the directory after it. Without those calls, the file system may reorder writes in ways that leave the old name pointing at nothing or the new file empty.
Modern filesystems with journaling make many of these cases safer, but they do not remove the need for fsync when durability matters. A rename without a sync is fast and usually correct, and it is not a guarantee against a crash.
Practical lessons
When you write a file that others read, write it under a temporary name in the same directory and rename it into place. When you move files between disks, expect the move to take time and to be interruptible. When you need to know that a rename happened on disk, sync the directory. These habits cost little and prevent a whole class of half-written configuration problems.
You can see the rename system call with strace during a mv inside one directory. The trace shows a single rename with the two paths, and no read or write of the file data.
cd /workspace
touch atomic-demo.txt
strace -e trace=rename -o trace.txt mv atomic-demo.txt atomic-moved.txt
cat trace.txtWhat a crash does to a rename
A crash can happen at any point in the sequence of an atomic replace. If it happens before the rename, the old file is still in place and the temporary file is left behind as debris. A cleanup step at startup can remove such leftovers, using a name pattern that identifies the temporary files. If the crash happens after the rename, the new file is in place and the old inode is freed when the system shuts down or the last descriptor closes.
The pattern therefore gives two clean outcomes and one cleanup case. A program that uses it does not need a journal of its own for the replace step, because the directory change is the commit point. The commit is only as durable as the directory sync, which is why the fsync call on the containing directory appears in careful programs after the rename.
Comparing with editing in place
An editor that writes into the existing inode keeps the name and the hard links, but a crash during the write can leave the file half updated. An editor that writes a new file and renames it keeps every reader on a consistent version, but it breaks hard links to the old inode and changes the inode number. Both designs are common. Know which one your tools use before you depend on links or on inode numbers.