For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2007 > What is the difference between unlink and rm?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author What is the difference between unlink and rm?
Bin Chen

2007-04-16, 8:05 am

Both command can delete a file, whats the difference?

Thanks.

Måns Rullgård

2007-04-16, 8:05 am

"Bin Chen" <binary.chen@gmail.com> writes:

> Both command can delete a file, whats the difference?


unlink is a system call, rm is a shell utility that calls unlink.

--
Måns Rullgård
mans@mansr.com
Måns Rullgård

2007-04-16, 8:05 am

Måns Rullgård <mans@mansr.com> writes:

> "Bin Chen" <binary.chen@gmail.com> writes:
>
>
> unlink is a system call, rm is a shell utility that calls unlink.


Ah, now I remember there also (at least in the GNU package) an
"unlink" executable. It appears to be a simple wrapper for the unlink
system call, taking a single file argument only. It's purpose is not
clear to me.

--
Måns Rullgård
mans@mansr.com
Gunvant Patil

2007-04-16, 7:04 pm

On Apr 16, 10:27 am, "Bin Chen" <binary.c...@gmail.com> wrote:
> Both command can delete a file, whats the difference?
>
> Thanks.


In the GNU system `unlink' can never delete the name of a directory.
Also unlink is unfriendly with wild cards.

-Cheers,
Gunvant
~~~~~~~~
No trees were killed in the sending of this message. However a large
number of electrons were terribly inconvenienced.

Barry Margolin

2007-04-16, 10:03 pm

In article <yw1x8xcsd958.fsf@thrashbarg.mansr.com>,
Måns Rullgård <mans@mansr.com> wrote:

> Måns Rullgård <mans@mansr.com> writes:
>
>
> Ah, now I remember there also (at least in the GNU package) an
> "unlink" executable. It appears to be a simple wrapper for the unlink
> system call, taking a single file argument only. It's purpose is not
> clear to me.


On some flavors of Unix, unlink can delete things that rm can't, such as
directories, if you're running as superuser.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Bin Chen

2007-04-16, 10:03 pm

On 4=D4=C216=C8=D5, =CF=C2=CE=E79=CA=B141=B7=D6, "Gunvant Patil" <gunvant.k=
..pa...@gmail.com> wrote:
> On Apr 16, 10:27 am, "Bin Chen" <binary.c...@gmail.com> wrote:
>
>
>
> In the GNU system `unlink' can never delete the name of a directory.
> Also unlink is unfriendly with wild cards.


So the underlying principle is exact the same except some
functionality lost in unlink?

Gordon Burditt

2007-04-17, 4:04 am

>> > Both command can delete a file, whats the difference?
>
>So the underlying principle is exact the same except some
>functionality lost in unlink?


In the ancient past, creating a directory consisted of three steps
(now done with mkdir()):

1. create an empty directory node with mknod().
2. Link (hard) a "." entry to that directory node.
3. Link (hard) a ".." entry to the directory's parent.

Removing a directory consisted of three steps (now done with rmdir(),
which also does some type checks first):

1. unlink() the ".." entry
2. unlink() the "." entry
3. unlink() the directory itself

unlink (run as root) would remove a name entry regardless of its type.

With a combination of link() and unlink() you could do all sorts
of evil things to the directory tree by repointing "." and ".."
entries. You could also orphan subtrees that take disk space but
have no references from the outside.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com