Flashcards in Hard and Soft Links Deck (27):
The In command creates two...?
distinct types of links.
Hard links assign multiple...?
dentries (filenames) to a single inode.
Soft links are ...?
distinct inodes that reference other filenames.
Linux users want the same file to exist two separate places, or have two different names. One approach is by...?
creating a hard link.
(Suppose elvis and blondie are collaborating on a duet. They would both like to be able to work on the lyrics as time allows, and be able to benefit from one another's work. Rather than copying an updated file to one another every time they make a change, and keeping their individual copies synchronized, they decide to create a hard link.)
(Blondie has set up a collaboration directory called ~/music, which is group owned and writable by members of the group music. She has elvis do the same. She then creates the file ~/music/duet.txt, chgrps it to the group music, and uses the ln command to link the file into elvis's directory.)
(Because the file was linked, and not copied, it is the same file under two names. When elvis edits /home/elvis/music/duet.txt, he is editing /home/blondie/music/duet.txt as well.)
How are hard links implemented?
When created, the file /home/blondie/music/duet.txt consists of a dentry, an inode, and data, as illustrated below.
See Figure 2.1. Regular File and Figure 2.2. Hardlink and Figure 2.3. Hard link after half is removed.
At a low level, the rm command is not said to delete a file, but "unlink" it. A file (meaning the file's inode and data) are automatically deleted from the system when its link count goes to...?
0 (Implying that there are no longer any dentries (filenames) referencing the file).
The other approach to assigning a single file two names is called a ...?
softlink (While superficially similar, soft links are implemented very differently from hard links.
How are soft links implemented?
When created, the file tuesday.txt, like most files, consists of a dentry, an inode, and data, as illustrated below. When the soft link today.txt was created, the soft link (unlike a hard link) really is a new file, with a newly created inode. The link is not a regular file, however, but a symbolic link. Symbolic links, rather than storing actual data, store the name of another file. When the Linux kernel is asked to refer to the symbolic link, the kernel automatically resolves the link by looking up the new filename. The user (or really, the process on behalf of the user) that referred to the symbolic link doesn't know the difference.
Creating Links with the ln Command...?
both hard links and soft links are created with the In command.
Create the link LINK referencing the file TARGET.
ln [OPTION...] TARGET... [DIRECTORY]
Create link(s) to the file(s) TARGET in the directory DIRECTORY.
Clobber existing destination files.
make symbolic (soft) link instead of hard link.
The ln command behaves very similarly to the cp command: if the last argument is a directory, the command creates links in the ...?
Specified directory which refer to (and are identically named) to the preceding arguments. Unlike the cp command, if only one argument is given, the ln command will effectively assume a last argument of ".".
When specifying links, the ln command expects the name of the ...?
original file(s) first, and the name of the link last. Reversing the order doesn't produce the desired results. Again when it doubt, recall the behavior of the cp command.
Soft links are susceptible to a couple of problems that hard links are not. The first is called dangling links... Example:
The symbolic link today.todo still references the file tuesday.todo, but the file tuesday.todo no longer exists! When madonna tries to read the contents of today.todo, she is met with an error.
Figure 2.5 Dangling Links.
The second problem with soft links is that symbolic links are susceptible to its recursive links. In day to day use, recursive links are not nearly as common almost has to be looking for them to create them. Examples:
What if madonna created two symbolic links, link_a, which referenced a file named link_b, and link_b, which references the file link_a, as illustrated in figure 2.6.
When creating soft link to be used, and when should a hard link be used...?
Generally, hard links are more appropriate if both instances of the link have a reasonable use, even if the other instance didn't exist. In the example above, even if blondie decided not to work on the duet and removed her file, elvis could reasonably continue to work. Soft links are generally more appropriate when one file cannot reasonably exist without the other file. In the example above, madonna could not have tasks for "today" if she did not have tasks for "tuesday". These are general guidelines, however, not hard and fast rules.
Sometimes, more practical restrictions make the choice between hard links and soft links easier. The following outlines some of the differences between hard and soft links. Do not be concerned if you do not understand the last two points, they are included for completeness.
Directories may not be ...?
Hard links have no concept of ...?
"original" and "copy". Once a hard link has been created, all instances are treated equally.
Hard links must refer to files in...?
the same filesystem.
Hard links may be shared between..?
Soft links may refer to ...?
Soft links have a concept of "referrer" and "referred". Removing the "referred" file results in...?
a dangling referrer.
Soft links may span....?
Soft links may not refer to files...?
outside of a "chroot"ed directory.