Saturday, December 5, 2009

LinuxInterview: Difference between Hard Link and Soft Link? - Creative Understanding

There's an enormous difference, actually. There's really no such thing as a "hard link". If anything, there is an action that you can call "creating a hard link".

The thing is, a file can have several names. If you create a hard link (by running `ln' without `-s'), you simply create a new name which points to the exact same file as the original name. For example, if you run the following commands:

Code:
cd /tmp
echo test >file1
ln file1 file2
Then you will have two filenames, /tmp/file1 and /tmp/file2, which both point to the same actual file. Notice the important distinction between a file and its name(s): A name knows which file it points to, but a file doesn't know what name(s) it has.

The reason that I say that there's no such thing as a hard link is because the names file1 and file2 are equivalent. There's no "original" name. You can in no way tell that "file2 is a hard link to file1", or the opposite, or anything evem similar -- they are simply two different names for the same file.

An interesting implication of this is that the `rm' command doesn't actually remove files. Instead, it only removes filenames (the system call that it uses is called `unlink', not `remove' or `delete'). The actual file is only automatically deleted by the system when it has no more names. Thus, if you delete file1, file2 will still points to the same file, and conversely, if you delete file2, file1 will still be pointing to the same file.

A symlink, on the other hand, is a file in its own right. It contains the filename of the file that it points to. If file2 were a symlink to file1, and you deleted file1, the file2 symlink would be "broken" (since the filename that it points to no longer exists). In this case, file1 is the name of the actual, original file, and file2 is just a symlink that points to it by name.

For bonus points: What would be the difference in output from the cat command in the following two sequences of commands, and why?

Sequence 1:

Code:
cd /tmp
echo 1 >file1
ln file1 file2
rm file1
echo 2 >file1
cat file2
rm file1 file2
Sequence 2:

Code:
cd /tmp
echo 1 >file1
ln -s file1 file2
rm file1
echo 2 >file1
cat file2
rm file1 file2

No comments:

Post a Comment