Linux Delete Symbolic Link ( Softlink )

How do I delete symbolic link (softlink) in Linux operating system using a command prompt? How can I remove symbolic links in Linux?

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:

ADVERTISEMENTS

  • 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

Linux Delete Symbolic Link Command
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
Linux Remove Symbolic Link Directory Using the rm or unlink command

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 {} +

How to Remove (Delete) Symbolic Links in Linux Using find command
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

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
15 comments… add one
  • Veerabahu Jun 9, 2011 @ 6:34

    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.

  • Srinivas Nov 23, 2011 @ 8:47

    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

    • L.Yao Dec 26, 2011 @ 6:18

      rm -R bugzilla-st/
      what`s the result?

  • nandeep Jun 12, 2012 @ 17:22

    dont put slash at the end, use “unlink bugzilla-st” then it will work even i ahd the same problem,

    • mschober Mar 1, 2013 @ 15:33

      thanks, the trailing slash was the issue for me too

  • Prasad Jayasinghe Jun 22, 2012 @ 6:10

    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

  • cac Jul 27, 2012 @ 4:07

    [root@localhost html]# unlink bugzilla-st/
    unlink: cannot unlink `bugzilla-st/’: Not a directory

    ==> unlink bugzilla-st

  • srinivas Dec 11, 2012 @ 6:38

    thanks All , After removing slash it is working .

  • TBotNik Sep 25, 2014 @ 16:53

    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

  • Renato Jun 18, 2015 @ 19:20

    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.

  • Ajju Jan 29, 2016 @ 9:12

    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 :(

    • RENJU Feb 18, 2016 @ 7:13

      Ajju, it will not delete the target file.

      • Ajju Mar 8, 2016 @ 4:39

        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.

        • milcza Mar 28, 2016 @ 21:50

          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.

        • 🐧 Vivek Gite Mar 28, 2016 @ 22:41

          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:

          11258342045f9aaa92753f4_000011

          This is why you need to make regular backups. Mistakes happen at the CLI. Goodluck!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.