6.8. Objective 7: Create and Change Hard and Symbolic LinksOften it is useful to have access to a file in multiple locations in a filesystem. To avoid creating multiple copies of the file, use a link. Links don't take up very much space, as they only add a bit of metadata to the filesystem, so they're much more efficient than using separate copies. There are two types of links used on Linux:
6.8.1. Why Links?To see an example of the use of links in practice, consider the directories in /etc/rc.d on a typical Red Hat system: drwxr-xr-x 2 root root 1024 Dec 15 23:05 init.d -rwxr-xr-x 1 root root 2722 Apr 15 1999 rc -rwxr-xr-x 1 root root 693 Aug 17 1998 rc.local -rwxr-xr-x 1 root root 9822 Apr 13 1999 rc.sysinit drwxr-xr-x 2 root root 1024 Dec 2 09:41 rc0.d drwxr-xr-x 2 root root 1024 Dec 2 09:41 rc1.d drwxr-xr-x 2 root root 1024 Dec 24 15:15 rc2.d drwxr-xr-x 2 root root 1024 Dec 24 15:15 rc3.d drwxr-xr-x 2 root root 1024 Dec 24 15:16 rc4.d drwxr-xr-x 2 root root 1024 Dec 24 15:16 rc5.d drwxr-xr-x 2 root root 1024 Dec 14 23:37 rc6.d Inside init.d are scripts to start and stop many of the services on your system, such as httpd, crond, and syslogd. Some of these files are to be executed with a start argument, while others are run with a stop argument, depending on the runlevel of your system. To determine just which files are run and what argument they receive, a scheme of additional directories has been devised. These directories are named rc0.d through rc6.d, one for each runlevel (see for a complete description of this scheme). Each of the runlevel-specific directories contains several links , each with a name that helps determine the configuration of services on your system. For example, rc3.d contains the following links, among many others: S30syslog -> ../init.d/syslog S40crond -> ../init.d/crond S85httpd -> ../init.d/httpd All of these links point back to the scripts in init.d as indicated by the arrows (->) after the script name. If these links were copies of the scripts, editing would be required for all of the runlevel-specific versions of the same script just to make a single change. Instead, links allow us to:
As another example, consider the directory for the kernel source, /lib/modules/kernel_version/build: build -> /usr/src/linux-2.4.18 Makefiles and other automated tools for building third-party kernel modules can refer to /lib/modules/'uname -r'/build, but in reality they reference /usr/src/linux-2.4.18. If a new kernel is added, say version 2.4.20, its source would be placed into an appropriately named directory and the build link in the new modules directory would be set, as follows: build -> /usr/src/linux-2.4.20 Now the appropriate directory can be selected simply by changing the link. No files need to be moved or deleted. Once created, links are normal directory entries, which may be copied, renamed, deleted, and backed up. Symbolic and hard links are created with the ln command.
6.8.1.1. Preserving linksPrograms such as tar and cp contain options that control whether symbolic links are followed during operation. In the case of a tar backup, this may be important if you have multiple links to large files, because you would get many redundant backups of the same data. When a symbolic link is encountered with cp, the contents of the file to which the link points are copied unless the -d option is specified. This "no dereference" operator causes cp to copy the links themselves instead. For example, consider a directory dir1 containing a symbolic link, which is recursively copied to other directories with and without the -d option: # ls -l dir1 total 13 lrwxrwxrwx 1 root root 19 Jan 4 02:43 file1 -> /file1 -rw-r--r-- 1 root root 10240 Dec 12 17:12 file2 # cp -r dir1 dir2 # ls -l dir2 total 3117 -rw-r--r-- 1 root root 3164160 Jan 4 02:43 file1 -rw-r--r-- 1 root root 10240 Jan 4 02:43 file2 # cp -rd dir1 dir3 # ls -l dir3 total 13 lrwxrwxrwx 1 root root 19 Jan 4 02:43 file1 -> /file1 -rw-r--r-- 1 root root 10240 Jan 4 02:43 file2 Directory dir2 has a copy of the entire file1, which is large, probably wasting disk space. Directory dir3, created with cp -rd, is the same as dir1 (including the symbolic link) and takes very little space. 6.8.1.2. Finding links to a fileFinding the file pointed to by a symbolic link is simple. The ls -l command displays a convenient pointer notation, indicating just where links are pointing: lrwxrwxrwx 1 root root 19 Jan 4 02:43 file1 -> /file1 Going the other way and finding symbolic links to a file is less obvious but is still relatively easy. The -lname option to the find utility locates them for you by searching for symbolic links containing the original filename. Here, the entire local filesystem is searched for myfile, turning up three symbolic links: # find / -lname myfile /home/world/rootsfile /home/finance/hisfile /root/myslink Remember that symbolic links could be anywhere, including being located on a remote system (if you're sharing files), so you may not be able to locate them all. (See "Objective 3: Perform Basic File Management in Chapter 5 for additional information on the find command). Since hard links aren't really links but duplicate directory entries, you can locate them by searching directory entries for the inode, which is identical in all the links. Unlike symbolic links, you are guaranteed to find all of the links since hard links cannot cross filesystem boundaries. First, identify the inode you're interested in, as well as the filesystem that contains the links: # df file1 Filesystem 1k-blocks Used Available Use% Mounted on /dev/sda9 1981000 451115 1427473 24% /home # ls -i file 90469 file1 file1 is on the /home filesystem, and its inode number is 90469. Next, find is used with the -inum option to locate all instances of inode 90469: # find /home -inum 90469 /home/world/file1 /home/finance/file1 /home/jdoe/private/.myfile1 This example turns up three links to file1, including one that user jdoe appears to be hiding!
|