Create a hard link in UNIX

Contributor Icon Contributed by qmchenry Date Icon October 9, 2003  
Tag Icon Tagged: UNIX

A hard link is a reference to a file or directory that appears just like a file or directory, not a link. Hard links only work within a filesystem. In other words, don’t use hard links between mounted filesystems. A hard link is only a reference to the original file, not a copy of the file. If the original file is deleted, the information will be lost.


To create a hard link of the file /export/home/fred/stuff to /var/tmp/thing, use:

ln /export/home/fred/stuff /var/tmp/thing

The syntax for creating a hard link of a directory is the same. To create a hard link of /var/www/html to /var/www/webroot, use:

ln /var/www/html /var/www/webroot

See also: Create a symbolic link in UNIX

Previous recipe | Next recipe |
 

Viewing 4 Comments

    • ^
    • v
    Probably a better way of describing the difference between hard and soft links:

    The UNIX file system stores files in inodes. The filesystem table contains a list of filenames, and the pointer to the inode in which that file exists.

    A hard link creates a second entry in the filesystem table pointing to the same inode of the file. This can be illustrated with the ls command very easily:



    1: # touch /this
    2: # ln /this /root/that
    3: # ls -i /this /root/that
    4: 18 /root/that 18 /this
    5: # rm -f /this
    6: # ls -i /root/that
    7: 18 /root/that



    What did I do above? Here's a play-by-play:

    1: create a file (/this)
    2: hardlink the file (/this) to another file (/root/that)
    3: ask to see the inodes that the files exist in
    4: note that the inode numbers are identical - 18
    5: delete the original file
    6: show me the inode of the other file again
    7: it's still the same.

    In the above example, Inode 18 is occupied only once, and is referenced by two different path/filenames. Removing the first file doesn't in any way affect the second. The actual inode is still in use by the second reference. As soon as you remove all references, the inode is considered unused and can have new data written to it.

    One caveat - hard links cannot span mount points. My own home directory is mounted from another disk, for example, so I cannot hardlink from /this to /home/bofh/that. The other caveat is that you cannot hard link directories.

    Soft links are really pointers to a pointer. ls -i on both files will still show they are pointing at the same inode... but if you remove the original file, then the inode is actually completely dereferenced. The plus-side is that soft links can span mount points and you can soft link directories. The downside is that if you remove the original then the link is broken.
    • ^
    • v
    There is another very cool usage of this found at this place : http://bulma.net/body.phtml?nIdNoticia=2012 . Actually some programs create a different inode from a hardlinked file if you modify the file; for example patch, or vi (if you had previously "set bkc=no") create a new inode when working on these hardlinks.
    So imagine you are compiling a kernel, trying different patchsets. You dont want to make many "real" copies of the same files, as it takes lot of space and time to do so. Then you could just do hardlinks to the files. And then, only the modified files will actually exist:

    cd /usr/src/
    cp -rl linux-clean linux-experiment

    then you will see, the new folder takes lot less space than the original one but is however fully funcional :) you can compile, apply patches, etc....
    • ^
    • v
    I have a hard link in Windows Explorer on XP home edition. How do I delete it? (In laymen's terms)
    • ^
    • v
    Removing the original file will not destroy the linked file, and therefore the information is not lost. The contents only disappear when all links have been removed
 
close Reblog this comment
blog comments powered by Disqus