Linux remove soft link

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.

Источник

Читайте также:  Linux mint xfce minimal

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.

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:

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.

Читайте также:  Чем linux unix ubuntu

Источник

Learn a thing or two about deleting soft links and hard links in the Linux command line.

The symbolic link, also known as soft link or symlink, in Linux is a special type of file that works as a shortcut to another file.

You can create a soft link using the ln command. But what about deleting them?

There is no special command for removing symbolic links in Linux. You can use the rm command which is also used for deleting files and directories.

You may also the unlink command here. Don’t go by its name. It’s not only for deleting links; it can also delete files.

unlink symbolic_link_name

All you have to do is to give the name of the path of the link to the command:

Let’s see it with an example. Can you identify the soft link in the long list output of ls command?

[email protected]:~/test$ 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

The link is called mylink. You can identify it in the long listing as it starts with the character l (for link) and the name shows the file it points to.

Let me delete the link and verify it:

[email protected]:~/test$ rm mylink [email protected]:~/test$ 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 

You can verify that the original file still exists.

Deleting soft links in Linux with the rm command

You can remove multiple symlinks at once with the rm command:

rm symlink1 symlink2 symlink3

Another way to delete soft links is by using the unlink command. It may sound like this command is only for removing links, it can delete files as well.

To remove a link with unlink, use it like this.

unlink name_or_path_of_link

I’ll use the same example I used earlier.

Removing soft link using unlink command

The unlink command is pretty limited. You can not delete multiple links at once with it.

You can create soft links to both files and directories. While you have to remove a directory with -r option, the link to a directory doesn’t need that.

Use it the same way you used to delete link to a file:

rm name_or_path_to_link_to_dir

Don’t use trailing slashes with the link else it will complain.

[email protected]:~/test$ rm link_to_dir/ rm: cannot remove 'link_to_dir/': Is a directory

Here’s an example of deleting a soft link to directory.

Delete link to a directory in Linux

Never force remove a link to a directory with rm -f because it will delete the contents of the directory.

Unlike soft links, a hard link is almost indistinguishable from the original file. You can only notice it with the inode number.

Читайте также:  Astra linux common edition virtualbox

Can you identify the hard link and the file in the output? Pay attention to the inode numbers.

[email protected]:~/test$ 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 

And deleting a hard link is the same as deleting a file.

rm path_or_name_of_hard_link

What about deleting the linked file?

I can’t think of a scenario where you would want to delete the original file while removing the soft link automatically.

Well, you can follow a symbolic link to get to the original file and use it to remove the file.

rm "$(readlink '/path/to/link')" /path/to/link 

Conclusion

While the unlink command exists, I recommend using the rm command for deleting symbolic links. You are already familiar with it and use this command for removing files. Use it for links as well. It’s one less command to remember.

Источник

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