In this article, we will show you how to remove a file in GNU/Linux systems using the unlink command.
Removing File with unlink
unlink is a command-line utility for removing a single file.
The syntax of the unlink command is as follows:
Where filename is the name of the file you want to remove. On success, the command doesn’t produce any output and returns zero.
The 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 rm command, 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 with 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: