[oclug] Ln command question.

Dave O'Neill doneill at linuxcare.com
Tue Jan 23 12:37:41 EST 2001


On Tue, 23 Jan 2001, Blashy - none - wrote:

> I learned about ln command last night in my UNIX/LInux class.

Well, here's a quick overview of how 'ln' works.

The 'ln' command allows you to create two types of links, symbolic links
and hard links.  By default, 'ln' creates hard links, but if you give it
the -s option, it will create a symbolic link.

The difference between the two is that a symbolic link is an indirect
pointer to the file, while the hard link is a direct pointer to that file.
(There are other differences, too, but they're not as important).  They
work roughly like this:

  Symbolic link:
      [ link name ] ---->  [ file name ] ----> [ file inode ]

  Hard link:
      [ file name ] ---->  [ file inode ]

> ln will create a link to another file which must not exist (it will create
> it automaticaly). Once the link created if you modify one the other will
> also be modified. If you delete one the other will still remain but not be
> accessible.

Right.   Creating a hard link just gives the file a second name.  So, if
you had a file named 'foo', and you ran the command 'ln foo foo-two', you
would have something that looks roughly like:

      foo      -------> +-----------+
                        | file data |
      foo-two  -------> +-----------+

Now, you can delete either name for the file (see 'man 2 unlink'), and the
file will still be accessible via the other name.

> If you use the ln -s command the file will be accessible if you delete one
> of the files it is linked to.

Almost.  With a symbolic link, only the original name points directly to
the file.  The symbolic link points to the original name.  So, if you
delete the symbolic link, you can still access the file via the original
name.  However, if you delete the original name, you can't access the file
via the symbolic link.  For example, run 'ln -s foo foo-three'.  That
gives you:

      foo-three  ----> foo ---->  +-----------+
                                  | file data |
                                  +-----------+

Now, deleting foo-three means that the file data is still accessible via
foo.  Deleting foo, however means that you're left with no way to access
the file data.

Hope that helps!

Dave
-- 
Dave O'Neill,    Senior Linux Consultant,     Linuxcare, Inc.
desk: (613) 562-9949 fax: (613) 562-9700 cell: (613) 223-0219
doneill at linuxcare.com               http://www.linuxcare.com/




More information about the OCLUG mailing list