How To

unlink Command in Linux: Remove a Single File or Symlink

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.

How the unlink Command Works in Linux

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.

Remove a File or Symbolic Link

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.

unlink vs rm: When to Use Each

rm calls unlink() internally for each regular file it removes. The practical difference is scope:

  • unlink removes exactly one file and accepts no options
  • rm 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.

Cyber Defence

Recent Posts

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

2 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

3 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

6 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

6 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

6 days ago