Exploring Your Linux System

Symbolic Links

Due to the inherent complexity of the Linux file system, many files are either the target of links, or links themselves. One example of this is the file /dev/cdrom. This file is always linked at boot to the file that represents you actual CD-ROM hardware. This might be /dev/hdb, /dev/hdc, or something else, depending on your configuration. By linking files, it allows administrators and developers to have some consistency throughout distributions. Conceptually a symbolic link is similar to a Windows shortcut.

Symbolic links come in two forms; Hard and Soft. A soft link functions as a shortcut to the file. Any commands which reference the link, affect the actual file, and only one copy of the file is maintained on the hard drive. Soft links are not the default type of link and must be explicitly created. When a hard link is created, two copies of the file exist in two separate locations. Changes to one version of the file are replicated to the other version immediately. It is important to note that there is no “master” copy of the file. Both copies are considered equal, and no changes are made to the way the files are displayed, other than to increment each files link counter from 1 to 2. Hard links are very useful to administrators that wish to improve the performance of key files, or to provide hot backups of key files. It is a common practice to hard link any key configuration files to a separate disk, to provide protection from a primary disk failure

Create a hard link by entering [ln realfile linkedfile]. Create a soft link by entering [ln –s realfile linkedfile]. Soft links will be displayed in the output of an [ls] command in a alternate color, and in the output of an [ls –l] command with the name of the target file as part of the file’s displayed name. Note the files FirstFile, HardLink and SoftLink. Note the incremented link counter on FirstFile and HardLink, as well as the file type of l on SoftLink.
[root@linuxbox root]# ll

total 28
-rw-r--r-- 1 root root 1065 Feb 10 16:13 anaconda-ks.cfg
-rw-r--r-- 2 root root 0 Apr 2 19:33 FirstFile
-rw-r--r-- 2 root root 0 Apr 2 19:33 HardLink
-rw-r--r-- 1 root root 70 Feb 27 18:39 logfile
drwx------ 2 root root 4096 Feb 28 19:19 mail
-rw------- 1 root root 10629 Feb 28 19:19 mbox
lrwxrwxrwx 1 root root 9 Apr 2 19:34 SoftLink -> FirstFile
-rwxr--r-- 1 root root 21 Mar 6 13:44 testscript
[root@linuxbox root]#