Section 4.9. Hard and Soft Links: ln


[Page 137 (continued)]

4.9. Hard and Soft Links: ln

The ln utility allows you to create both hard links and symbolic (soft) links between files, as described in Figure 4-21.

Figure 4-21. Description of the ln command.

Utility: ln -sF original [ newLink ]

ln -sF { original }+ directory

ln is a utility that allows you to create hard links or symbolic (soft) links to existing files.

To create a hard link between two regular files, specify the existing file label as the original filename and the new file label as newLink. Both labels will then refer to the same physical file, and this arrangement will be reflected in the hard-link count shown by the ls utility. The file can then be accessed via either label, and is removed from the file system only when all of its associated labels are deleted. If newLink is omitted, the last component of original is assumed. If the last argument is the name of a directory, then hard links are made from that directory to all of the specified original filenames. Hard links may not span file systems.

The -s option causes ln to create symbolic links. A symbolic link is a new file that contains a pointer (by name) to another file. A symbolic link may span file systems since there is no explicit connection to the destination file other than the name. Note that if the file pointed to by a symbolic link is removed, the symbolic-link file still exists but will result in an error if accessed.

The -F option allows a super-user to create a hard link to a directory.

For further information about how hard links are represented in the file system, see the discussion of Linux file systems in Chapter 13, "Linux Internals."



[Page 138]

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.


[Page 139]

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. ... $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net