unlink
Remove a file
TLDR
Remove the specified file if it is the last link
SYNOPSIS
unlink [OPTION]... FILE
PARAMETERS
FILE
The path to the file to be removed. Only a single file can be specified.
--help
Display a help message and exit.
--version
Output version information and exit.
DESCRIPTION
The unlink command is a minimalist utility in Linux, serving as a direct wrapper for the unlink(2) system call. Its primary function is to remove a single specified file from the filesystem. Unlike rm, unlink does not accept any options for force deletion, verbose output, or recursive directory removal. It simply attempts to delete the specified filename.
If the filename is a hard link to an inode, unlink decrements the link count for that inode. When the link count reaches zero and no process has the file open, the file's data blocks are deallocated, and the inode is marked as free. If the filename is the last link to an open file, the file's content persists until all file descriptors referring to it are closed, but the name is removed immediately.
unlink is often preferred in scripts for its directness and predictability, avoiding the potential complexities of rm's various options. It operates silently, providing no output on success.
CAVEATS
unlink removes only a single file; it cannot operate on multiple files at once.
It cannot remove directories. Use rmdir or rm -r for directories.
Does not support options like force (-f), verbose (-v), or recursive (-r).
Fails if the specified file does not exist.
Operates silently on success; provides no confirmation prompt.
The file's name is removed immediately, even if other processes have it open; however, its data blocks are not freed until all file descriptors referencing it are closed.
<B>UNLINK</B> VS. <B>RM</B>
While both unlink and rm remove files, unlink is a simpler, more direct wrapper around the unlink(2) system call. It only takes one argument (the file to remove) and lacks any options like -f (force), -i (interactive), or -r (recursive). rm is a higher-level command providing more user-friendly features, including the ability to delete multiple files, directories, and interactive prompts. For scripting where direct unlink(2) behavior is desired without any embellishments, unlink is often preferred.
HARD LINKS AND INODES
The unlink command specifically removes a directory entry (a hard link) pointing to an inode. If a file has multiple hard links, unlink only removes one of these links and decrements the inode's link count. The file's data and inode are only deleted from the disk when the link count reaches zero and no process has the file open. This behavior is crucial for understanding how files are managed on Unix-like filesystems.
HISTORY
The unlink command has been a fundamental part of Unix-like operating systems since their early days, directly mirroring the unlink(2) system call. Its simplicity and direct mapping to the underlying system call reflect the Unix philosophy of providing small, focused tools. Unlike the more feature-rich rm command, which evolved with various options for user convenience and safety (like prompting or recursive deletion), unlink has remained essentially unchanged, primarily serving as a low-level interface for removing hard links from the filesystem. This makes it particularly suitable for programmatic use in shell scripts where precise control over the unlink(2) system call's behavior is desired without the overhead or additional logic of a more complex utility.