Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do the UNIX commands mv and rm work with open files?

If I am reading a file stored on an NTFS filesystem, and I try to move/rename that file while it is still being read, I am prevented from doing so. If I try this on a UNIX filesystem such as EXT3, it succeeds, and the process doing the reading is unaffected. I can even rm the file and reading processes are unaffected. How does this work? Could somebody explain to me why this behaviour is supported under UNIX filesystems but not NTFS? I have a vague feeling it has to do with hard links and inodes, but I would appreciate a good explanation.

like image 681
jl6 Avatar asked Mar 07 '11 12:03

jl6


People also ask

What does mv command do in Unix?

Use the mv command to move files and directories from one directory to another or to rename a file or directory. If you move a file or directory to a new directory without specifying a new name, it retains its original name.

What is the command to open a file in Unix?

In the file manager, double-click any folder to view its contents. Double-click or middle-click any file to open it with the default application for that file. Middle-click a folder to open it in a new tab. You can also right-click a folder to open it in a new tab or new window.

What is the difference between rm and mv?

To move file1 and file2 to a different directory, issue “mv file1 file2 <path_to_new_dir>”. To move directory dir1 with all the files and subdirectories to a different directory, issue “mv dir1 <path_to_new_dir>”. “rm” command is used to remove files and directories.

What does rm command do in Unix?

The rm command is used to delete files. rm -i will ask before deleting each file. Some people will have rm aliased to do this automatically (type "alias" to check).


2 Answers

Unix filesystems use reference counting and a two-layer architecture for finding files.

The filename refers to something called an inode, for information node or index node. The inode stores (a pointer to) the file contents as well as some metadata, such as the file's type (ordinary, directory, device, etc.) and who owns it.

Multiple filenames can refer to the same inode; they are then called hard links. In addition, a file descriptor (fd) refers to an inode. An fd is the type of object a process gets when it opens a file.

A file in a Unix filesystem only disappears when the last reference to it is gone, so when there are no more names (hard links) or fd's referencing it. So, rm does not actually remove a file; it removes a reference to a file.

This filesystem setup may seem confusing and it sometimes poses problems (esp. with NFS), but it has the benefit that locking is not necessary for a lot of applications. Many Unix programs also use the situation to their advantage by opening a temporary file and deleting it immediately after. As soon as they terminate, even if they crash, the temporary file is gone.

like image 145
Fred Foo Avatar answered Sep 22 '22 02:09

Fred Foo


On unix, a filename is simply a link to the actual file(inode). Opening a file also creates a (temporary) link to the actual file. When all links to a file have disappeared (rm and close()) then the file is removed.

On NTFS, logically the filename is the file. There's no indirection layer from the filename to the file metainfo, they're the same object. If you open it, it's in use and can't be removed, just as the actual file(inode) on unix can't be removed while it's in use.

Unix:   Filename ➜ FileInfo ➜ File Data

NTFS: FileName + FileInfo ➜ File Data

like image 34
Erik Avatar answered Sep 22 '22 02:09

Erik