Home > System Tutorial > LINUX > How to delete soft links in Linux system

How to delete soft links in Linux system

WBOY
Release: 2024-02-11 15:00:14
forward
1077 people have browsed it

In Linux systems, symbolic links (symbolic links, or symlinks for short) are also called soft links. It is a special type of file that is used as a shortcut to another file.

We can use the ln command to create soft links. So how to delete it? There is no command specifically for deleting symbolic links in Linux systems, but we can use the rm command to delete it. The rm command is mainly used to delete files and directories.

rm symbolic_link_name
Copy after login

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 symbolic_link_name
Copy after login

Let’s take a closer look below.

Use the rm command to delete symbolic links

Provide the name of the link path after the rm command:

rm name_or_path_of_link
Copy after login

Let’s look at a specific example. The files in the current path are listed in the following command:

$ ls -l
total 4708
-rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json
-rw-rw-r-- 1 abhishek abhishek     311 Sep 22 12:19 line.txt
lrwxrwxrwx 1 abhishek abhishek      26 Oct 17 11:24 mylink -> ./Documents/sample-mark.md
-rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json
-rw-r--r-- 1 abhishek abhishek   12817 Sep 22 12:28 sample.txt
Copy after login

Mylink in the above list 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 use rm to delete it and verify:

$ rm mylink 
$ ls -l
total 4708
-rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json
-rw-rw-r-- 1 abhishek abhishek     311 Sep 22 12:19 line.txt
-rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json
-rw-r--r-- 1 abhishek abhishek   12817 Sep 22 12:28 sample.txt
Copy after login

As shown below, the source file still exists:

Linux 系统中如何删除软连接

Delete multiple links

You can use the rm command to delete multiple symbolic links at once:

rm symlink1 symlink2 symlink3
Copy after login

Use the unlink command to delete soft links

Another way to delete soft links is to use the unlink command. This command may sound like it is only used to delete links, but it can also delete files. The usage is as follows:

unlink name_or_path_of_link
Copy after login

We use the above example, as shown below:

Linux 系统中如何删除软连接

unlink Multiple links cannot be deleted at one time.

Delete soft links pointing to directories

We can create soft links pointing to files or directories. Deleting a directory requires the -r option, but deleting a link to a directory does not require the -r option.

Usage method is the same as deleting file links:

rm name_or_path_to_link_to_dir
Copy after login

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 link_to_dir/
rm: cannot remove 'link_to_dir/': Is a directory
Copy after login

The following figure is an example of deleting a directory soft link:

Linux 系统中如何删除软连接

Note, try not to use the -f option to force the deletion of links to directories, as it will delete the contents of the directory. As shown below:

Linux 系统中如何删除软连接

Delete hard link

Unlike soft links, hard links are almost indistinguishable from the original file, you can only notice it by the inode number.

Look at the example below:

$ ls -li
total 4716
 544057 -rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json
 544884 -rw-rw-r-- 2 abhishek abhishek     311 Sep 22 12:19 hard_link
 544884 -rw-rw-r-- 2 abhishek abhishek     311 Sep 22 12:19 line.txt
1181365 drwxrwxr-x 2 abhishek abhishek    4096 Oct 17 12:33 my_dir
 546419 -rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json
Copy after login

Deleting a hard link is the same as deleting a file:

rm path_or_name_of_hard_link
Copy after login

What happens if you delete the linked file?

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/link')" /path/to/link
Copy after login

Note that if the source file is deleted but the soft link is not deleted, the link will become a broken or dangling link.

Although the unlink command exists, I recommend using the rm command to delete 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.

The above is the detailed content of How to delete soft links in Linux system. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template