Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we create a hard link to a symbolic link in UNIX? [closed]

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 ?

like image 525
Gagan Avatar asked Oct 27 '15 06:10

Gagan


People also ask

Is a symbolic link a hard 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.

How do you create a hard link in Unix?

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.

Is a symbolic link a soft 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.

Why hard link is not allowed in directory?

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.


2 Answers

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
like image 96
Mark Harrison Avatar answered Nov 15 '22 10:11

Mark Harrison


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.

like image 24
Jonathan Leffler Avatar answered Nov 15 '22 12:11

Jonathan Leffler