unixsymlinkhardlink

Linking in UNIX


I have a few questions regarding links in UNIX

  1. Can I say Soft links in UNIX are analogous to shortcuts in windows?
  2. Difference between copying and hard-linking?
  3. Can anyone give me a use-case where I should prefer hard-linking over copying?

I'm so messed up right now. Any help is highly appreciated


Solution

    1. I don't known much about shortcuts in windows, but I think it's similar but not the same. A soft link on file system level is basically a textfile with a pathname and a flag marking it as link. It can be any relative or absolute pathname anywhere on the machine. Normally any user process who opens that link is redirected by the kernel to the file it points to and doesn't even 'realize' it. Reading the link as link itself requires special system calls like readlink() When you remove the file a soft link points to, the link remains but now points to 'nowhere' and can't be read anymore.

    2. You can imagine a hard link as a second directory entry that's pointing to the same area on the file system as the 'original file' (more exactly: it points to the same inode that represents the location of the file and meta information like size, owner etc.). Having made a hard link 'original' and 'link' are indisdinguishable and if you change the file via one of the pathnames, you will see the changes via the other pathname as well. That doesn't apply for removing, as long as the link count (another value stored in the inode) is greater then 1 only the directory entry is removed and the link count is decremented. Hard links can only be made within the same file system because every file system has it's own table of inodes.

    3. That follows more or less from 2. If you want to use the special properties of hard links (or just save space in case of huge files) use a hard link otherwise do a copy