Removing symbolic link in linux

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.

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.

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.

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:

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.

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.

Читайте также:  Linux find only current directory

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.

Источник

permission denied: You do know you need sudo if you want to use it in a situation you do not own the file? That goes for ‘rm’ too.

@RaheelKhan no you did -not- You removed a SYMLINK. Python relies on this symlink though. If you recreate that symlink python will be back.

You can try the unlink command as well.

unlink is a similar command to rm . Therefore rm will work same as unlink

While you are correct that unlink will remove the symlink, it is not an alias of rm . They are different, if ever so slightly. For one you cannot pass multiple arguments to unlink

Suppose you were trying to do:

sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin/ 
sudo ln -s /usr/share/somethingelse /var/www/phpmyadmin/ 

To correct it simply navigate to the folder where the link is and unlink

cd /var/www/phpmyadmin/ ~:# unlink somethingelse 

You can use the following to remove the symbolic link

Explanation

  • rm is the terminal command to remove a file. See rm —help for more options that it can take.
  • sudo is used because the symbolic link was created with sudo . The file therefore belongs to root and your normal user will not have permission to edit/remove it (you would be able to force this if you had write permission on the directory, which would not be the case here).

Extra

Also see this post and my comment to the first answer to access phpmyadmin when getting a not found error after install.

A small caveat I found was that I was trying to run rm and unlink on a symlink and I was getting an error that it was a directory.

$ rm folder_name/ rm: cannot remove ‘folder_name/’: Is a directory $ unlink folder_name/ unlink: cannot unlink ‘folder_name/’: Not a directory 

To remove the symlink, I used unlink folder_name . It was failing as there was a trailing / which causes the file to appear to be 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?

Читайте также:  Linux apache enable so

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.

Источник

Оцените статью
Adblock
detector