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.

rm Command:

  • 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

unlink Command:

  • 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 rm an object to which you don’t have write permissions (which are irrelevant to your ability to remove it: the containing directory’s permissions are!) rm nevertheless refuses unless -f is specified. rm normally complains if the file doesn’t exist, as does unlink; however with -frm does 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 rm file nor rm -f file meets the requirements. rm file will refuse if the file isn’t writable. But rm -f file will neglect to complain if the file is missing. unlink file does the job.

– Kaz/Fritz

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 --help and --version. The syntax is simple, invoke the command and pass a single filename as an argument to remove that file.

[[email protected] ~]$ 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:

[[email protected] ~]$ 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.

[[email protected] ~]$ 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
[[email protected] ~]$ unlink *.txt
unlink: extra operand ‘putorius.txt’
Try 'unlink --help' for more information.

If the wildcard represents a single file, all is well.

[[email protected] ~]$ ls -l
total 0
-rw-rw-r--. 1 savona savona 0 Mar 12 23:59 test.txt
[[email protected] ~]$ unlink *.txt
[[email protected] ~]$ 

NOTE: Unlink cannot be used to remove a directory.

[[email protected] ~]$ 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.

[[email protected] ~]$ unlink link.txt

When you use unlink to remove a symbolic link, the linked file is left intact.

[[email protected] ~]$ 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
[[email protected] ~]$ unlink link.txt 
[[email protected] ~]$ 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