The “unlink” Command in Linux [ With 3 Practical Examples]

The unlink command is a fundamental utility in Linux, designed to remove a specified file. It operates similarly to the rm command but offers a more straightforward approach for deleting individual files. It allows users to efficiently delete files or symbolic links, especially in scenarios where the rm command might be cumbersome or impractical.

Below, I will illustrate 3 common applications of the unlink command in Linux, demonstrating its flexibility and utility in everyday tasks. I will also discuss some of the benefits and limitations of using this command.

Syntax of “unlink” Command

The basic syntax of the unlink command is:

unlink [file_name]

If you want to go through the version-related details of this command, you can write unlink --version. Or, if you want any help with this command, you can write unlink --help. The unlink command has no other options other than these two.

Note: For more information about the unlink command, run man unlink in your Linux terminal.

Difference Between “unlink” and “rm” Commands

While both commands rm and unlink are used for removing files, rm is more versatile and commonly used for broader file and directory management tasks, including recursive deletions, while unlink is specifically designed for removing individual files without prompting.

Below is a feature-by-feature comparison between these 2 commands:

Feature “rm” Command “unlink” Command
Purpose Removes files or directories. Removes file or symlink.
Default Action Deletes file/files without prompt. Deletes a file without prompt.
Confirmation May prompt for confirmation depending on options or aliases. Typically does not prompt for confirmation.
Behavior Can delete directories recursively with the -r option. Primarily designed for unlinking individual files.
Command Aliases Often used with a set of aliases, such as rm -i, rm -r, etc. No commonly used aliases.
Functionality Can delete multiple files or directories at once. Can not delete multiple files at once.

3 Examples to Delete File Using “unlink” Command in Linux

The unlink command in Linux is a powerful tool used to remove a specific file or symbolic link from the filesystem. Below, I’ll outline some practical applications and examples of using the unlink command:

1. Delete a File from Current Directory

Suppose, you are working inside the Desktop directory and the directory contains a file named “test.txt” which you want to delete using the unlink command. Run the following command inside the current directory to delete that file:

unlink test.txt

Deleting file using unlink commandIn the image above, I have used the ls command to list all the files inside the current directory before and after executing the unlink command. The lists of the files inside the current directory before and after using the unlink command show that the file named “test.txt” has been removed from the current directory.

2. Delete a File from Anywhere Using Actual File Address

To delete a file from anywhere using the unlink command, you need to provide the full path of the file to be deleted while running the command. Suppose, you are currently in your home directory. But you want to delete a file named “test.txt” that resides inside the Desktop directory. You do not need to change your current directory. Instead, you can write the full path address of the file after unlink. For example:

unlink /home/soumik/Desktop/test.txt

Deleting file from original path using unlink commandIn the image above, I have used the ls Desktop command to list all the files inside the Desktop directory. You can see that the file no longer exists there after running the unlink command.

3. Delete a Symbolic Link Using “unlink” Command

The process of deleting a symbolic link is identical to the process of deleting a file. According to the image below, there is a symbolic link named “filelink.txt” inside my current directory (you can also delete the link while staying in a different directory using the actual symlink address).

To delete a symlink run the following command:

unlink filelink.txt

Deleting symlink using unlink commandIn the above image, by running the ls command I called out the item list of the current directory. This command also highlights the symbolic link (in a different color than other files or directories) to differentiate it from other files and directories, which gets removed after using the unlink command.

Some Limitations of Using “unlink” Command in Linux

While unlink is a useful command for deleting individual files in Linux systems, it does have its limitations. Understanding these constraints can help users make informed decisions when utilizing this utility.

Limitation 1: No Confirmation Option Like “rm -i” Command

Unlike the rm -i command, which prompts for confirmation before deleting each file, the unlink command lacks a built-in option for confirmation. This means that when using unlink, there’s no safeguard against unintentional file deletion, making it important for users to exercise caution when employing this command.

Limitation 2: “unlink” Command Cannot Delete a Directory

The unlink command is limited to deleting individual files and cannot remove directories. Unlike the rm command, which can delete directories recursively with the -r option, unlink is specifically designed for unlinking files or links only, making it unsuitable for directory deletion tasks.

In the following image, as you can see I tried to delete a directory named “hello” using unlink. However, the unlink command displayed an error message instead of deleting the directory:

Directories cannot be deleted using unlink command

Limitation 3: “unlink” Command Cannot Delete Multiple Files at Once

One of the limitations of the unlink command is its inability to delete multiple files simultaneously. Unlike the rm command, which allows users to specify multiple files for deletion in a single command, unlink requires separate commands for each file, making bulk file deletion tasks less efficient when using this command.

In the image below, you can see that I typed two file names after the unlink command to delete them simultaneously. But it is not the way this command works. It takes only the first argument as the file name to be deleted and the second file name is considered as an extra operand.

Multiple files cannot be deleted using unlink command

Limitation 4: Data Unrecoverable

A significant limitation of the unlink command is that once a file is unlinked, its data becomes almost unrecoverable. Unlike some file deletion methods that allow for recovery through specialized tools or file system features, unlink immediately removes the file from the file system, making any data contained within it permanently inaccessible. Therefore, users should exercise caution when using unlink, as there is no built-in mechanism for recovering accidentally deleted files.

Why Should You Use “unlink” Instead of “rm” in Linux?

After all the limitations discussed above, you may wonder why should you use the unlink command? Well, the unlink command is handy when you want to delete a file regardless of its permissions. Unlike rm, it won’t complain if the file isn’t writable, and it will fail if the file doesn’t exist or for any other reason. This makes it particularly useful in shell scripting when you need precise file deletion without extra fuss.

Conclusion

In this article, I’ve explored the unlink command in Linux, discussing its functionalities and limitations through practical examples. While unlink offers a straightforward approach to deleting individual files, its inability to delete directories and multiple files simultaneously, along with the permanent and unrecoverable nature of data deletion, are important considerations for users. I encourage readers to further explore unlink and its alternatives to make informed decisions in their file deletion tasks.

People Also Ask

Can I use wildcards with the unlink command?

No, you cannot use wildcards with the unlink command. Unlike the rm command, which supports wildcard expansion for deleting multiple files matching a pattern, unlink is designed to unlink individual files specified explicitly in the command.

What happens when I delete a file using the unlink command?

When you delete a file using the unlink command, the file is removed from the file system immediately. Unlike some other deletion methods that may move the file to a trash or recycle bin, unlink permanently deletes the file, making it unrecoverable. The space previously occupied by the file on the disk is marked as available for reuse by the file system.

Do I need special privileges to use the unlink command?

No, you typically do not need special privileges to use the unlink command. As long as you have read and write permissions for the file you intend to unlink and the directory containing it, you should be able to use unlink without any special privileges. However, if the file or directory you are attempting to unlink requires elevated permissions (e.g., it is owned by another user or is located in a system directory), you may encounter permission-denied errors unless you have the necessary privileges, such as using sudo or being logged in as the root user.

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Avatar photo

Md. Nafis Soumik graduated from the Bangladesh University of Engineering & Technology, Dhaka, with a BSc—Engg in Naval Architecture & Marine Engineering. In January 2023, Soumik joined Softeko as an Excel and VBA content developer. Since then, he has authored over 50 articles, spanning topics from fundamental to advanced Excel concepts such as Data Analysis and Manipulation, Data Visualization, Pivot Tables, Power Query, VBA, and so on. He participated in 2 specialized training programs on VBA and Chart & Dashboard design in Excel. Furthermore, he possesses a keen interest in Linux and currently serves as an executive content developer specializing in Linux. He aims to produce valuable content tailored to Linux users. During his leisure time, he enjoys music, traveling, and science documentaries. Read Full Bio

Leave a Comment