Linux Delete Symbolic Link ( Softlink )

last updated in Categories , , , , , , ,

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


ADVERTISEMENTS

15 comment

  1. 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.

  2. 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

  3. 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

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

    ==> unlink bugzilla-st

  5. 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

  6. 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.

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

      1. 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.

        1. 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.

        2. 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:

          # create a dir
          mkdir foo
          # copy some file 
          cp /etc/resolv.conf foo
          # Create soft link
          ln -s foo bar
          # Verify it
          ls -ldi foo bar
          # Delete the link
          unlink bar
          # Verify it and foo should be there
          ls -li

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

Leave a Comment