answersLogoWhite

0

AllQ&AStudy Guides
Best answer
Linux reference-counts each file and will only reclaim its blocks when the reference-count goes to zero, that is, when the last hard link is severed.

Consider this example:
$ date > foo
$ rm foo


Typically, you create a file, and it has a ref-count of 1 for its entire lifetime, and eventually we delete the directory entry, decrement the ref-count, and reclaim the inode and data blocks.

Now consider this example:
$ date > foo1
$ ln foo1 foo2
$ rm foo1
$ cat foo2

In this case, the 1st name disappears, but the original data are still available via the 2nd name. This will continue to be true even if we add and delete many names, and even if we move those names up and down the directory hierarchy within the same filesystem, as long as at least one reference remains. Deleting that last name will reclaim the inode and data blocks, too. If you are familiar with RDBMs, you might think of it in terms of referential integrity.


Soft links, on the other hand, have very little to do with all that. Here are two commands which bear some similarity to each other:

$ echo 'Go look at foo2' > bar
$ ln -s foo2 baz

The first offers human-readable instructions, while the second phrases those instructions in terms the filesystem understands. In both cases, if someone rm's foo2 we won't know about it until we "cat baz" or consult bar and notice that foo2 has been pulled out from under us. Symlinks are simple. There is no ref-counting and no guarantees of integrity when it comes to symlinks. That said, they are still enormously useful. Note that symlinks can cross filesystems, a feat which hard links were not designed to handle.
This answer is:
Related answers
Linux reference-counts each file and will only reclaim its blocks when the reference-count goes to zero, that is, when the last hard link is severed.

Consider this example:
$ date > foo
$ rm foo


Typically, you create a file, and it has a ref-count of 1 for its entire lifetime, and eventually we delete the directory entry, decrement the ref-count, and reclaim the inode and data blocks.

Now consider this example:
$ date > foo1
$ ln foo1 foo2
$ rm foo1
$ cat foo2

In this case, the 1st name disappears, but the original data are still available via the 2nd name. This will continue to be true even if we add and delete many names, and even if we move those names up and down the directory hierarchy within the same filesystem, as long as at least one reference remains. Deleting that last name will reclaim the inode and data blocks, too. If you are familiar with RDBMs, you might think of it in terms of referential integrity.


Soft links, on the other hand, have very little to do with all that. Here are two commands which bear some similarity to each other:

$ echo 'Go look at foo2' > bar
$ ln -s foo2 baz

The first offers human-readable instructions, while the second phrases those instructions in terms the filesystem understands. In both cases, if someone rm's foo2 we won't know about it until we "cat baz" or consult bar and notice that foo2 has been pulled out from under us. Symlinks are simple. There is no ref-counting and no guarantees of integrity when it comes to symlinks. That said, they are still enormously useful. Note that symlinks can cross filesystems, a feat which hard links were not designed to handle.
View page

Hard Link: Two actual copies of the same program. Make changes to one and the other reflects the same changes. Make changes to the latter and the former changes. Both files take up space on disk. Use this if you want to mirror some of your files. I don't think it is possible to do this with directories, only files.

Symbolic Link: Only one true copy of the file on disk. The symlink(s) which point(s) to it are simply pointers which know the address of the true copy. Any changes to the symlink changes the true copy and any changes to the true copy changes the true copy. So only the true copy gets modified, but it may be modified through any of the symlinks which are linked to it. So you need to take care if you modify any of the symlinks because the true copy will reflect those changes and then in turn so will all of the symlinks. Possible with directories.

View page
Featured study guide
📓
See all Study Guides
✍️
Create a Study Guide
Search results