How do I create links in unix?



  • Link can be created in two ways hard links and soft links (also called symbolic links)

    Hard links is useful for maintaining single file with multiple names, the following command creates a hard link to a file called bfile, afile already exists, bfile does not

    $ ls -l
    -rw-r--r-- 1 root root 14 Sep 4 19:29 afile
    
    $ ln afile bfile
    
    $ ls -l
    -rw-r--r-- 2 root root 14 Sep 4 19:29 bfile
    

    Notice that the number after the permission, the second column changed from 1 to 2, meaning, it now has two links, two file names, editing either file afile or bfile, the changes will take effect on both file names. Note that hard links can only be created within the same file system and only file can be linked

    Soft links can be created across file systems and can be created on directories too. Soft links can associate a shorter name to a long unix directory path. For example, if I wanted to access the directory /export/home/unixbod/scripts/aix/menus from dritchie's home directory by changing directory to menus, I would create a symbolic link to it, by doing the following:

    $ cd /export/home/dritchie
    
    $ ln -s /export/home/unixbod/scripts/aix/menus menus
    
    $ ls -l
    lrwxrwxrwx 1 root root 38 Sep 4 19:45 menus -> /export/home/unixbod/scripts/aix/menus
    
    $ cd menus
    
    $ touch afile
    
    $ cd /export/home/unixbod/scripts/aix/menus
    
    $ ls -l
    -rw-r--r-- 1 root root 0 Sep 4 19:47 afile
    

    Notice the "l" in front of the ls listing, that means a symbolic link


Log in to reply
 

© Lightnetics 2024