Hard Link - Example

Example

In the figure to the right, two hard links, named "LINK A.TXT" and "LINK B.TXT", point to the same physical data.

If the file "LINK A.TXT" is opened in an editor, modified and saved, then those changes will be visible if the file "LINK B.TXT" is then opened for viewing since both filenames point to the same data ("opened," because, on POSIX systems, an associated file descriptor remains valid after opening, even when the original file is moved). The same is true if the file were opened as "LINK B.TXT" - or any other name associated with the data.

Some editors however break the hard link concept, e.g. emacs. When opening a file "LINK B.TXT" for editing, emacs first renames "LINK B.TXT" to "LINK B.TXT~", loads "LINK B.TXT~" into the editor, and saves the modified contents to a newly created "LINK B.TXT". Using this approach, the two hard links are now "LINK A.TXT" and "LINK B.TXT~" (the backup file); "LINK B.TXT" would now have just one link and no longer shares the same data as "LINK A.TXT". (This behavior can be changed using the emacs variable backup-by-copying)

Any number of hard links to the physical data may be created. To access the data, a user only needs to specify the name of any existing link; the operating system will resolve the location of the actual data.

If one of the links is removed with the POSIX unlink function (for example, with the UNIX rm command), then the data are still accessible through any other link that remains. If all of the links are removed and no process has the file open, then the space occupied by the data is freed, allowing it to be reused in the future. This semantic allows for deleting open files without affecting the process that uses them. This technique is commonly used to ensure that temporary files are deleted automatically on program termination, including the case of abnormal termination.

Read more about this topic:  Hard Link

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)