unlink Command in Linux | Explained

In Linux, the “unlink” is a powerful command utility that enables one to remove a file or link based on its specific name. This command can also remove a symbolic link or the soft link. The unlink command is an alternative to the “rm” command for removing files.

Considering the importance of the unlink command, this post offers various methods to use the unlink Command in Linux. The content provided by this post is:

Let’s start with the guide.

How to Use the unlink Command in Linux?

In a Linux system, one of the main differences between the “rm” and “unlink” commands is that the “unlink” command can remove a single file at a time while the “rm” command can remove multiple files or directories from the system. For deleting files and links, the “unlink” command is utilized as below:

General Syntax

$ unlink linkname/filename

In the above syntax, the unlink command removes the single file or link from the system specifying the name.

You can get a more detailed usage of the unlink command using the command: 

$ unlink --help

Let’s head over to the practical implementation.

Example 1: How to Remove a Particular File in Linux?

An example is considered for removing a particular file with the help of the “unlink” command. For instance, the below script will display the current file in the operating system:

$ ls

The output shows that “file.c” is present in the Home directory, which it wants to remove from the system.

Remove File

The “unlink” command is utilized here for removing the “file.c” from the directory.

$ unlink file.c

The above command has successfully removed the “file.c” from the system.

$ ls

The output shows that the “file.c” file has been successfully removed from the system.

Example 2: How to Remove a Link in Linux?

Another example is followed to remove a soft link (a specific file that acts as a reference) from the system. For this, locate the particular directory on which the particular link is stored. In our case, access the “Office” directory from the “cd” command and present content which is present inside via the “ls” command as below:

$ cd Office
$ ls

A “test.txt” symbolic link that is present in the “Office” directory as seen in the above figure.

Remove Link

To remove the soft link “test.txt” from the specified directory “Office”, execute the below script after accessing the directory:

$ unlink /home/itslinuxfoss/Office/test.txt

After the successful execution of the above command, the “test.txt” file will be removed in the “Office” directory.

Example 3: How to Remove the SoftLink of the Directory?

The “unlink” command assists in deleting the directory link from the system. For instance, the below script will display the soft link of the available directory:

$ ls

The output shows the “Soft_Dir” directory link is present in the “Home” directory, as seen in the above figure.

Remove SoftLink Directory

To remove the soft link of a particular directory “Soft_Dir”, run the below command:

$ unlink Soft_Dir

After the successful execution of the script, you can verify the removed directory link by “ls” command:

$ ls

 The output confirms that the soft directory “Soft_Dir” has been successfully removed.

Conclusion

In Linux, the “unlink” command removes the file and soft link after accessing the required directory. This command matches the existing filename or link name in the directory and deletes it from the operating system. This guide has demonstrated the working usage and functionality of the “unlink” command in Linux.