A symbolic link (also known as a “soft link” or “symlink”) consists of a special type of file that serves as a reference to another file or directory. You can use any one of the following command to delete or remove symbolic links in Linux operating systems:
- rm command – Removes each given FILE including symbolic links in Linux.
- unlink command – Deletes a single specified file name including symbolic links in Linux.
Let us see some examples about to remove (delete) symbolic links in Linux.
Warning: Care must be taken with the following rm, unlink, and find command as those commands won’t prompt for removal confirmation. The author or nixCraft site is not responsible for any data loss. Please use all command with care and think twice before you press the [Enter] key. Always keep a verified backup of all files and data.
Linux Delete Symbolic Link File
Use the following syntax:
rm linkname unlink linkname
Examples
First, we are going to create a new symbolic link in Linux using the ln command. Use the cd command to /tmp/ directory:
cd /tmp/ ## create a new symbolic for demo purpose ## ln -s /etc/resolv.conf dns ## List it ## ls -l dns
Sample outputs:
lrwxrwxrwx 1 vivek vivek 16 2009-08-16 04:28 dns -> /etc/resolv.conf
Now we are going to delete the dns symbolic link using the rm command or unlink command as follows:
rm dns ## OR ## unlink dns
Verify it using the ls command:
ls -l dns ls -l /etc/resolv.conf
Getting confirmation prompt
We can force prompt before every removal of symbolic links by passing the -i to the rm:
rm -i {link-name}
rm -i -v test-link
rm: remove symbolic link 'test-link'? y
removed 'test-link'
Delete Symbolic Link Directory
The syntax is same:
rm linkDirName unlink linkDirName
Please avoid appending / at the end of linkDirName. cd in to the /tmp/ using the cd command:
cd /tmp/ ## create a new symlink for demo purpose ## ln -s /etc test ## List it ## ls -ld test
Sample Output:
lrwxrwxrwx 1 vivek vivek 4 2009-08-16 04:31 test -> /etc
Now delete the test symbolic link directory using any one of the following command:
rm test ## OR ## unlink test
Make sure symbolic link is removed using the ls command:
ls -ld test ls -ld /etc
Remove Symbolic Links with find command
Here is we can search and list all symbolic links using the find:
find {/path/to/dir} -type l -action find {/path/to/dir} -type l -name 'files-regex-to-search' -action find {/path/to/dir} -type l -iname 'Case-Insensitive-files-regex-to-search' -action find {/path/to/dir} -lname 'files-regex-to-search' -action
Say list all symlinks in /tmp/bin/, run:
find /tmp/bin/ -type l -print
Only list *.txt or *.sh symlinks, run:
find /tmp/bin/ -type l -iname "*.sh" -print find /tmp/bin/ -type l -iname "*.txt" -print ## modern syntax for GNU/find ## find /tmp/bin/ -lname "*.txt" -print find /tmp/bin/ -lname "*.db" -print find /tmp/bin/ -lname "*.sh" -print
Find all symbolic links with find and delete them
All you have to do is replace the -print action with the -delete as follows to delete all ‘*.sh’ symlinks:
find /tmp/bin/ -lname "*.sh" -delete
To get confirmation use the following syntax when you need to find all “*.txt” symlinks and delete them:
find /tmp/bin/ -type l -name "*.txt" -exec rm -i {} + find /tmp/bin/ -type l -name "*.db" -exec rm -i {} +
Where find command options are:
- -type l : Find only symbolic link
- -lname "*.txt" :File is a symbolic link whose contents match shell pattern such as “*.txt”. Pass the -ilname "pattern" to the find for the case insensitive match. This option only works the latest version of GNU/find.
- -print : Print matched file lists.
- -delete : Remove/delete matched symlinks.
- -exec rm -i {} + : Remove/delete matched symlinks using the rm command with confirmation
Linux Remove Symbolic Link Command Options
Type the following command:
rm --help
unlink --help
Conclusion
You learned the rm and unlink command to delete or remove a symbolic link under Linux operating systems. See the rm/unlink command man page by typing the following man command or read it online here:
man rm
man find
man unlink
🐧 15 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Is there any way to automatically delete the links too on deletion of a file
say,
mainfile -> linkfile
now rm -f mainfile , should delete linkfile too ?? in RHEL it exists but points no where.
Hi ,
can you explain below thing
[root@localhost html]# unlink bugzilla-st/
unlink: cannot unlink `bugzilla-st/’: Not a directory
[root@localhost html]#
[root@localhost html]#
[root@localhost html]# rm bugzilla-st/
rm: cannot remove `bugzilla-st/’: Is a directory
Thanks&Regards,
Srinu
rm -R bugzilla-st/
what`s the result?
dont put slash at the end, use “unlink bugzilla-st” then it will work even i ahd the same problem,
thanks, the trailing slash was the issue for me too
1st delete the source file..
then remove the Link
Eg:
symlink file
test 1–> /opt/test
removing source file
# rm -Rf /opt/test
remove link
# unlink test1
enjoy……. :D
[root@localhost html]# unlink bugzilla-st/
unlink: cannot unlink `bugzilla-st/’: Not a directory
==> unlink bugzilla-st
thanks All , After removing slash it is working .
All,
So how do you write a bash script to wipe all existing symlinks (unknown to you) and re-establish new ones?
Would need special exceptions for system created symlinks necessary for OS functions
Cheers!
TBNK
I found out the hard way that “unlink” will not only remove symbolic links but will delete a file if you run “unlink” on an actual file instead of a symbolic link.
unlink will also remove the target file of a sym link if that is the only link and no one has opened the target (no open file descriptor). Lost 10 days of work in a zippy damn it :(
Please mention this detail sir so others are saved from this pain like I am going through now :(
Ajju, it will not delete the target file.
I ran unlink on my softlink and the entire target file (which was a directory however a dir is also just a special kind of a file right?) got deleted.
Ouch, sorry to hear that. :-(
I’m experiencing exactly the same behavior on Fedora 22. Running the rm or ulink command on the symlink removes the content of the target directory, but the symbolic link stays unaffected. I can’t seem to be capable of figuring out why that is.
Don’t worry, rm or unlink doesn’t remove directories unless specifically told to do so. Also, rm/unlink doesn’t remove the target directory or file. You must have mixed options and deleted the target first. Here is an example to prove it:
2047198206603bc30a508be_000011
This is why you need to make regular backups. Mistakes happen at the CLI. Goodluck!