4.9. Hard and Soft Links: lnThe ln utility allows you to create both hard links and symbolic (soft) links between files, as described in Figure 4-21.
In the following example, I added a new label "hold" to the file referenced by the existing label "hold.3". Note that the hard-link count field incremented from one to two when the hard link was added, and then back to one again when the hard link was deleted: $ ls -lG ...look at the current directory contents. total 3 -rw-r--r-- 1 glass 124 Jan 12 17:32 hold.1 -rw-r--r-- 1 glass 89 Jan 12 17:34 hold.2 -rw-r--r-- 1 glass 91 Jan 12 17:34 hold.3 $ ln hold.3 hold ...create a new hard link. $ ls -lG ...look at the new directory contents. total 4 -rw-r--r-- 2 glass 91 Jan 12 17:34 hold -rw-r--r-- 1 glass 124 Jan 12 17:32 hold.1 -rw-r--r-- 1 glass 89 Jan 12 17:34 hold.2 -rw-r--r-- 2 glass 91 Jan 12 17:34 hold.3 $ rm hold ...remove one of the links. $ ls -lG ...look at the updated directory contents. total 3 -rw-r--r-- 1 glass 124 Jan 12 17:32 hold.1 -rw-r--r-- 1 glass 89 Jan 12 17:34 hold.2 -rw-r--r-- 1 glass 91 Jan 12 17:34 hold.3 $ _ A series of hard links may be added to an existing directory if the directory's name is specified as the last argument to ln. In the following example, I created links in the "tmp" directory to all of the files matched by the pattern "hold.*": $ mkdir tmp ...create a new directory. $ ln hold.* tmp ...create a series of links in "tmp". $ ls -lG tmp ...look at the contents of "tmp". total 3 -rw-r--r-- 2 glass 124 Jan 12 17:32 hold.1 -rw-r--r-- 2 glass 89 Jan 12 17:34 hold.2 -rw-r--r-- 2 glass 91 Jan 12 17:34 hold.3 $ _ A hard link may not be created from a file on one file system to a file on a different file system. To get around this problem, create a symbolic (or soft) link instead. A symbolic link may span file systems. To create a symbolic link, use the -s option to ln. In the following example, I tried to create a hard link from my home directory to the file "/usr/include/stdio.h". Unfortunately, that file was on a different file system, and so ln failed. However, ln with the -s option succeeded. When ls is used with the -F option, symbolic links are followed by the @ character. By default, ls displays the contents of the symbolic link; to obtain information about the file that the link refers to, use the -L option. $ ln /usr/include/stdio.h stdio.h ...hard link. ln: stdio.h: Cross-device link $ ln -s /usr/include/stdio.h stdio.h ...symbolic link. $ ls -lG stdio.h ...examine the file. lrwxrwxrwx 1 glass 20 Jan 12 17:58 stdio.h -> /usr/include/stdio.h $ ls -F ...@ indicates a sym. link. stdio.h@ $ ls -lGL stdio.h ...look at the link itself. -rw-r--r-- 1 root 27839 May 11 07:40 stdio.h $ cat stdio.h ...look at the file. /* Define ISO C stdio on top of C++ iostreams. Copyright (C) 1991,1994-2002,2003 Free Software Foundation This file is part of the GNU C Library. ... $ _ |