LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to delete a symbolic link (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-a-symbolic-link-190542/)

aroop 06-06-2004 11:54 PM

How to delete a symbolic link
 
I have made a symbolic link by mistake. How to delete it?

rm -rf doesnt work.

Thanks,

Bruce Hill 06-07-2004 12:21 AM

Re: How to delete a symbolic link
 
Quote:

Originally posted by aroop
I have made a symbolic link by mistake. How to delete it?

rm -rf doesnt work.

Thanks,

You only need issue
bash-2.05b# rm <name of link>

sh-2.05b$ man symlink
SEE ALSO
readlink(2), link(2), unlink(2), rename(2), open(2), lstat(2), ln(1)
bash-2.05b$ man ln
Quote:

A soft link (or symbolic link, or symlink) is an entirely
different animal: it is a small special file that contains
a pathname. Thus, soft links can point at files on dif-
ferent filesystems (possibly NFS mounted from different
machines), and need not point to actually existing files.
When accessed (with the open(2) or stat(2) system calls),
a reference to a symlink is replaced by the operating sys-
tem kernel with a reference to the file named by the path
name. (However, with rm(1) and unlink(2) the link itself
is removed, not the file it points to. There are special
system calls lstat(2) and readlink(2) that read the status
of a symlink and the filename it points to. For various
other system calls there is some uncertainty and variation
between operating systems as to whether the operation acts
on the symlink itself, or on the file pointed to.)
And an example from this morning ->
Code:

bash-2.05b# ls -alc /usr/lib | less
drwxr-xr-x    9 root    root          408 Jun  3 22:29 j2sdk1.4.2_01
drwxrwxr-x    9 root    root          408 Jun  7 07:06 j2sdk1.4.2_04
lrwxrwxrwx    1 root    root          23 Jun  7 07:21 java -> /usr/lib/j2sdk1.4.2_01/
bash-2.05b# rm java

and to make the new link

Code:

bash-2.05b# ln -s /usr/lib/j2sdk1.4.2_04/ /usr/lib/java

adz 06-07-2004 12:44 AM

What happens when you just do an rm <filename>? Or your command?

aroop 06-07-2004 02:01 AM

Hi,

The symbolic link was to a directory. (I forgot to mention this earlier)

I am using RHL 9. Have a look at the following messages, if this helps.


[root@linux2 html]# ls -l
total 8
lrwxrwxrwx 1 root root 25 Jun 7 10:42 elixir -> /home/elixir/p
ublic_html/
drwxr-xr-x 2 root root 4096 May 3 17:43 mrtg
lrwxrwxrwx 1 root root 25 Jun 7 10:09 public_html -> /home/eli
xir/public_html/
drwxr-xr-x 2 webalizer root 4096 Jun 2 04:02 usage
[root@linux2 html]# rm public_html/
rm: remove directory `public_html/'? y
rm: cannot remove directory `public_html/': Is a directory
[root@linux2 html]#

Bruce Hill 06-07-2004 02:42 AM

rm elixir
 
Quote:

Originally posted by aroop
Hi,

The symbolic link was to a directory. (I forgot to mention this earlier)

I am using RHL 9. Have a look at the following messages, if this helps.


Code:

[root@linux2 html]# ls -l
total 8
lrwxrwxrwx    1 root    root          25 Jun  7 10:42 elixir -> /home/elixir/public_html/
drwxr-xr-x    2 root    root        4096 May  3 17:43 mrtg
lrwxrwxrwx    1 root    root          25 Jun  7 10:09 public_html -> /home/elixir/public_html/
drwxr-xr-x    2 webalizer root        4096 Jun  2 04:02 usage
[root@linux2 html]# rm public_html/
rm: remove directory `public_html/'? y
rm: cannot remove directory `public_html/': Is a directory
[root@linux2 html]#

As in my previous example, the actual symbolic link is elixir. So issue
[root@linux2 html]# rm elixir
and you're done.

Then if you want to delete your public_html directory, issue
[root@linux2 html]# rm -rf /home/elixir/public_html
and it will delete not only that directory, but all the files under it. The switches
after the rm command are r for recursive and f for force.

When you issued
[root@linux2 html]# rm public_html/
the system tried to help you by not letting you delete a dir that contained files.

Just issue
[root@linux2 html]# rm elixir
and you've deleted the symlink, but you'll still have your dir and all your files.

Issue and read
$ man rm
rm - remove files or directories

stuNNed 06-07-2004 03:00 AM

unlink link

aroop 06-07-2004 03:29 AM

There are two symbolic links in the directory: public_html and elixir.

I want to delete the public_html one.

I have a feeling that rm does not allow deleting symbolic links to directories in this distribution. Even unlink doesnt work.

motub 06-07-2004 03:35 AM

Are you doing this as root? I notice, in reading man ln that only root is allowed to make hard links to directories. Maybe only root is allowed to remove them, as well.

aroop 06-07-2004 03:41 AM

Its a soft link, not a hard one!!

motub 06-07-2004 04:02 AM

Question still stands, though, slightly altered and expanded:

Who owns this link (or rather, who owns the directory that the link points to, as the permissions will be the same)? Does this user have write permissions to the link/directory, and is the user with such write permissions the one who is trying to write to the link by deleting it?

aroop 06-07-2004 04:06 AM

I am working as root, and I believe root has privileges to all the directories.

motub 06-07-2004 04:10 AM

Root does not necessarily have full permissions to all directories. If the program that created the directory prefers that the directory not be writeable, even root may not be given write permissions unless they are explicitly set. So maybe you should look at the actual properties of the link.

Just an idea.

aroop 06-07-2004 04:12 AM

I dont want to delete the folder, only the link.

motub 06-07-2004 04:30 AM

I understand that. But the permissions for the link propagate from the permissions for the folder. So if you couldn't delete the folder (because you had no write permissions to do so), you won't be able to delete the link either.

Bruce Hill 06-07-2004 05:53 AM

Alright boys and girls, let's settle down. It's not permissions, as he's root,
and both links have lrwxrwxrwx (777). You're both right, but we've all missed
his mistake. Let's revisit his previous post, shall we?

[root@linux2 html]# ls -l
total 8
lrwxrwxrwx 1 root root 25 Jun 7 10:42 elixir -> /home/elixir/public_html/ <--- this is a link
drwxr-xr-x 2 root root 4096 May 3 17:43 mrtg
lrwxrwxrwx 1 root root 25 Jun 7 10:09 public_html -> /home/elixir/public_html/ <--- this is a link
drwxr-xr-x 2 webalizer root 4096 Jun 2 04:02 usage
[root@linux2 html]# rm public_html/ <--- but you asked to delete a directory
rm: remove directory `public_html/'? y
rm: cannot remove directory `public_html/': Is a directory
[root@linux2 html]#

This is giving me a headache, but I think what you've got to do is to descend into
that /home/elixir/public_html/ directory and remove the link from there. What say?
Give that a go, mate, and please post back what happened. I've been riding the
public_bus to get a repaired_hd and a repaired_router and the suspense is just
killing me! Hope you stayed up late aroop.


All times are GMT -5. The time now is 08:35 PM.