You may sometimes need to create or remove symlinks on Linux. If so, do you know what to do? Have you ever done this before? Did you step in the hole? If you step on a pothole, that’s no problem. If not, don’t worry, we’re here to help you.
The symbolic link is removed using the rm and unlink commands.
What is a symbolic link?
Symlink, also known as soft link, is a special file type. In Linux, a file points to another file or directory. It is similar to shortcuts in Windows. It can point to a file or directory on the same or different file systems or partitions.
Symbolic links are commonly used to link library files. It can also be used to link log files to folders on mounted NFS (network file system).
What is the rm command?
The rm command is used to remove files and directories. It’s very dangerous and you have to be very careful every time you use rm.
What is the unlink command?
The unlink command is used to remove special files. It is installed as part of GNU Gorutils.
1) How to remove symbolic link files using 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
Copy the code
Always use the rm command with -i to understand what is being done.
# rm -i symlinkfile1Rm: remove symbolic link 'symlinkFile1'? yCopy the code
It allows us to remove multiple symbolic links at once:
# rm -i symlinkfile2 symlinkfile3Rm: remove symbolic link 'symlinkFile2'? Y rm: Remove symbolic link 'symlinkFile3'? yCopy the code
1a) How to remove a symbolic link directory using rm command
This is like removing symbolic link files. Use the following command to remove the symbolic link directory.
# rm -i symlinkdirRm: Remove symbolic link 'symlinkdir'? yCopy the code
Use the following command to remove multiple symlink directories.
# rm -i symlinkdir1 symlinkdir2Rm: Remove symbolic link 'symlinkdir1'? Y rm: Remove symbolic link 'symlinkdir2'? yCopy the code
If you add a slash at the end, the symlink directory will not be removed. If you add, you will get an error.
# rm -i symlinkdir/Rm: cannot remove 'symlinkdir/' : Is a directoryCopy the code
You can increase -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 may not be what you intended.)
# 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 directoryCopy the code
2) How to remove symbolic links using unlink command
The unlink command deletes a specified file. It accepts only one file at a time.
Delete symbolic link files:
# unlink symlinkfile
Copy the code
Delete symbolic link directory:
# unlink symlinkdir2
Copy the code
If you add a slash at the end, you cannot remove the symlink directory using the unlink command.
# unlink symlinkdir3/Unlink: Cannot unlink 'symlinkdir3/' : Not a directoryCopy the code
Via: www.2daygeek.com/remove-dele…
Magesh Maruthamuthu (lujun9972
This article is originally compiled by LCTT and released in Linux China