Tuesday, January 27, 2009

Hard links

Hard Links

You can give the same file several names by using the ln command on the same file many times. To set up a hard link, you use the ln command with no -s option and two arguments: the name of the original file and the new, added filename. The ls operation lists both filenames, but only one physical file will exist.

$ ln original-filename added-filename

In the next example, the monday file is given the additional name storm. In this case,
storm is just another name for the monday file.

$ ls
today
$ ln monday storm
$ ls
monday storm
To erase a file that has hard links, you need to remove all its hard links. The name of a file is actually considered a link to that file—hence the command rm removes the link to the file. If you have several links to the file and remove only one of them, the others stay in place and you can reference the file through them. The same is true even if you remove the original link—the original name of the file. Any added links will work just as well. In the next example, the today file is removed with the rm command. However, a link to that same file exists, called weather. The file can then be referenced under the name weather.
$ ln today weather
$ rm today
$ cat weather
The storm broke today
and the sun came out.
$

No comments:

Post a Comment