The unlink command in Linux removes a single file by deleting its directory entry. It is a thin wrapper around the unlink() POSIX system call and has no functional options: no force flag, no recursive mode, and no verbose output.
For removing multiple files, directories, or using wildcards, use rm instead.
The syntax is:
bashunlink filename
The command returns exit code 0 on success and produces no output. The only available options are --help and --version.
unlink removes the directory entry — the name-to-inode mapping in the parent directory. File data on disk is only freed when no more hard links point to the inode and no process has the file open.
To remove a file in the current directory:
bashunlink file.txt
To remove a file by path:
bashunlink /tmp/file.txt
Write permission is required on the parent directory, not on the file itself. The parent directory governs whether its entries can be added, renamed, or removed. If you see Permission denied, you need write access to the directory containing the file. Use sudo if needed.
Run ls file.txt to confirm the target exists before deleting. unlink does not ask for confirmation and there is no undo.
To remove a symbolic link:
bashunlink symlink_name
Only the symlink’s directory entry is removed. The file the symlink points to is not affected. The target’s hard link count remains unchanged.
unlink cannot remove directories. On GNU/Linux it always fails with Is a directory when given a directory path. Use rmdir for empty directories or rm -r for non-empty ones.
What happens when you unlink an open file. The filename disappears from the directory immediately. If a process still has the file open, the underlying data stays on disk until every file descriptor pointing to it is closed. Only then does the filesystem free the storage blocks. This behavior underlies log rotation: the old log file is unlinked while the application is still writing to it, and disk space is reclaimed only after the application closes the file handle.
rm calls unlink() internally for each regular file it removes. The practical difference is scope:
unlink removes exactly one file and accepts no optionsrm supports multiple files, globbing, recursive deletion (-r), and force mode (-f)Use unlink when you want a deliberate, explicit single-file deletion where the intent is clear from the command name itself. Use rm for everything else.
unlink removes one directory entry at a time. File data on disk persists until all hard links and open file handles are gone. Leave a comment below if you run into any issues.