Is it 100% safe to rename a file while it's being written?

In Ubuntu, unlike in Windows, I can rename a file while it's being written onto the hard disk (for example because it's the output of a program or the copy of another file), and I can also rename a directory containing files that are being written: the system doesn't send messages like "The file is being used, operation not allowed". Is this renaming operation 100% safe, or there's a risk of creating corrupted files? And, in the first case, what is the technical reason that makes it possible, differently from other operating systems? (maybe something related with Linux file system structure and the use of Inodes, I think...?)

Thank you very much.


Solution 1:

Linux works on file handles and renaming or moving a file in Linux does not alter the file handle. Even deleting a file will not be a problem: the process that owns the file handle can still read and write.

The Wikipedia page on inodes will be the best read on this.

In computing, an inode (index node) is a data structure found in many Unix file systems. Each inode stores all the information about a file system object (file, device node, socket, pipe, etc.). It does not store the file's data content and file name except for certain cases in modern file systems.

Implications

A file's inode number stays the same when it is moved to another directory on the same device, or when the disk is defragmented which may change its physical location. This also implies that completely conforming inode behavior is impossible to implement with many non-Unix file systems, such as FAT and its descendants, which don't have a way of storing this invariance when both a file's directory entry and its data are moved around.


So: yes.