Unlink Command - Remove Files and Symbolic Links in Linux
The unlink command doesn't get as much attention as some of the other GNU Core Utilities. It has one simple function, to remove a file or symbolic link from a Linux file system. In this article we will discuss how to use the unlink command, and the differences between unlink and the rm command.
Unlink vs rm Command
The unlink command differs in several ways from the rm command. Under the hood it uses the same
unlink() system call. However there are functional differences that users will notice.
- Performs safety checks (confirmation prompts if no write permissions)
- Can provide feedback via STDOUT
- Capable of removing multiple files
- Can remove directories
- Additional options to expand functionality
- No safety checks
- Does not provide additional options
- Cannot remove multiple files
- Does not remove directories
Why Use unlink Instead of rm?
I have been a Linux admin for over two decades and I really never had a need for the unlink command. However, I did some research and the only decent use case I can find was in an old thread, quoted below.
If you try to
rman object to which you don't have write permissions (which are irrelevant to your ability to remove it: the containing directory's permissions are!)
rmnevertheless refuses unless
rmnormally complains if the file doesn't exist, as does
unlink; however with
rmdoes not complain.....
...Suppose you want to just remove a regular file regardless of what its own permissions are. Furthermore, suppose you want the command to fail if the file doesn't exist, or any other reason. Neither- Kaz/Fritz
rm -f filemeets the requirements.
rm filewill refuse if the file isn't writable. But
rm -f filewill neglect to complain if the file is missing.
unlink filedoes the job.
After reading the above, I can see where
unlink would be useful, especially in a shell scripting context.
Using the Unlink Command to Remove a File
The unlink command is used to remove a single file and will not accept multiple arguments. It has no options other than
--version. The syntax is simple, invoke the command and pass a single filename as an argument to remove that file.
[savona@putor ~]$ unlink test.log
You can pass it a wildcard or globbing pattern, but only if that pattern expands to a single file. For example, we can use rm to remove all text files using a wildcard like so:
[savona@putor ~]$ rm *.txt
If we pass a wildcard to unlink, you will receive an extra operand error. This is because the wildcard represents more than a single file. In this example it expands to three files.
[savona@putor ~]$ ls -lrt total 0 -rw-rw-r--. 1 savona savona 0 Mar 12 23:59 test.txt -rw-rw-r--. 1 savona savona 0 Mar 12 23:59 article.txt -rw-rw-r--. 1 savona savona 0 Mar 12 23:59 putorius.txt [savona@putor ~]$ unlink *.txt unlink: extra operand ‘putorius.txt’ Try 'unlink --help' for more information.
If the wildcard represents a single file, all is well.
[savona@putor ~]$ ls -l total 0 -rw-rw-r--. 1 savona savona 0 Mar 12 23:59 test.txt [savona@putor ~]$ unlink *.txt [savona@putor ~]$
NOTE: Unlink cannot be used to remove a directory.
[savona@putor ~]$ unlink dir1 unlink: cannot unlink 'dir1': Is a directory
Using the Unlink Command to Remove a Symbolic Link
You can remove a symbolic link the same way you would remove a file.
[savona@putor ~]$ unlink link.txt
When you use unlink to remove a symbolic link, the linked file is left intact.
[savona@putor ~]$ ls -lrt total 0 -rw-rw-r--. 1 savona savona 0 Mar 13 00:08 test.txt lrwxrwxrwx. 1 savona savona 8 Mar 13 00:08 link.txt -> test.txt [savona@putor ~]$ unlink link.txt [savona@putor ~]$ ls -lrt total 0 -rw-rw-r--. 1 savona savona 0 Mar 13 00:08 test.txt
Unlink is a simple tool for removing a single file. However, for most purposes it's functionality has be replaced with the more popular rm command. Furthermore,
rm is better suited for everyday use because of it's safety checks and robust options like recursion, verbose output and ability to remove directories.
Resources and Links
This site uses Akismet to reduce spam. Learn how your comment data is processed.