- Remove Symbolic Links
- What is a Symlink?
- How to Remove a Symbolic Link
- Using rm Command
- Using unlink
- Using Find and Delete
- How to Find Dangling Symlinks
- Removing Strings of Symlinks
- Conclusion
- More Information
- Remove a symlink to a directory
- 12 Answers 12
- How Do I Remove a Symbolic Link in Linux?
- Remove a Symbolic Link using the unlink command
- Remove a Symbolic Link using the rm command
- About the author
- Karim Buzdar
Remove Symbolic Links
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Symbolic links (also known as symlinks) in Linux are special files that reference other files, directories, devices, or pipes. In other operating systems, symlinks may be called “shortcuts”. Symlinks can be used to control access to files. For example, you can build links in a user’s home directory that accesses a more public file for reading and writing. Group permissions perform this function as well, but with a higher administrative maintenance load. Symlinks also bring access to a file deeply nested within a directory tree. Thus, users do not have to walk the file directory tree to access the file.
Learn how to create symlinks in our guide How to Create Linux Symlinks.
What is a Symlink?
A Linux symbolic link is a special kind of file. The content of this file is a file path, for example /tmp/file . The path name can be any valid path, so a symlink can cross over into other file systems, including network file systems. Most operations, such as reading, overwriting, appending, changing permissions, and changing owners affect the target object, not the symlink. However, you cannot delete the target via a symlink as a delete request affects the symlink directly.
Symlinks can point to other symlinks, too. The operating system traverses the chain of symlinks to arrive at the ultimate targeted file object.
When you use the ls -l to display a symlink, the output looks like this:
symlink -> /tmp/reference-file
The first part is the symlink, the part after the arrow is where the symlink points to.
How to Remove a Symbolic Link
To remove a symlink, you need write permission for the directory containing the symlink.
Keep in mind that when you remove a symlink, you do not affect the file system object the symlink points to. The target object is completely unaffected in any way when a symlink to it is removed.
When the target object is moved or deleted, the symlink that points to it becomes a dangling symlink.
Using rm Command
The rm command treats a symlink like any other file. Just use the normal rm syntax:
The command can specify any number or combination of files, wildcards, and modifiers, including symlinks:
Include the -i flag to be prompted before each file deletion.
Using unlink
The unlink command also treats a symlink like any other file. However, you can only specify one object per invocation. For example:
Using Find and Delete
To see all symlinks in a specific directory and its sub-directories, use the find command with the -type l flag:
To look only in the specified directory, ignoring the contents of any subdirectories, add the -maxdepth 1 switch:
For example, the following command searches the /tmp directory for symlinks:
Here’s an how the output of that command would look like:
/tmp/dangling-symlink1 /tmp/dangling-symlink3 /tmp/symlink2 /tmp/symlink1 /tmp/symlink /tmp/symlink3 /tmp/dangling-symlink2
To unconditionally delete all symlinks in a directory, add the -delete switch:
How to Find Dangling Symlinks
Overuse of symlinks can clog your file system and consume resources. To see the dangling symlinks in a single directory, use the find command with the -xtype l flag:
The command outputs each symlink, one per line. For example, the following command finds all dangling symlinks in the /tmp directory:
Here’s an example of how that output should appear:
/tmp/dangling-symlink1 /tmp/dangling-symlink3 /tmp/dangling-symlink2
When you want to make a clean sweep of the excess symlinks, add the delete flag:
Change the -maxdepth 1 to the recursion level needed, or remove the switch completely to cleanse all subdirectories of useless, dangling symlinks.
Removing Strings of Symlinks
A symlink can point to another symlink, which in turn can point to a symlink. This linking continues until you finally get to the target file, directory, device, pipe, or other file system target. Removing a chain of symlinks safely is a manual process.
First, install the package util-linux if it’s not already loaded into the Linux system.
The command namei takes a file path. If the file path is a symlink, it follows it all the way to the end, or until the symlink recursion limit is reached. For this example, inspect a chained symlink from /tmp/symlink to the file /home/satch/target with the following command:
f: /tmp/symlink d / d tmp l symlink -> symlink1 l symlink1 -> symlink2 l symlink2 -> symlink3 l symlink3 -> /home/satch/target d / d home d satch - target
The command follows the path through directories and symbolic links until it encounters a socket, block device, character device, FIFO (named pipe) or regular file. When deleting the symlink chain, start at the top symlink, changing the working directory as you go. In the output of the namei command, the first letter indicates the kind of file system object for that name. Here d stands for directory, l for symbolic link, and — for a regular file. The namei man page describes the indicator letters for other file system objects. Given the output shown above, issue the following to remove all levels of the symlink:
Run the namei command with the -mov flag to show when the remove command needs to be run with sudo :
Here’s an example output from this command:
f: /tmp/symlink dr-xr-xr-x root root / drwxrwxrwt root root tmp lrwxrwxrwx satch satch symlink -> symlink1 lrwxrwxrwx satch satch symlink1 -> symlink2 lrwxrwxrwx satch satch symlink2 -> symlink3 lrwxrwxrwx satch satch symlink3 -> /home/satch/target dr-xr-xr-x root root / drwxr-xr-x root root home drwx--x--- satch satch satch -rw-rw-r-- satch satch target
The difference is that the permissions and ownership are shown. This can save time and error messages when a symlink chain bounces through multiple partitions, network file systems, and protected directories.
Conclusion
Tidying up a file system of obsolete Linux symlinks frees up resources on your disks and network file systems. It’s good practice to remove a symbolic link when it is no longer needed, or the target has been moved or deleted.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on Tuesday, April 18, 2023.
Remove a symlink to a directory
I have a symlink to an important directory. I want to get rid of that symlink, while keeping the directory behind it. I tried rm and get back rm: cannot remove ‘foo’ .
I tried rmdir and got back rmdir: failed to remove ‘foo’: Directory not empty
I then progressed through rm -f , rm -rf and sudo rm -rf Then I went to find my back-ups. Is there a way to get rid of the symlink with out throwing away the baby with the bathwater?
what’s wrong with asking linux-related questions? last time i checked, stack overflow was not os-specific
@mwfearnley It would be, but check the date. Super User wasn’t a thing yet back then. It’s been ported, so it’s there too. Not sure why it’s still here, but it’s my highest scoring question and I’m not gonna look gift karma in the mouth.
12 Answers 12
# this works: rm foo # versus this, which doesn't: rm foo/
Basically, you need to tell it to delete a file, not delete a directory. I believe the difference between rm and rmdir exists because of differences in the way the C library treats each.
At any rate, the first should work, while the second should complain about foo being a directory.
If it doesn’t work as above, then check your permissions. You need write permission to the containing directory to remove files.
This does not always work. Occasionally you need to run rm -rf folderName (without trailing /) in order to remove the symlink. Amazon Linux behaves this way under certain circumstances, for example.
@r3mus: Never use -r with a symlink unless you want to lose everything inside it. -f shouldn’t be needed either, except perhaps to override file permissions.
Specifically, unlink has no relation to the ln AKA ‘link’ operation. The name unlink refers to the process of unlinking/removing a file from the file system’s file table so that the contents become detached from any reference to them — they are unlinked. It’s a confusing name that’s downright misleading when applied to links and symlinks in particular. unlink will work with symlinks because it works with any file regardless of type.
If you’re ever actually worried about doing something silly, alias rm=»rm -i» has saved more hides than just about anything else out there I think.
use the «unlink» command and make sure not to have the / at the end
unlink() deletes a name from the file system. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse. If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.
I think this may be problematic if I’m reading it correctly.
If the name referred to a symbolic link the link is removed.
If the name referred to a socket, fifo, or device the name for it is removed but processes that have the object open may continue to use it.
How Do I Remove a Symbolic Link in Linux?
A symbolic link, also known as symlink, is a file that points to another file. The file points to can be in the same or different directory. It is similar to the shortcuts in Windows OS.
In today’s post, we will be describing how to remove a symbolic link in Linux. Note that removing a symbolic link does not affect the file it points to.
Before removing a file, you can verify whether it is a symbolic link using the ls -l command. It will also show you the file or directory that it points to.
The l in permissions (lrwxrwxrwx) confirms that it’s a symbolic link.
Remove a Symbolic Link using the unlink command
The unlink command is used for removing a single file from the file system. To remove a symbolic link in Linux, type the unlink command followed by the name of the symbolic link and hit Enter:
Replace symbolic_link with the name of the symbolic link you want to remove. After that, you can use the ls -l command to confirm if the symlink has been removed.
Remove a symlink that points to a directory, don’t use the slash after the directory name. Let’ say we want to remove a symbolic link directory named Docs, as shown in the following screenshot:
The command to remove the symlink directory will be:
Remove a Symbolic Link using the rm command
The rm command can also be used to remove a symbolic link. For removing a symbolic link in Linux, type the rm command followed by the name of the symbolic link and hit Enter:
After that, you can use the ls -l command to confirm if the symlink has been removed.
You can also use the -i flag with the rm command to prompt for confirmation.
Removing a symlink that points to a directory, don’t use the slash after the directory name. Let’ say we want to remove a symbolic link directory named Docs, as shown in the following screenshot:
The command to remove the symlink directory will be:
After that, you can use the ls -l command to confirm if the symlink has been removed.
That is all there is to it! You have learned to remove a symbolic link in Linux OS using the unlink and rm command in this post. While removing a symbolic link, make sure only to remove the symbolic link itself, not the file or directory it is linking to.
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.