Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between unlink and rm on unix

Whats the real different between these two commands? Why is the system call to delete a file called unlink instead of delete?

like image 687
veeyikpong Avatar asked Jun 01 '14 08:06

veeyikpong


People also ask

Does rm use unlink?

With a single file, rm and unlink do the same task, remove the file. As POSIX defined, rm and unlink both call to unlink() system call. In GNU rm , it calls to unlinkat() system call, which is equivalent to the unlink() or rmdir() function except in the case where path specifies a relative path.

What does unlink () do?

The unlink() function shall remove a link to a file. If path names a symbolic link, unlink() shall remove the symbolic link named by path and shall not affect any file or directory named by the contents of the symbolic link.

What does unlink mean in Linux?

unlink() deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open, the file is deleted and the space it was using is made available for reuse.

What is link unlink in Unix?

In Unix-like operating systems, unlink is a system call and a command line utility to delete files. The program directly interfaces the system call, which removes the file name and (but not on GNU systems) directories like rm and rmdir.


1 Answers

You need to understand a bit about the original Unix file system to understand this very important question.

Unlike other operating systems of its era (late 60s, early 70s) Unix did not store the file name together with the actual directory information (of where the file was stored on the disks.) Instead, Unix created a separate "Inode table" to contain the directory information, and identify the actual file, and then allowed separate text files to be directories of names and inodes. Originally, directory files were meant to be manipulated like all other files as straight text files, using the same tools (cat, cut, sed, etc.) that shell programmers are familiar with to this day.

One important consequence of this architectural decision was that a single file could have more than one name! Each occurrence of the inode in a particular directory file was essentially linking to the inode, and so it was known. To connect a file name to the file's inode (the "actual" file,) you "linked" it, and when you deleted the name from a directory you "unlinked" it.

Of course, unlinking a file name did not automatically mean that you were deleting / removing the file from the disk, because the file might still be known by other names in other directories. The Inode table also includes a link count to keep track of how many names an inode (a file) was known by; linking a name to a file adds one to the link count, and unlinking it removes one. When the link count drops down to zero, then the file is no longer referred to in any directory, presumed to be "unwanted," and only then can it be deleted.

For this reason the "deletion" of a file by name unlinks it - hence the name of the system call - and there is also the very important ln command to create an additional link to a file (really, the file's inode,) and let it be known another way.

Other, newer operating systems and their file systems have to emulate / respect this behavior in order to comply with the Posix standard.

like image 92
Eliyahu Skoczylas Avatar answered Sep 30 '22 16:09

Eliyahu Skoczylas