How to Remove a Symbolic Link in Linux: unlink and rm
Complete guide to remove symbolic link in linux: syntax, options, practical examples, real-world Linux workflows, security considerations, and troubleshooting.
9 min read
Complete guide to remove symbolic link in linux: syntax, options, practical examples, real-world Linux workflows, security considerations, and troubleshooting.
- What unlink Does and When to Use It
- Availability and Installation Across Linux Distributions
- Command Syntax, Arguments, and Exit Status Codes
Managing system pointers and deployments frequently requires administrators to remove symbolic link in linux configurations. Whether retiring obsolete application versions, cleaning up broken pointers after file refactoring, or reconfiguring web server virtual host links, deleting a symlink must be performed cleanly without affecting the underlying target asset.
To remove symbolic link in Linux without touching or deleting the underlying target file or directory, use either unlink /path/to/symlink or rm /path/to/symlink. When removing a symlink that points to a directory, never include a trailing slash (e.g., use rm mylink, not rm mylink/) to avoid accidentally deleting the target directory's contents.
Linux provides two command-line tools for deleting symbolic links: unlink and rm. The unlink command is a minimalist utility that directly wraps the kernel's unlink(2) system call, deleting exactly one file or symlink at a time with no flags or prompts. The rm command is more versatile, supporting multiple arguments and interactive prompts.
A critical pitfall in symlink removal involves trailing slashes. Appending a trailing slash to a symlink that points to a directory (e.g. rm -r mylink/) instructs the shell and kernel to dereference the link and delete the actual contents of the target directory rather than the symlink itself. Mastering safe removal syntax is essential to prevent accidental data loss.
What unlink Does and When to Use It
At the kernel interface, removing a symbolic link invokes the unlinkat(2) or unlink(2) system call. The Linux Virtual File System (VFS) locates the directory entry (dentry) matching the symlink name in the parent folder, removes the dentry from the dcache, and decrements the link count of the symlink's own inode. Because a symbolic link has its own independent inode (type S_IFLNK), unlinking the symlink has zero effect on the target file's inode or data blocks.
The trailing slash trap occurs during path resolution in the kernel. When a path ends with a trailing slash (e.g., symlink_dir/), POSIX path resolution rules dictate that the trailing slash implies a directory traversal (equivalent to symlink_dir/.). The kernel automatically dereferences the symlink, resolving it to the target directory. If combined with rm -r, the command operates on the contents of the target folder rather than unlinking the symlink inode itself.
At the operating system level, unlink (distributed in coreutils) interfaces with the Linux kernel through standard C library system calls and virtual filesystems. Understanding how unlink resolves file descriptors, kernel structures, and exit status codes helps system administrators predict performance on high-load production servers.
Availability and Installation Across Linux Distributions
The unlink utility is packaged in coreutils across mainstream Linux distributions. It is pre-installed on standard Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and Arch Linux systems, while minimal container images may require manual installation:
Before running administrative tasks or automation scripts, verify whether unlink is installed and inspect which binary path and version your shell resolves:
type -a unlink
unlink --version 2>/dev/null || man -f unlink
# Illustrative output (reviewed against documentation)
unlink is /usr/bin/unlink
unlink (coreutils)
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y coreutils
# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y coreutils
# Arch Linux
sudo pacman -S --noconfirm coreutils
# Alpine Linux
sudo apk add --no-cache coreutils
Command Syntax, Arguments, and Exit Status Codes
unlink SYMLINK_NAME | rm [OPTION]... SYMLINK_NAME...
The simplest syntax is unlink SYMLINK_NAME. The unlink utility accepts exactly one operand and does not accept options other than standard --help and --version. Because it cannot accept multiple files, wildcard patterns, or recursive flags, unlink provides an inherent safeguard against accidental recursive deletion.
The alternative syntax is rm [OPTION]... SYMLINK_NAME.... With rm, you can remove multiple symlinks simultaneously (e.g., rm link1 link2 link3), prompt interactively with -i, or log actions with -v. Never pass -r when deleting a symlink, as a symlink itself is always a file, never a directory.
0— The symbolic link was successfully removed from the filesystem.1— Operational failure, such as insufficient permissions to modify the parent directory.2— Malformed command syntax (such as passing multiple arguments to unlink).
Important Options and Flags Reference
| Option / Flag | Long Form / Argument | Technical Behavior and Purpose |
|---|---|---|
unlink <FILE> | No Flags | Calls the unlink(2) syscall on exactly one file or symlink operand. |
rm <LINK> | Basic Removal | Removes one or more symbolic links safely (without trailing slash). |
rm -i <LINK> | --interactive | Prompts for confirmation before unlinking the symlink. |
rm -v <LINK> | --verbose | Outputs an informative confirmation line showing which link was removed. |
rm -f <LINK> | --force | Removes symlink without prompting and ignores nonexistent filenames. |
find -xtype l | Search Filter | Locates broken (dangling) symbolic links across a directory tree. |
-delete (find) | Action Filter | Directly unlinks discovered files or broken symlinks during find traversal. |
The deliberate simplicity of unlink makes it exceptionally safe for automation scripts. If a script inadvertently passes a wildcard that expands to multiple files, unlink immediately exits with an error (unlink: extra operand) rather than deleting multiple items. Similarly, passing a directory path to unlink fails instantly with unlink: cannot unlink 'dir': Is a directory.
When cleaning large trees with hundreds of broken symlinks, combining find -xtype l with -delete is vastly more efficient than manual deletion. The kernel evaluates symlink targets in memory and unlinks dangling pointers without launching external subshells.
When combining short flags on the command line, POSIX and GNU utilities allow clustering boolean flags behind a single hyphen, provided any option taking an argument appears last. In automated shell scripts, using long-form GNU options where portable makes self-documenting code that is easier to audit during peer review.
Practical Basic and Advanced Usage Examples
1. Remove a Symbolic Link Safely Using unlink
Delete a single symbolic link cleanly using the dedicated unlink command.
touch /tmp/target_file.txt
ln -s /tmp/target_file.txt /tmp/my_link
unlink /tmp/my_link
ls -l /tmp/target_file.txt
# Illustrative output (reviewed against documentation)
-rw-r--r-- 1 admin admin 0 Oct 22 21:30 /tmp/target_file.txt
unlink deletes the my_link symlink while leaving target_file.txt completely intact and untouched.
2. Remove a Symbolic Link Safely Using rm
Delete a symbolic link pointing to a file using standard rm with verbose feedback.
ln -s /etc/hosts /tmp/hosts_link
rm -v /tmp/hosts_link
# Illustrative output (reviewed against documentation)
removed '/tmp/hosts_link'
rm unlinks the symlink file without modifying /etc/hosts, confirming deletion with the -v flag.
3. Safe Removal of a Directory Symlink (No Trailing Slash)
Safely delete a symlink pointing to a directory without touching the target directory or its contents.
mkdir -p /tmp/real_data && touch /tmp/real_data/important.db
ln -s /tmp/real_data /tmp/data_link
# Safe removal: NO trailing slash!
rm /tmp/data_link
ls -d /tmp/real_data /tmp/real_data/important.db
# Illustrative output (reviewed against documentation)
/tmp/real_data
/tmp/real_data/important.db
Because data_link had no trailing slash, rm unlinks the symlink only; the real_data folder and database file are preserved.
4. Remove Multiple Symbolic Links in a Single Command
Use rm to delete multiple symbolic links simultaneously using shell wildcards.
touch /tmp/base.txt
ln -s /tmp/base.txt /tmp/link_a.tmp && ln -s /tmp/base.txt /tmp/link_b.tmp
rm -v /tmp/link_*.tmp
# Illustrative output (reviewed against documentation)
removed '/tmp/link_a.tmp'
removed '/tmp/link_b.tmp'
Wildcard expansion passes all matching link names to rm, unlinking each in a single operation.
5. Prompt Before Removing a Symlink Interactively (-i)
Use interactive confirmation to double-check symlink deletion in sensitive directories.
ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
rm -i /etc/nginx/sites-enabled/default
# Illustrative output (reviewed against documentation)
rm: remove symbolic link '/etc/nginx/sites-enabled/default'?
Interactive mode prompts before removing the link, allowing the administrator to confirm or abort.
6. Find and Delete All Broken Symlinks in One Command
Identify and purge all dangling symbolic links within a directory tree automatically.
mkdir -p /tmp/test_broken
ln -s /tmp/nonexistent_file /tmp/test_broken/orphan_link
find /tmp/test_broken -xtype l -delete
find /tmp/test_broken -xtype l
find -xtype l isolates broken symlinks, and -delete removes them instantly without external rm calls.
Real-World Workflows, Pipelines, and Shell Automation
Nginx Site Deactivation and Symlink Cleanup
Safely deactivate a virtual host in Nginx by removing its symlink from sites-enabled while preserving the original configuration in sites-available.
SITE="example.com.conf"
ENABLED_LINK="/etc/nginx/sites-enabled/${SITE}"
if [ -L "${ENABLED_LINK}" ]; then
echo "Deactivating site ${SITE}..."
sudo rm "${ENABLED_LINK}"
echo "Testing Nginx configuration syntax..."
sudo nginx -t && sudo systemctl reload nginx
echo "Site successfully deactivated."
else
echo "Notice: ${ENABLED_LINK} is not an active symlink."
fi
# Illustrative output (reviewed against documentation)
Deactivating site example.com.conf...
Testing Nginx configuration syntax...
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Site successfully deactivated.
Removing the symlink disables the site cleanly; the configuration in sites-available remains available for reactivation.
Automated Deployment Release Retirement Pipeline
During automated deployment cleanups, safely unlink deprecated release pointers before archiving old release directories.
BASE_DIR="/opt/myapp"
OLD_RELEASE="v1.8.2"
OLD_LINK="$BASE_DIR/releases/$OLD_RELEASE/config.env"
if [ -L "$OLD_LINK" ]; then
echo "Unlinking deprecated configuration pointer: $OLD_LINK"
unlink "$OLD_LINK"
fi
Using unlink guarantees that only the symlink pointer is removed, preventing accidental deletion of shared config files.
Pipelines, Redirection, Quoting, and Shell Expansion
When embedding unlink inside shell pipelines (|), conditional chains (&& and ||), or command substitutions ($(...)), the calling shell performs tilde expansion, variable interpolation, and globbing before invoking the binary. Always wrap paths, patterns, and variables in quotes so whitespace and wildcard characters (*, ?) are not expanded prematurely, and use set -euo pipefail in unattended scripts.
Permissions, Security Implications, and Safe Execution
CRITICAL DANGER: Never append a trailing slash when removing a symlink that points to a directory (e.g. rm -r my_symlink/). The trailing slash forces the command to dereference the link and delete the actual contents of the target folder! Always use rm my_symlink or unlink my_symlink.
Removing a symbolic link requires write (w) and execute (x) permissions on the parent directory containing the symlink. You do not need write permissions on the symlink itself or on the target file it points to.
In shared temporary directories like /tmp, the sticky bit (chmod +t) prevents unprivileged users from unlinking symbolic links created by other users, protecting against race conditions and symlink hijacking attacks.
GNU/Linux, BSD, and BusyBox Implementation Differences
Both unlink and rm are standardized under POSIX.1-2017 and are universal across Linux, macOS, and BSD systems.
The unlink command is strictly limited to a single argument in POSIX. Writing unlink file1 file2 will fail on all standard Unix and Linux distributions. When scripting batch deletions, use rm or find.
Troubleshooting Common Errors and Diagnostics
rm: cannot remove 'link/': Is a directory
Root Cause: You included a trailing slash on a symlink pointing to a directory (e.g. rm link/). The shell dereferenced the link to the target directory container.
Resolution: Remove the trailing slash from the command line: use rm link rather than rm link/.
rm link
unlink: extra operand 'file2'
Root Cause: The unlink command was passed more than one file argument or a wildcard glob that expanded to multiple names.
Resolution: Use rm when deleting multiple symlinks, or loop through arguments with unlink.
rm file1 file2
unlink: cannot unlink 'dir': Is a directory
Root Cause: unlink was invoked on a physical directory rather than a symbolic link or regular file.
Resolution: Use rmdir to remove empty directories or rm -r to remove directory trees.
rmdir dir/
Alternatives, Related Commands, and Related Tutorials
- How to Create Symlinks in Linux: ln -s Command Guide — Practical syntax, flags, and examples for
ln. - How to Remove a Directory in Linux: rmdir & rm Guide — Practical syntax, flags, and examples for
rmdir. - rm Command in Linux: Flags, Syntax & Safe File Deletion — Practical syntax, flags, and examples for
rm. - ls Command in Linux: List Files, Flags & Practical Guide — Practical syntax, flags, and examples for
ls. - 25 Best Linux Commands Every Sysadmin Should Know — Core command-line utilities for daily Linux server administration.
- How to Find a File in Linux from the Command Line — Practical file search patterns across Linux directories.
- Linux Commands Knowledge Hub (#linux-commands) — Browse all Linux command-line tutorials and system administration references.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides

Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!