Sunday, August 24, 2014

Unix - Symbolic links

A symbolic link or soft link is very much like a shortcut in Windows. Unlike a hard link, a symbolic link does not contain the data in the target file, and instead points to another file in the system.

  • Let's create a hard link of new directory as the source file
    # mkdir /usr/test

  • Let's create a symbolic link  
    # ln -s /usr/test/ /test        ( # ln –s sourceFile symbolicLinkFile )

  • # ls –al /

    lrwxr-xr-x   1 root  wheel       10 Sep 25 11:18     test -> /usr/test/


  • # touch /usr/test/sample.txt

  • # ls /test
    sample.txt

    Note: sample.txt IS listed.

  • # ls -al /test
    lrwxr-xr-x  1 root  wheel  10 Sep 25 11:18 /test -> /usr/test/

    Note: sample.txt IS NOT listed during detailed listing.

  • # ls -al /usr/test/
    total 6
    drwxr-xr-x   2 root  wheel  512 Sep 25 11:21 .
    drwxr-xr-x  18 root  wheel  512 Sep 25 11:18 ..
    -rwxr-xr-x   1 root  wheel   61 Sep 25 11:22 sample.txt


  • # touch /test/sample2.txt

  • # ls -al /test
    lrwxr-xr-x  1 root  wheel  10 Sep 25 11:18 /test -> /usr/test/


    Note: sample2.txt is NOT listed either.

  • # ls /test
    sample.txt       sample2.txt


  • # rm /test

    Note: Symbolic link is deleted without any complain about files within directory

  • # ls /usr/test/
    sample.txt       sample2.txt


  • # rm /usr/test
    rm: /usr/test: is a directory