next up previous contents
Next: Useful commands Up: Essential commands Previous: more/less

ln

Make links between files. Files/directories located somewhere in the filesystem can be accessed in another location as if they are located in the stated location.

e.g.

  1. Check directory content of /usr/tmp and /var/tmp:

    [yuni@lovely-linux yuni]$ ls -l /usr/tmp/*
    ls: /tmp/xxx/*: No such file or directory
    [yuni@lovely-linux yuni]$ ls -l /var/tmp/*
    ls: /tmp/xxx/*: No such file or directory

    Nothing inside both directories.

  2. Create a file test_linking in /usr/tmp:

    [yuni@lovely-linux yuni]$ cat - > /usr/tmp/test_linking
    Testing, testing, testing...
    ^D
    [yuni@lovely-linux yuni]$ ls -ld /usr/tmp/*
    -rw-------    1 yuni     yuni           29 Jul 13 13:30 /usr/tmp/test_linking

    The file test_linking created in /usr/tmp with file size 29 bytes.

  3. Check /var/tmp:

    [yuni@lovely-linux yuni]$ ls -ld /var/tmp/*
    -rw-------    1 yuni     yuni           29 Jul 13 13:30 /var/tmp/test_linking

    The same file exists in /var/tmp too!!

  4. Why?

    [yuni@lovely-linux yuni]$ ls -l /usr/tmp
    lrwxrwxrwx 1  root  root    10 Feb  6 19:06 /usr/tmp -> ../var/tmp/

    Because /usr/tmp is not a real directory. It is simply a symbolic link to /usr/var/tmp, i.e., /var/tmp.

There are two kinds of links: symbolic link and hard link.

To create symbolic link,

ln -s <target file or directory> <link name>

To create hard link,

ln <target file or directory> <link name>

How:



System Administrator
Thu Jul 26 11:24:05 HKT 2001