Tip 20: Hard/Soft Links

From Vlsiwiki
Revision as of 23:45, 18 August 2014 by Mrg (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Create a soft link:

inductor:foo mrg$ ln -s ../test.sp test2.sp

Create a hard link:

inductor:foo mrg$ ln ../test.sp test3.sp

Now look at the directory:

inductor:foo mrg$ ls -al
total 16
drwxr-xr-x   4 mrg  staff   136 Aug 18 16:36 .
drwxr-xr-x+ 97 mrg  staff  3298 Aug 18 16:31 ..
lrwxr-xr-x   1 mrg  staff    10 Aug 18 16:36 test2.sp -> ../test.sp
-rw-r--r--   2 mrg  staff   815 Jul 17 12:44 test3.sp

Note that the soft link is designated with a "->" while the hard link just looks like a file. However, the hard link is actually the SAME FILE:

inductor:foo mrg$ ls -al ../test.sp
-rw-r--r--  2 mrg  staff  815 Jul 17 12:44 ../test.sp

which shows the same date, permissions, etc as the link test3.sp. You can confirm this by seeing what the filesystem inode is for each of these using the -i options to ls:

inductor:foo mrg$ ls -li ../test.sp
18906739 -rw-r--r--  2 mrg  staff  815 Jul 17 12:44 ../test.sp
inductor:foo mrg$ ls -li test3.sp
18906739 -rw-r--r--  2 mrg  staff  815 Jul 17 12:44 test3.sp
inductor:foo mrg$ ls -li test2.sp
19237135 lrwxr-xr-x  1 mrg  staff  10 Aug 18 16:36 test2.sp -> ../test.sp

The first two are identical where as the symbolic link is a separate inode.

So, why use hard links? Well, what if you remove the original file?

inductor:foo mrg$ rm ../test.sp
inductor:foo mrg$ ls -al
total 16
drwxr-xr-x   4 mrg  staff   136 Aug 18 16:36 .
drwxr-xr-x+ 96 mrg  staff  3264 Aug 18 16:39 ..
lrwxr-xr-x   1 mrg  staff    10 Aug 18 16:36 test2.sp -> ../test.sp
-rw-r--r--   1 mrg  staff   815 Jul 17 12:44 test3.sp
inductor:foo mrg$ more test2.sp
test2.sp: No such file or directory
inductor:foo mrg$ more test3.sp
 * File: invx1.pex.netlist
...

The soft link is pointing to nothing now. The hard link still has the file. Files are only "removed" when there are no more hard links to a file.

One example where I use hard links is when I do backups. You can tell rsync, for example, to use hard links rather than make new copies when files don't change. That's beyond the scope of this though.