Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Remove symbolic links file in Linux?
Symbolic links (also called soft links) are pointers to files or directories located elsewhere in the filesystem. They provide convenient shortcuts for accessing files without duplicating data. This tutorial demonstrates how to properly remove symbolic links in Linux without affecting the original files.
Understanding the Setup
Let's start with a directory containing files and their symbolic links. Here's our example structure
$ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt lrwxrwxrwx 1 kent kent 4 Apr 26 23:48 dirLink -> aDir/ lrwxrwxrwx 1 kent kent 9 Apr 26 23:48 fileLink -> aFile.txt
Notice the l at the beginning of symbolic links and the arrow (->) showing the target. We want to remove these symbolic links safely.
Method 1: Using the rm Command
The rm command is the most common method for removing symbolic links. Let's remove the file link first
$ rm fileLink $ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt lrwxrwxrwx 1 kent kent 4 Apr 26 23:48 dirLink -> aDir/
The symbolic link is removed successfully, but the original file remains intact.
Important: Avoid Trailing Slashes
When removing directory links, never add a trailing slash. This common mistake causes errors
$ rm dirLink/ rm: cannot remove 'dirLink/': Is a directory
The correct way is to use the link name without the trailing slash
$ rm dirLink $ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt
Method 2: Using the unlink Command
The unlink command is specifically designed for removing links and is part of the CoreUtils package. Let's recreate the links and demonstrate this method
$ unlink fileLink $ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt lrwxrwxrwx 1 kent kent 4 Apr 27 00:15 dirLink -> aDir/
The same trailing slash rule applies to unlink
$ unlink dirLink/ unlink: cannot unlink 'dirLink/': Not a directory $ unlink dirLink $ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt
Note: The unlink command can also remove regular files, not just symbolic links.
Method 3: Bulk Removal with find and xargs
For removing multiple symbolic links matching a pattern, use find combined with xargs. Consider this directory structure
$ tree . ??? 2bDeleted_01.txt -> aFile.txt ??? 2bDeletedDir -> aDir ??? 2bDeletedDir_01 -> aDir ??? 2bDeletedDir_02 -> aDir ??? 2bDeleted_I_am_not_a_link.txt ??? 2bDeleted.txt -> aFile.txt ??? aDir ? ??? 2bDeleted_etc -> /etc ? ??? keepMe_etc -> /etc ??? aFile.txt ??? keepMeDir -> aDir ??? keepMe.txt -> aFile.txt
To remove all symbolic links starting with "2bDeleted", first find them
$ find . -type l -name '2bDeleted*' ./2bDeleted.txt ./2bDeleted_01.txt ./2bDeletedDir ./2bDeletedDir_01 ./2bDeletedDir_02 ./aDir/2bDeleted_etc
The find command uses two key parameters
-type l Search for symbolic links only
-name '2bDeleted*' Match names starting with "2bDeleted"
Now pipe the results to xargs for removal
$ find . -type l -name '2bDeleted*' | xargs -I{} rm "{}"
$ tree
.
??? 2bDeleted_I_am_not_a_link.txt
??? aDir
? ??? keepMe_etc -> /etc
??? aFile.txt
??? keepMeDir -> aDir
??? keepMe.txt -> aFile.txt
Notice that only symbolic links were removed the regular file 2bDeleted_I_am_not_a_link.txt remains untouched.
Comparison
| Method | Command | Best For | Notes |
|---|---|---|---|
| rm | rm linkname |
Individual links | Most common method |
| unlink | unlink linkname |
Individual links | Specifically for links |
| find + xargs | find . -type l | xargs rm |
Multiple links | Bulk operations |
Key Points
Never add trailing slashes when removing directory symbolic links
Removing symbolic links does not affect the original files or directories
Both
rmandunlinkwork identically for symbolic linksUse
find -type lto locate symbolic links specificallyAlways test find commands before piping to removal operations
Conclusion
Removing symbolic links in Linux is straightforward using either rm or unlink commands. The key rule is to use the link name exactly without trailing slashes. For bulk operations, combine find with xargs to safely remove multiple links while preserving the original files.
