unlink Command in Linux: Remove a Single File

Sometimes you need to delete exactly one file and nothing else: a stale lock file, a leftover socket, or a symbolic link that points to the wrong place. rm can do that, but it also accepts wildcards, multiple arguments, and recursive options, which leaves room for mistakes. unlink is a smaller tool that removes a single file by deleting its directory entry.
Unlike the more powerful rm
command, unlink accepts only one filename at a time and has no functional options. This guide explains how to use the unlink command with practical examples, how it relates to hard links, and how to fix the errors you are likely to see.
unlink Command Syntax
The syntax of the unlink command is as follows:
unlink filenameWhere filename is the name of the file you want to remove. On success, the command does not produce any output and returns zero.
unlink accepts only two informational options: --help, which displays usage information, and --version, which shows the version. There are no flags for forcing deletion, recursion, or verbose output; all of these are available in rm.
Removing a File
unlink never asks for confirmation, even for write-protected files. Once a file is deleted, recovery is often difficult and unreliable without backups.
By default, rm prompts for an unwritable file when standard input is a terminal and -f is not used. To request confirmation for each deletion, use rm -i.
Before deleting, you can quickly confirm the target exists with ls file.txt.
To remove a file, pass its name or path as the argument:
unlink file.txtTo remove a file in another directory, provide the full or relative path:
unlink /tmp/file.txtTo remove a file, you need write and execute (search) permission on the directory that contains it, plus search permission on every parent directory in its path. You do not need write permission on the file itself. Without the required directory permissions, you will get a “Permission denied” error.
If the directory has the sticky bit set, as /tmp does, you must also own the file or the directory, or have privileges to override this restriction.
Removing a Symbolic Link
When removing symbolic links
with unlink, only the link itself is removed. The file the symlink points to is not affected.
unlink symlink_nameIf the symlink points to a directory, do not add a trailing slash to its name. With the slash, the path resolves to the target directory instead of the link, and the command fails. See the Troubleshooting section below for the exact error.
For more ways to remove symbolic links, see How to Remove Symbolic Links in Linux .
How unlink Works: Hard Links and Link Count
The command gets its name from the unlink() system call it runs. In Linux, a filename is only a link that points to the file data (the inode). A file can have several names, called hard links
, and unlink removes one of them. The data is freed only when the last name is gone and no process has the file open.
To see this in action, create a file and a second hard link to it:
echo "hello" > file.txt
ln file.txt file2.txt
ls -l file.txt file2.txtThe second column of the output shows the link count. Both names point to the same data, so the count is 2:
-rw-rw-r-- 2 linuxize linuxize 6 Oct 7 12:00 file.txt
-rw-rw-r-- 2 linuxize linuxize 6 Oct 7 12:00 file2.txtNow remove the first name:
unlink file.txt
ls -l file2.txt-rw-rw-r-- 1 linuxize linuxize 6 Oct 7 12:00 file2.txtThe link count dropped to 1, and the data is still there under file2.txt. If you run cat file2.txt, it prints hello. The data is released when you unlink file2.txt as well, provided no process still has the file open.
Quick Reference
| Command | Description |
|---|---|
unlink file.txt | Remove a single file |
unlink /path/to/file.txt | Remove a file by full path |
unlink symlink_name | Remove a symbolic link (not its target) |
unlink -- -file.txt | Remove a file whose name starts with a dash |
Troubleshooting
Permission denied
You are missing write or execute (search) permission on the directory containing the file, or search permission on a parent directory in its path. Check the directory permissions and the sticky-bit ownership restrictions described above before using sudo or changing permissions. Example error: unlink: cannot unlink '/opt/file.txt': Permission denied.
No such file or directory
The path does not exist, or you made a typo in the name. Check the name with ls, and remember that filenames in Linux are case-sensitive. Example error: unlink: cannot unlink 'file.txt': No such file or directory.
Is a directoryunlink cannot remove directories on GNU/Linux systems. Use rmdir
for empty directories or rm -r for non-empty ones. Example error: unlink: cannot unlink 'dir1': Is a directory.
Not a directory when removing a symlink
You added a trailing slash to the name of a symlink that points to a directory, which is easy to do when you use Tab completion. Remove the slash and run unlink symlink_name again. Example error: unlink: cannot unlink 'symlink_name/': Not a directory.
Unexpected argument or extra operandunlink accepts only one filename. On Ubuntu 25.10 and 26.04, which ship the Rust-based uutils coreutils, passing a second filename produces error: unexpected argument 'file2.txt' found. GNU coreutils, used by Debian, Fedora, and Ubuntu 24.04, prints unlink: extra operand 'file2.txt'. Use rm
to delete multiple files in one command.
Filename starts with a dash
A name such as -file.txt is read as an option, and the command fails with error: unexpected argument '-f' found on uutils or unlink: invalid option -- 'f' on GNU coreutils. Put -- before the name to mark the end of options: unlink -- -file.txt. Using a path such as ./-file.txt works as well.
FAQ
What happens when I unlink a file that is still open by a process?unlink removes the directory entry immediately. If this was the last hard link, the file’s data remains until all processes that have it open close their file descriptors. If another hard link remains, the data stays on disk even after the last open handle is closed.
Conclusion
The unlink command removes one name for a file and never prompts, which makes it a precise tool for single files and symlinks. For multiple files, directories, or wildcards, use rm
, and for a broader overview of file and directory removal on Linux, see How to Remove Files and Directories Using Linux Command Line
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page