Create a hard link in UNIX

Posted October 9, 2003 by Quinn McHenry in 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

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

  • Anonymous

    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

  • BnB

    I agree with karelc above.

    This is a totally inaccurate title, and douches like you should pick up a linux book and get a hold of your basics, before being allowed to post articles on the web.

  • BnB

    I agree with karelc above.

    This is a totally inaccurate article, and douches like you should pick up a linux book and get a hold of your basics, before being allowed to post articles on the web.

  • juhi

    hey plz tell me the advantages of hard link…why is this useful?

  • Bernard Valentin

    Hard links allow a file to exist in several folders while not being duplicated on disk (this is the reason hard links cannot be used across file systems). Actually, one can consider that any file is “hard linked” at least ones, in the folder it has been created. The OS and the applications do not make any distinction between the original location and the “hard linked” ones.
    E.g. if you store a file in folder A and create a hard link to the file in folder B, the file will be seen by any application as existing at both places (which is not always the case with symbolic links). If you delete the file from folder A, it will still “exist” in folder B, exactly as if it has been created there at the first place. Creating hard links is thus a way to prevent losing data by deleting files by accident.
    Symbolic links, on the other hand, do not prevent losing data: they are small data structures that contain the path (possibly across file systems) and name of the target file. If the target file is deleted, the data is lost and the symbolic link is “broken”.
    If you want to go deeper, read how inodes are used in Un*x file systems.