search
HomeSystem TutorialLINUXHow to delete soft links in Linux system

How to delete soft links in Linux system

Feb 11, 2024 pm 03:00 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux learning

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

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

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

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

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

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

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

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

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

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

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

rm path_or_name_of_hard_link

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

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!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
How to Use 'next' Command with Awk in Linux - Part 6How to Use 'next' Command with Awk in Linux - Part 6May 15, 2025 am 10:43 AM

In this sixth installment of our Awk series, we will explore the next command, which is instrumental in enhancing the efficiency of your script executions by skipping redundant processing steps.What is the next Command?The next command in awk instruc

How to Efficiently Transfer Files in LinuxHow to Efficiently Transfer Files in LinuxMay 15, 2025 am 10:42 AM

Transferring files in Linux systems is a common task that every system administrator should master, especially when it comes to network transmission between local or remote systems. Linux provides two commonly used tools to accomplish this task: SCP (Secure Replication) and Rsync. Both provide a safe and convenient way to transfer files between local or remote machines. This article will explain in detail how to use SCP and Rsync commands to transfer files, including local and remote file transfers. Understand the scp (Secure Copy Protocol) in Linux scp command is a command line program used to securely copy files and directories between two hosts via SSH (Secure Shell), which means that when files are transferred over the Internet, the number of

10 Most Popular Linux Desktop Environments of All Time10 Most Popular Linux Desktop Environments of All TimeMay 15, 2025 am 10:35 AM

One fascinating feature of Linux, in contrast to Windows and Mac OS X, is its support for a variety of desktop environments. This allows desktop users to select the most suitable and fitting desktop environment based on their computing requirements.A

How to Install LibreOffice 24.8 in Linux DesktopHow to Install LibreOffice 24.8 in Linux DesktopMay 15, 2025 am 10:15 AM

LibreOffice stands out as a robust and open-source office suite, tailored for Linux, Windows, and Mac platforms. It boasts an array of advanced features for handling word documents, spreadsheets, presentations, drawings, calculations, and mathematica

How to Work with PDF Files Using ONLYOFFICE Docs in LinuxHow to Work with PDF Files Using ONLYOFFICE Docs in LinuxMay 15, 2025 am 09:58 AM

Linux users who manage PDF files have a wide array of programs at their disposal. Specifically, there are numerous specialized PDF tools designed for various functions.For instance, you might opt to install a PDF viewer for reading files or a PDF edi

How to Filter Command Output Using Awk and STDINHow to Filter Command Output Using Awk and STDINMay 15, 2025 am 09:53 AM

In the earlier segments of the Awk command series, our focus was primarily on reading input from files. However, what if you need to read input from STDIN?In Part 7 of the Awk series, we will explore several examples where you can use the output of o

Clifm - Lightning-Fast Terminal File Manager for LinuxClifm - Lightning-Fast Terminal File Manager for LinuxMay 15, 2025 am 09:45 AM

Clifm stands out as a distinctive and incredibly swift command-line file manager, designed on the foundation of a shell-like interface. This means that users can engage with their file system using commands they are already familiar with.The choice o

How to Upgrade from Linux Mint 21.3 to Linux Mint 22How to Upgrade from Linux Mint 21.3 to Linux Mint 22May 15, 2025 am 09:44 AM

If you prefer not to perform a new installation of Linux Mint 22 Wilma, you have the option to upgrade from a previous version.In this guide, we will detail the process to upgrade from Linux Mint 21.3 (the most recent minor release of the 21.x series

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software