Operation and Maintenance | How to delete soft links in Linux system | Linux
introduce
In Linux, symbolic link (symbolic link, or symlink) is also calledsoft link , is a special type of file used as a shortcut to another file.
Instructions
We can use the ln
command to create a soft link, so how to delete it?
There is no special command for deleting symbolic links in Linux, but you can use the rm
command, which is mainly used to delete files and directories, as follows:
$ rm [file|dir]
Of course you can also use the unlink
command, but do not use the name of the soft link directly, because it is not only used to delete the link, but also deletes the file.
$ unlink [file]
Note
The unlink command can only delete files, not directories
Let’s take a closer look below.
- Create a symbolic link using the ln command
ln <-s|-f> [symlink_origin_file|symlink_origin_dir] [symlink_target_file|symlink_target_dir]
- Remove symbolic links using the rm command
Provide the name of the link path after the rm command:
$ rm [symlink_file|symlink_dir]
Let's look at a specific example. The following command lists the files in the current path:
$ ls -l
Reference example
In the above list, my_link.txt
is a symbolic link. You can see that it starts with l
(indicating a link), and the name shows the file it points to.
We delete it using rm
and verify:
$ rm my_link.txt
$ ls -l
Reference example
As shown in the image below, the soft link is removed in this way, but the source file still exists:
- Delete multiple links
You can use the rm
command to delete multiple symbolic links at once:
$ rm [symlink1 symlink2 ... symlink3]
- Use the unlink command to delete soft links
Another way to delete a soft link is to use the unlink
command. This command sounds like it is only used to delete links, but it can also delete files. Here's how to use it:
$ unlink [symlink_file|symlink_dir]
We use the above example as shown below:
Note
The unlink command cannot delete multiple links at one time.
- Remove soft links to directories
We can create soft links to files and directories. Deleting a directory requires the -r
option, but deleting a link to a directory does not require the -r
option.
The usage is the same as deleting file links:
$ rm [symlink_file|symlink_dir]
When deleting a soft link pointing to a directory, do not add a slash at the end of the directory, otherwise an error will be reported:
$ rm [symlink_dir/]
rm: 无法删除"symlink_dir/": 是一个目录
The following figure is an example of deleting a directory soft link:
Note
Try not to use the-f
option to force removal of links to directories, as it will delete the contents of the directory.
- Remove hard link
Unlike soft links, hard links are virtually indistinguishable from the original file and you can only notice it by inode number
.
Look at the following example:
$ ls -li
total 4716
544057 -rw-rw-r-- 1 root staff 4794657 Sep 27 20:36 my_hard_link.txt
Deleting a hard link is the same as deleting a file:
rm [symlink_file|symlink_dir]
- Delete both linked files and source files
In fact, there are almost no scenarios and requirements for deleting the original file while deleting the soft link. Of course, if there is one, you can find the original file via the symlink and delete it.
rm "$(readlink '/path/to/symlink')" /path/to/symlink
Note
If the source file is deleted but the soft link is not deleted, the link will become a broken or dangling link.
Summarize
While the unlink
command exists, I recommend using the rm
command to remove symbolic links. Because this is a command we are all familiar with, we often use it to delete files. Of course, it can also be used to delete symbolic links.