Unlink Command in Linux
How do you delete files in the Linux terminal? You use the rm command.
That's what people usually do. But another command can be used for deleting files and links in Linux.
The command is called unlink
and though it may sound like it is for deleting links only in Linux, it can also delete files.
After all, the delete process in Linux is basically unlinking. When you delete a file, the Linux kernel finds that it corresponds to inode number X. It will remove the file entry from its directory's listing and reduce the inode X’s link count by 1. Now that inode X’s link count is 0, the kernel knows that there is no one linking to this inode, so it is safe to remove the inode and delete the data block associated with it.
Let's see how to use the unlink command.
Unlink command
The unlink command is used for removing files and links. You can use it to delete both soft and hard links.
It is one of the simplest commands that have no options other than --help
and --version
.
To delete a file with unlink, use it like this:
unlink filename
To delete a link:
unlink link_name
You won't see any output when successfully deleting a file or link.
Basically, that's about it for the unlink command. It cannot do anything else.
You cannot delete multiple files or links at once
While the rm command can delete multiple files at once, unlink can only remove one file at a time. You cannot give more than one files to it at once. You cannot use globbing patterns as well.
If you try something like that, it throws an error and doesn't delete any file.
abhishek@LHB:~/test$ unlink *.txt
unlink: extra operand ‘file2.txt’
Try 'unlink --help' for more information.
The same goes for deleting multiple links at once.
You cannot delete directories with unlink
The GNU implementation of unlink command cannot delete a directory. It throws an error if you try removing a folder.
abhishek@LHB:~$ unlink new_dir
unlink: cannot unlink 'new_dir': Is a directory
Difference between rm and unlink commands
The unlink command uses unlink system command while rm command uses unlinkat system call. Both system calls are almost the same.
There are a few obvious differences between the two commands and you can see that already.
The unlink command only handles one file or link at a time. The rm command can handle multiple at a time.
The rm command can delete directories with the recursive option. The unlink command cannot delete a directory.
The rm command performs a security check. If you don't have write permission on a file, it will ask you to confirm it interactively or use the force option -f
. With unlink, there is no safety checks. It will delete the write-protected file.
Unlink doesn't have additional functionalities or options, unlike the rm command.
There could be a few use cases where you would prefer using the unlink command over rm. Imagine that you want to 'force remove' a file irrespective of the write protection, but you want the script to fail if the file doesn't exist. The rm -f won't show any errors if the file doesn't exist but unlink will throw an error.