How to delete soft links in Linux system
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:

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:

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:

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:

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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.
