1. Overview
The unlink command in Linux is used to remove symbolic links and files. It’s an alternative to the Linux rm command that is used to delete files and directories from a system.
In this tutorial, we’ll look at the Linux unlink command and how we can use it with some examples. Moreover, we’ll also see how it differs from the rm command.
2. unlink Command Syntax
The syntax of the unlink command is:
unlink filename
The syntax of the unlink command is pretty simple. All we need is to type unlink followed by the file name or the symbolic link name. However, if a file or a symlink is placed in some other directory, we’ll need to specify the path.
3. Examples
Let’s see how to use the unlink command with some examples.
3.1. Remove a File From the Current Directory
Using the unlink command, we can remove a file present inside a directory. Let’s say we have a test file in the current directory:
$ ls -l
total 40
drwxr-xr-x 2 ubuntu ubuntu 4096 May 3 22:19 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 7 2023 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 May 30 10:16 Downloads
-rw-rw-r-- 1 ubuntu ubuntu 17 May 30 10:17 test
drwxr-xr-x 3 ubuntu ubuntu 4096 Feb 16 21:27 Videos
Let’s remove this file using the unlink command:
$ unlink test
Now, if we list files under the current directory, we’ll see the file has been removed:
ubuntu@ubuntu:~$ ls -l
total 40
drwxr-xr-x 2 ubuntu ubuntu 4096 May 3 22:19 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 7 2023 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 May 30 10:16 Downloads
drwxr-xr-x 3 ubuntu ubuntu 4096 Feb 16 21:27 Videos
The unlink command does not accept multiple arguments. Therefore, we can’t remove multiple files at once. If we try to use a wildcard pattern to remove multiple files, it’ll give an extra operand error.
We have 3 files test1, test2, and test3 in our directory. Let’s try to remove them using the wildcard:
$ ls -l
total 16
drwxrwxr-x 2 ubuntu ubuntu 4096 May 30 21:49 mydocs
-rw-rw-r-- 1 ubuntu ubuntu 3 May 30 21:52 test1
-rw-rw-r-- 1 ubuntu ubuntu 4 May 30 21:52 test2
-rw-rw-r-- 1 ubuntu ubuntu 8 May 30 21:53 test3
$ unlink test*
unlink: extra operand ‘test2’
Try 'unlink --help' for more information.
As shown above, the output has returned an error.
However, if a wildcard represents a single file, we can use it to remove the file.
Let’s say we have a single file test1 in a directory:
$ ls -l
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 May 30 21:49 mydocs
-rw-rw-r-- 1 ubuntu ubuntu 9 May 30 21:49 test1
Now, let’s try to remove this file using the wildcard pattern:
$ unlink test*
$ ls -l
total 4
drwxrwxr-x 2 ubuntu ubuntu 4096 May 30 21:49 mydocs
As shown above, we’ve successfully deleted the single file using the wildcard pattern.
3.2. Remove a File From Any Location
We can also remove a file or symlink placed in some directory other than the current working directory. To do this, we’ll need to specify the full path to the file:
$ unlink /home/Documents/test.txt
This will remove the file test.txt located under the /home/Documents directory.
3.3. Delete a Symbolic Link
Similar to files, we can also remove symbolic links using the unlink command. Let’s remove a symlink test using the unlink command:
$ ls -l
total 40
drwxr-xr-x 2 ubuntu ubuntu 4096 May 3 22:19 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 7 2023 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 May 30 10:16 Downloads
-rw-rw-r-- 1 ubuntu ubuntu 4 May 30 10:17 test
lrwxrwxrwx 1 ubuntu ubuntu 4 May 30 10:33 testsymlink -> test
$ unlink testsymlink
Now, let’s use the ls command to see the directory listing:
$ ls -l
total 40
drwxr-xr-x 2 ubuntu ubuntu 4096 May 3 22:19 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 7 2023 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 May 30 10:16 Downloads
-rw-rw-r-- 1 ubuntu ubuntu 17 May 30 10:17 test
drwxr-xr-x 3 ubuntu ubuntu 4096 Feb 16 21:27 Videos
Here, we see that it removed the symbolic link testsymlink. Additionally, we noticed that it didn’t remove the file to which the symlink points, which is the test file. The linked file will remain completely unaffected by this action.
4. Limitations of unlink Command
As we’ve discussed above the usage of unlink command. Now, let’s also discuss some limitations of the unlink command in Linux.
4.1. No Confirmation Prompt
When we delete a file or symlink using the rm command, it asks for confirmation. On the other hand, when we delete a file or symlink using the unlink command, it doesn’t ask for any confirmation. It deletes the file right away without asking.
4.2. Cannot Delete a Directory
Another limitation of the unlink command is that it cannot be used to delete a directory; it can only delete files and symbolic links.
4.3. Cannot Remove Multiple Files at Once
We cannot delete multiple files at once using the unlink command in Linux as it only accepts a single argument. Therefore, to remove multiple files, we need to run the command every time.
4.4. Limited Command Options
Unlike other commands with multiple command options to enable additional functionalities, the unlink command has only two command options available –help and –version.
5. unlink vs rm Command
Let’s look at some differences between the unlink and rm commands. While they may seem to perform the same function, they’ve some key differences:
unlink Command
rm Command
Delete symlinks or files
Delete files, symlinks, or directories
Delete files without prompting for confirmation
Delete files with a prompt for confirmation
Only two command options are available
Various command options are available
Only accepts a single argument, so we can only delete one file at a time
Accept multiple arguments, so we can delete multiple files and directories at once
Only accept wildcard if represents a single file
Accepts wildcard patterns to remove multiple files
6. Use Case for unlink Command
Although we mostly use the rm command for file removal, there is one use case where we prefer unlink over rm which is in shell scripting. Let’s say we are writing a shell script and we need to delete a file regardless of its permissions, and we want the command to fail if the file doesn’t exist or if there’s any error. In this case, the unlink command is the perfect solution. Unlike the rm command, which won’t delete unwritable files, and rm -f, which won’t report missing files, the unlink command handles both cases correctly by ignoring permissions and reporting errors.
7. Conclusion
In this article, we explored how to use the unlink command in Linux with some examples. Moreover, we also discussed the limitations of the unlink command and how it differs from the rm command.