Hard Link Examples

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.

e.g.

RedHat uses softlinks from:

lrwxrwxrwx  1 root root   19 Jul 16 11:14 S98haldaemon -> ../init.d/haldaemon
lrwxrwxrwx  1 root root   19 Jul 16 11:14 S99firstboot -> ../init.d/firstboot
lrwxrwxrwx  1 root root   11 Jul 19 15:39 S99local -> ../rc.local
lrwxrwxrwx  1 root root   16 Jul 16 11:12 S99smartd -> ../init.d/smartd
 

I would like to find a hard link example. Can some one show me one please?

Thanks,
jaysunn

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).

2 Likes

@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?

Thanks,

jaysunn

Common example is the "ls" command.

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.

1 Like

Not sure what I am doing wrong or not understanding. I thought I would see more output. I am on RHEL5:

[root@jralph-linux ~]# ls -lisad /bin/ls
15237133 96 -rwxr-xr-x 1 root root 91240 Feb 23 04:54 /bin/ls
[root@jralph-linux ~]# find / -xdev -inum 15237133 -exec ls -lisad {} \;
15237133 96 -rwxr-xr-x 1 root root 91240 Feb 23 04:54 /bin/ls
[root@jralph-linux ~]# ls -lisad /bin/vi
15237163 624 -rwxr-xr-x 1 root root 632912 Jun 12  2009 /bin/vi
[root@jralph-linux ~]# find / -xdev -inum 15237163 -exec ls -lisad {} \;
15237163 624 -rwxr-xr-x 1 root root 632912 Jun 12  2009 /bin/vi
[root@jralph-linux ~]# 

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:

ls -la|grep \^\-|sort -n -r +1|more 
1 Like

Try this on RHEL5

ls -al /bin/ed /bin/red

You should see that the link count is 2

1 Like

@fpmurphy,

Thanks for the reply. However this appears to be a symbolic soft link. I am in search of a hard link example on RHEL.

[root@jralph-linux ~]# ls -ali /bin/ed /bin/red
15237230 -rwxr-xr-x 1 root root 49344 Oct 10  2008 /bin/ed
15237134 lrwxrwxrwx 1 root root     2 Jul 16 11:09 /bin/red -> ed

jaysunn

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.

1 Like

OK, fired up my old RHEL5.3 harddisk and had a look for hard links. The following hard links exists in /usr/bin

-rwxr-xr-x  2 root root       20044 Jan  5  2009 iconvconfig
-rwxr-xr-x  2 root root       20044 Jan  5  2009 iconvconfig.i686
1 Like

Awesome,
Thanks so much for all the great answers. I understand hard links fully now.

jaysunn

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.