unlink Command in Linux

Written by: Linuxopsys   |   Last updated: August 27, 2023

The unlink command is used to remove a file and symbolic link from your computer. Basically, it deletes a name from the filesystem and doesn't handle directories.

If the file is a physical file and is not open, then the file is deleted permanently from your computer. If the file is a symbolic link, then the symbolic link is removed, but the file remains intact.

The unlink command can be used to remove only a single file in one command. It does not have any options, other than --help and --version. You need to just specify the command name followed by the file name to remove a file using the unlink command.

Syntax

The syntax of the unlink command is:

unlink file_name

How to use Unlink Command

The unlink command is very easy to use. It does not have any options and the syntax is very simple. The following examples show you how to use the unlink command to remove a given file and symbolic links.

To delete a file type unlink command followed by the filename:

unlink dev.txt
unlink a file

If you use wildcards to delete files using unlink, you get an error "unlink: extra operand".

unlink *.txt
unlink  delete multiple files

The error occurs because unlink can delete only one specified file at a time and in this case there are two text files. If there is only one text file, unlink can remove it using the wildcards:

unlink *.png
unlink using wildcards

When you try to delete a file that does not exist, then unlink returns “unlink: cannot unlink ' ' No such file or directory” error.

Unlink cannot be use used to delete a directory. If you attempt to remove a directory, you get the error message "unlink: cannot unlink ' ': Is a directory".

The unlink command is commonly used to remove symbolic links. This will not delete the target directory or file. The syntax is the same as deleting a single file.

As unlink cannot remove directories, no need to add a trailing slash (/) to the end of the symlink name.

The following command remove a symbolic link named linkfile.txt:

unlink linkfile.txt
unlink command to remove symbolic links

You can verify symbolic links by using ls command or find . -type l command. You may also use the unlink command to delete broken symbolic links.

Linux Unlink vs Rm Command

The unlink and rm commands are both used to delete files in Linux. However, they have the following functional differences:

rm commandunlink command
Can be used to delete multiple files in a single command.Can be used to delete only one file in a single command.
Performs safety checks to ensure if the file is write-protected or notDoes not perform any safety checks
Can delete a directoryCannot delete a directory
Provides multiple options for additional functionalityDoes not provide any additional options
Supports wildcards to remove files based on type and extensionDoes not support wildcards and returns “extra operand” error if wildcards are used.
Note: If there is only one file of the specified extension, then unlink can delete that single file.
Does not return an error when used with the -f option and the file does not existReturns error if the file does not exist
SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment