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 ?
Differences between soft and hard links:Symbolic links can be made to files and directories while hard links can only be made between files. Symbolic links can be made between different file systems, hard ones cannot. Hard links share the inode number, symbolic links do not.
To create a hard links on a Linux or Unix-like system: Create hard link between sfile1file and link1file, run: ln sfile1file link1file. To make symbolic links instead of hard links, use: ln -s source link. To verify soft or hard links on Linux, run: ls -l source link.
A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system.
To prevent loops in the filesystem, and to keep the interpretation of the " .. " file (parent directory) consistent, operating systems do not allow hard links to directories. UNIX System V allowed them, but only the superuser had permission to make such links.
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
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 whetherlink()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With