Command for removing (deleting) symbolic links in Linux

  • 2021-07-01 08:30:03
  • OfStack

You may sometimes need to create or delete symbolic links on Linux. If so, do you know what to do? Have you done it before? Did you step on the pit? If you step on the pit, there is no problem. If not, don't worry, we will help you here.

You can remove (delete) symbolic links using the rm and unlink commands.

What is symbolic link?

Symbolic link (symlink), also known as soft link, is a special file type. In Linux, the file points to another file or directory. It is similar to the shortcut in Windows. It can point to 1 file or directory in the same or different file system or partition.

Symbolic links are usually used to link library files. It can also be used to link log files and folders on mounted NFS (Network File System).

What is the rm command?

The rm command is used to remove files and directories. It is very dangerous, so you should be very careful every time you use rm command.

What is the unlink command?

The unlink command is used to remove special files. It was installed as part 1 of GNU Gorutils.

1) How to remove a symbolic link file using the rm command

The rm command is the most frequently used command in Linux and allows us to remove symbolic links as described below.

# rm symlinkfile

Always use the rm command with-i 1 to see what is being done.


# rm -i symlinkfile1
rm:remove symbolic link  ' symlinkfile1'?y

It allows us to remove multiple symbolic links at once:


# rm -i symlinkfile2 symlinkfile3
 
rm:remove symbolic link  ' symlinkfile2'?y
rm:remove symbolic link  ' symlinkfile3'?y

1a) How to remove a symbolic link directory using the rm command

This is like removing symbolic link files. Use the following command to remove the symbolic link directory.


# rm -i symlinkdir
rm:remove symbolic link  ' symlinkdir'?y

Use the following command to remove multiple symbolic link directories.


# rm -i symlinkdir1 symlinkdir2
rm:remove symbolic link  ' symlinkdir1'?y
rm:remove symbolic link  ' symlinkdir2'?y

If you add/at the end, this symbolic link directory will not be deleted. If you add it, you will get 1 error.


# rm -i symlinkdir/
rm:cannot remove ' symlinkdir/': Is a directory

You can add-r to deal with the above problems. But if you add this parameter, it will delete the contents of the target directory, and it will not delete the symbolic link file. (LCTT) This may not be your intention


# rm -ri symlinkdir/
rm:descend into directory ' symlinkdir/'?y
rm:remove regular file  ' symlinkdir/file4.txt'?y
rm:remove directory ' symlinkdir/'?y
rm:cannot remove ' symlinkdir/': Not a directory

2) How to remove symbolic links using the unlink command

The unlink command deletes the specified file. It accepts only one file at a time.

Delete symbolic link file:

# unlink symlinkfile

Delete the symbolic link directory:

# unlink symlinkdir2

If you add/at the end, you cannot delete the symbolic link directory using unlink command.


# unlink symlinkdir3/
 
unlink:cannot unlink  ' symlinkdir3/': Not a directory

Summarize


Related articles: