Unlink Command in Linux (Remove File)
2 min read
In this article, we will show you how to remove a file in GNU/Linux systems using the
Removing File with
unlink is a command-line utility for removing a single file.
The syntax of the
unlink command is as follows:
filename is the name of the file you want to remove. On success, the command doesn’t produce any output and returns zero.
unlink command accepts only two options,
--help which displays the command help and
--version which shows the version information.
Be extra cautious when removing files using the
unlink command, because once the file is deleted, it cannot be fully recovered.
Unlike the more powerful
unlink can accept only a single argument which means that you can delete only one file. If you try to remove more than one file, you will get “unlink: extra operand” error.
When removing symbolic links
unlink, the file the symlink points to is not removed.
To remove a given file, you need to have writing permissions on the directory containing that file. Otherwise, you will get “Operation not permitted” error.
For example, if you try to remove the file
file3.txt under the
/opt directory which is owned by root:
The system will print the following message:
unlink: cannot unlink '/opt/file2.txt': Permission denied
On GNU/Linux systems
unlink can never delete a directory
. If you try to remove a directory:
You will get the following message:
unlink: cannot unlink 'dir1': Is a directory
Removing files with
unlink is a simple process, but you must be careful not to delete relevant data.