Remove all symbolic link linux

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.

Читайте также:  Usb hid device linux

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 all symlinks in a folder (dozens of them) at once? It’s not practical to insert every single one of them by hand when using unlink or rm.

What do you mean by «folder»? Are you just talking about what Linux calls directories, or do you have some other usage in mind?

4 Answers 4

You can use the find -command to do that:

find /path/to/directory -maxdepth 1 -type l -delete 

To be on the safe side, check first without the -delete -option:

find /path/to/directory -maxdepth 1 -type l 

-maxdepth 1 ensures that find will look only in /path/to/directory but not in it’s subfolders for symlinks. Feel free to take a look at man find .

List the links in the current directory alias folder and check that you really want to remove them,

find -type l -ls # search also in subdirectories find -maxdepth 1 -type l -ls # search only in the directory itself 

If things look good, and you want to delete these links, run

find -type l -delete # delete also in subdirectories find -maxdepth 1 -type l -delete # delete only in the directory itself 

If you want to delete interactively, you can use the following command line (this is safer)

find -type l -exec rm -i <> + # delete also in subdirectories find -maxdepth 1 -type l -exec rm -i <> + # delete only in the directory itself 

Источник

Читайте также:  Cpu frequency scaling linux

I want to remove all the symbolic links of files. I have a directory structure like /usr/local/instantclient/11.2.0.3 which contains lots of files and i have symbolic links of these files in /usr/local/lib/ Now i want to delete all these symbolic links of those files. How can i do this in one command. If i remove the actual dir /usr/local/instantclient/11.2.0.3 containing files then it will leave the broken links in /usr/local/lib .

2 Answers 2

To remove the links (from man find under -type ):

 l symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype. 

I think this should do the trick:

find /usr/local/lib/ -maxdepth 1 -follow -type l 

Does the output produce a list of the files you want to delete? If so, when you are 100% sure:

find /usr/local/lib/ -maxdepth 1 -follow -type l -delete 

This will remove only broken links. To delete all links, remove the -follow stanza, but I wouldn’t do that under /usr/local/lib .

Just to clarify: this assumes that the target directory has already been removed, so the links are already broken. The command will list all broken symlinks in the /usr/local/lib directory (but not recurse), so if there are other broken links, then it will show more than needed. Not that there should be a lot of broken links there to begin with, so it’s probably OK, but it is possible for a system to have purposefully broken links, and we’d like to spare those.

I tried the command suggested by Petter. The -follow option doesn’t work. Only find /usr/local/lib/ -maxdepth 1 -type l | delete worked. But i think this is not the best way to do it. There is actually no way to list all the soft links of a file while hard links of a files can be listed using inode value of file. Here is a discussion about it stackoverflow.com/questions/4532241/…

How to find and delete all hard links to a file is discussed here linuxcommando.blogspot.in/2008/09/… but this doesn’t work for sybmolic links.

Читайте также:  Скопировать файл linux сети

While there may be no (easy) way of «reverse readlink» a symbolic link, I don’t see how that is relevant to your problem; you said that the links created were located in /usr/local/lib ? also, do you have no -follow option in your version of find?

Источник

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.

Источник

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