Hello,
Please move this if I chose the wrong forum category. This question pertains to Unix and Linux I believe. I google the difference between hard and symbolic/soft links and I understand the difference. What I am trying to find is a real example of a hard link being used in a Operating system.
Basically, a soft link is a special kind of file that points to a different file somewhere on the available (mounted) filesystems. A hard link is a regular file entry in a directory, only that it points to the same inode as a different directory entry, and cannot cross to other file systems.
If you remove the file a soft link points to the link itself becomes invalid. If you remove the file entry that points to the same inode as a different entry (the hard link), the hard link still is fully valid. Also, both entries for an inode share the same access permission (UID, GID, permission bits).
@pludi,
Thanks for the reply. However I understand the differneces. I am just wondering why one would use a hard link? Or is there a example of a hard link that I can view in a shipped O/S version?
First find the inode <number> of the ls command.
ls -lisad /usr/bin/ls
The inode number is in the first column.
Then use "find" to find every file with the same inode <number>.
find /usr/ -xdev -inum <number> -exec ls -lisad {} \;
The "ls" command will behave differently according to the name by which it was called.
Now try the same sequence for the "vi" command and be amazed.
Interesting. Obviously a bad example for that strain of Linux, but still a valid example for mainstream unix.
In unix when the links count for a file is greater than value 1 this signifies a hard link. Possible to try some likely directories (like /bin) and sort by links count to see if there are any:
Hard links are harder to spot since they don't tell you where they all are.
One use I've abused hard links for is renaming a file that's being written to. Since the file still exists through all points in the process, and nothing like cp that reads the file is used, nothing bad happens:
some-long-running-process > hugrfile &
# ...whoops, I wanted hugefile, not hugrfile.
ln hugrfile hugefile
# hugrfile and hugefile are now the same file
rm hugrfile
# Now only hugefile exists. The file has been renamed with no interruption
# to "some-long-running-process"
Another use for hard links would be when you have a file that needs to be edited by several different people who don't all have access to the directory it's in. Create a hard link in a directory they do have access to and voila. A symbolic link would fail here, since whoever opens it would still need access to the real file.
A couple very common examples of hard links being used in an O/S which is not specific to any version:
. is a hard link to the current directory.
.. is a hard link to the parent directory.
In an "empty" directory, "." will have 2 links. The actual directory named file in the parent directory will be one of those links, the other is the dot itself. As you create subdirectories, each of those will get a ".." entry which will be another hard link to the current directory.
Easy example for you to try on your own. Create a direcotry, cd to it, ls -lai, create a direcotry, ls -lai again.