Difference between revisions of "Tip 20: Hard/Soft Links"

From Vlsiwiki
Jump to: navigation, search
(Created page with "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 inductor:foo mrg$ ls -al total 16 drwxr-xr-x 4 ...")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Create a soft link:
 
Create a soft link:
inductor:foo mrg$ ln -s ../test.sp test2.sp
+
inductor:foo mrg$ ln -s ../test.sp test2.sp
Create a hard link:
+
inductor:foo mrg$ ln ../test.sp test3.sp
+
  
inductor:foo mrg$ ls -al
+
Create a hard link:
total 16
+
inductor:foo mrg$ ln ../test.sp test3.sp
drwxr-xr-x  4 mrg  staff  136 Aug 18 16:36 .
+
 
drwxr-xr-x+ 97 mrg  staff  3298 Aug 18 16:31 ..
+
Now look at the directory:
lrwxr-xr-x  1 mrg  staff    10 Aug 18 16:36 test2.sp -> ../test.sp
+
inductor:foo mrg$ ls -al
-rw-r--r--  2 mrg  staff  815 Jul 17 12:44 test3.sp
+
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:
 
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
+
inductor:foo mrg$ ls -al ../test.sp
-rw-r--r--  2 mrg  staff  815 Jul 17 12:44 ../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.
 
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:
 
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
+
inductor:foo mrg$ ls -li ../test.sp
18906739 -rw-r--r--  2 mrg  staff  815 Jul 17 12:44 ../test.sp
+
18906739 -rw-r--r--  2 mrg  staff  815 Jul 17 12:44 ../test.sp
inductor:foo mrg$ ls -li test3.sp
+
inductor:foo mrg$ ls -li test3.sp
18906739 -rw-r--r--  2 mrg  staff  815 Jul 17 12:44 test3.sp
+
18906739 -rw-r--r--  2 mrg  staff  815 Jul 17 12:44 test3.sp
inductor:foo mrg$ ls -li test2.sp
+
inductor:foo mrg$ ls -li test2.sp
19237135 lrwxr-xr-x  1 mrg  staff  10 Aug 18 16:36 test2.sp -> ../test.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.
 
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?
 
So, why use hard links? Well, what if you remove the original file?
  
inductor:foo mrg$ rm ../test.sp
+
inductor:foo mrg$ rm ../test.sp
inductor:foo mrg$ ls -al
+
inductor:foo mrg$ ls -al
total 16
+
total 16
drwxr-xr-x  4 mrg  staff  136 Aug 18 16:36 .
+
drwxr-xr-x  4 mrg  staff  136 Aug 18 16:36 .
drwxr-xr-x+ 96 mrg  staff  3264 Aug 18 16:39 ..
+
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
+
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
+
-rw-r--r--  1 mrg  staff  815 Jul 17 12:44 test3.sp
inductor:foo mrg$ more test2.sp
+
inductor:foo mrg$ more test2.sp
test2.sp: No such file or directory
+
test2.sp: No such file or directory
inductor:foo mrg$ more test3.sp
+
inductor:foo mrg$ more test3.sp
* File: invx1.pex.netlist
+
  * 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.
 
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.
 
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.

Latest revision as of 23:45, 18 August 2014

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.