Site icon DesignLinux

How to Delete (Remove) Symbolic Links in Linux

How to Delete (Remove) Symbolic Links in Linux

A symbolic link is also known as symlink or soft link, is a special type of file that serves as a reference to another file or directory. A symlink can point to a file or a directory on the same or a different filesystem or partition. This guide explains how to remove symbolic links in Linux systems.

Before going ahead for removing symbolic link, make sure use have writing permission on parent directory of the symlink. Otherwise, you will get “Operation not permitted” error.

To check symlink and to find the destination directory or file, use ls -l command.

ls -l /home/file.php
lrwxrwxrwx 1 tecnstuff tnsgrp 4 May 2 14:03 /home/file.php -> file_link.php

In above output the first character l shows that the file is symlink and the arrow -> symbol indicates where the symlink points to.

Remove Symbolic Links with rm

To remove a symbolic link, use the rm command followed by the symbolic link name. Using rm command you can remove given file or directories.

rm SYMLINK_NAME

For example, to delete the /home/file.php symlink, you would run following command:

rm /home/file.php

It will not show output and exits with zero.

If you would like to delete more than one symbolic link, you can pass multiple symlink names as arguments with space separated.

rm SYMLINK_NAME_1, SYMLINK_NAME_2

If you would like to prompt confirmation message before deleting the symlink, you should pass -i option along with rm command:

rm -i SYMLINK_NAME

Type y and press Enter key for confirmation.

You will get following output:

rm: remove symbolic link 'SYMLINK_NAME'?

Ensure that you never use -r option along with rm command while removing symlink. Otherwise it will remove all the contents of the destination directory.

Remove Symbolic Links with unlink

Unlink command removes the given symlink. It is possible to delete only a single file using unlink.

To remove a symlink using unlink, run the following command followed by the symlink name.

unlink SYMLINK_NAME

For instance, to remove the /home/file.php symlink, you would run following unlink command:

unlink /home/file.php

When removing a symbolic link that points to a directory do not append a trailing slash to the symlink name.

Conclusion

This tutorial shown you how to remove symbolic links or symlink using rm or unlink command.

If you have any question or feedback, please leave a comment below.

Exit mobile version