Using Symbolic Links


The cyan files in the /bin listing represent another very common file type in Linux called a symbolic link. A symbolic link allows a single given file to appear in many places or under many names at once, without taking up any additional space on your hard drive. A symbolic link is often used to conserve disk space; other times, it's used simply for convenience. When a symbolic link points to an executable file, it enables the command to take on several identities or behaviors at once, depending on whether you type the name of the original command to use it or the name of the symbolic link.

Nearly anything done to a symbolic link also affects the original file, with the notable exception of removal. Using rm to remove a symbolic link does not remove the original file; instead, using rm with a symbolic link removes the link itself. To get a little experience with symbolic links, let's create one now. In your home directory, create a directory called green and an empty file within it called color.txt:

 [you@workstation20 ~]$ mkdir green [you@workstation20 ~]$ touch green/color.txt [you@workstation20 ~]$ ls green color.txt [you@workstation20 ~]$ 

Now use the file linking command, ln, to create a symbolic link called blue that points to the green directory. To do this, supply the -s option to ln, followed by the name of the source file as the first argument and the destination link as the second:

 [you@workstation20 ~]$ ln -s green blue [you@workstation20 ~]$ ls Desktop  anotherfile.txt  blue  green [you@workstation20 ~]$ 

To illustrate how the symbolic link behaves, list the files in each directory, both the original green directory and the symbolic link, blue, which points to it:

 [you@workstation20 ~]$ ls green color.txt [you@workstation20 ~]$ ls blue color.txt [you@workstation20 ~]$ 

You can see that the symbolic link, blue, behaves very much as the original directory, green, does. You can even make blue your present working directory:

 [you@workstation20 ~]$ cd blue [you@workstation20 blue]$ pwd /home/you/blue [you@workstation20 blue]$ ls color.txt [you@workstation20 blue]$ cd .. [you@workstation20 ~]$ 

You can also see and create symbolic links using the file manager. Open a file manager window now, and you should see both the new directory called green and the symbolic link to it called blue in the list of icons, as shown in Figure 7.1.

Figure 7.1. In the new file manager window, you see both the direc tory you cre ated, called green, and the symbolic link you created, called blue. Note the arrow indicating that blue is a symbolic link.


Symbolic links are shown in the file manager with the same type of icon as the file that they point to, along with a small green arrow overlaying the icon, indicating that the icon represents a symbolic link.

To create a symbolic link in the file manager, simply right-click the icon you would like to create a link to. Right-clicking displays the context menu. Then choose Make Link from the context menu, as shown in Figure 7.2, and a new symbolic link icon appears. You are then free to rename or use this link at your discretion; no matter what you call it or how you choose to use it, it still connects to the original file.

Figure 7.2. Choose Make Link from the context menu to create a symbolic link using the file manager.


You won't do much more with symbolic links in this book, but they will come up in your day-to-day work with Linux, so you need to be aware of them and how they behave. As a final note, remember that you can also create symbolic links that point to symbolic links:

 [you@workstation20 ~]$ ln -s blue yellow [you@workstation20 ~]$ ls Desktop  anotherfile.txt  blue  green  yellow [you@workstation20 ~]$ ls yellow color.txt [you@workstation20 ~]$ cd yellow [you@workstation20 yellow]$ pwd /home/you/yellow [you@workstation20 yellow]$ cd .. [you@workstation20 ~]$  



    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

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