6

I created a symbolic link of a file and a hard link of that symbolic link. But instead of hard link symbolic link is being created and inodes of both of them are equal. Is it possible to create a hard link for a symbolic link ?

Gagan
  • 85
  • 1
  • 11

2 Answers2

4

Not reliably — as shown (demonstration on Mac OS X 10.10.5 Yosemite):

$ ls -l ???.c
-rw-r--r--  1 jleffler  staff  688 Oct 26 00:09 ncm.c
$ ln -s ncm.c ppp.c
$ ln ppp.c qqq.c
$ ls -l ???.c
-rw-r--r--  2 jleffler  staff  688 Oct 26 00:09 ncm.c
lrwxr-xr-x  1 jleffler  staff    5 Oct 26 23:58 ppp.c -> ncm.c
-rw-r--r--  2 jleffler  staff  688 Oct 26 00:09 qqq.c
$ 

The link follows the symlink and is created to the file at the end of the symlink, not to the symlink itself. See the POSIX specifications of symlink() and (in particular) link().

Under link(), it says:

If path1 names a symbolic link, it is implementation-defined whether link() follows the symbolic link, or creates a new link to the symbolic link itself.

On Mac OS X, it follows the symbolic link. Other systems may do it differently.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
3

It works on Linux. We use this regularly in some mission-critical code. As Jonathan notes, this is not guaranteed by the Posix standard, so you should double-check that it works for your system.

$ date >a
$ ln -s a b
$ ln b c
$ ls -li
total 4
396074 -rw-rw-r-- 1 ec2-user ec2-user 29 Oct 27 07:15 a
396345 lrwxrwxrwx 2 ec2-user ec2-user  1 Oct 27 07:16 b -> a
396345 lrwxrwxrwx 2 ec2-user ec2-user  1 Oct 27 07:16 c -> a

You can use the --physical flag to enforce this.

   -P, --physical
          make hard links directly to symbolic links
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465