VizagInfo.com Home | VizagInfo Mirror of LG


  Take Command: ln
Submitted by staff on Saturday, November 29, 2003 - 05:55
 

The ln command creates pseudonyms for files which allows them to be accessed by different names. These pseudonyms are called links. There are two different forms of the command and two different kinds of links that can be created. First, let me explain the two forms.

    ln [options] exiting_path [new_path]
    ln [options] exiting_paths directory

In the first form, a new name is created called new_path which is a psuedonym for existing_path. The reason this is called a path is that it can be a full pathname to a file. That is, it does not have to specify a file in the current directory.

In the second form, the last argument is taken to be a directory name and all the other arguments are paths to existing files. A link for each existing file is created in the specified directory with the same filename as the existing files.

Time for a few examples:
Create a link named my_file in the current directory to the file /home/bill/his_file:

    ln /home/bill/his_file my_file 

As above but the link is created in /home/joe/my_file:

    ln /home/bill/his_file /home/joe/my_file 

As above but the link is named his_file and created in the current directory:

     ln /home/bill/his_file 

Here is an example of the second form where links to dog, cat and cow from the current directory are created in /home/joe:

    ln dog cat cow /home/joe 

Everything so far has created what is called a hard link. All this means is that the new pseudonym has exactly the same properties as the original name. In fact, the system makes on distinction between them. For example, you could rename the file pig to chicken with the following command sequence:

    ln pig chicken
    rm pig

The first line creates the pseudonym chicken for pig and the second deletes pig. The filesystem is smart enough to know that as long as at least one name points to the file, the file cannot be deleted.

All that said, there is a second kind of link called a symbolic link which has quite different properties. That is, rather than pointing to the file itself, it points to the file name (directory entry). This is the only kind of link that can be used between filesystems. To create a symbolic link, all works as above except you need to include the -s option. For example, to make a symbolic link called chicken that points to pig, you would say:

     ln -s pig chicken

The only way you will see that chicken is a symbolic link is by using the ls -l command (ls -l chicken). The output of this command will look much like this:

    lrwxrwxrwx  1 joe users    3 2003-11-18 17:26 chicken -> pig

The first character (l) indicates that this is a link and the chicken -> pig part indicates that chicken is a pointer to pig.

Now, if you were to delete the file pig (rm pig) the link named chicken would still exist but any attempt to reference it (for example, cat chicken) would give you a file not found error message.

Like most Linux commands, there are lots more options and lots more choices. If you enter ln --help you will see the complete list of options.

The first line creates the pseudonym chicken for pig and the second deletes pig. The filesystem is smart enough to know that as long as at least one name points to the file, the file cannot be deleted.

All that said, there is a second kind of link called a symbolic link which has quite different properties. That is, rather than pointing to the file itself, it points to the file name (directory entry). This is the only kind of link that can be used between filesystems. To create a symbolic link, all works as above except you need to include the -s option. For example, to make a symbolic link called chicken that points to pig, you would say:

     ln -s pig chicken

The only way you will see that chicken is a symbolic link is by using the ls -l command (ls -l chicken). The output of this command will look much like this:

    lrwxrwxrwx  1 joe users    3 2003-11-18 17:26 chicken -> pig

The first character (l) indicates that this is a link and the chicken -> pig part indicates that chicken is a pointer to pig.

Now, if you were to delete the file pig (rm pig) the link named chicken would still exist but any attempt to reference it (for example, cat chicken) would give you a file not found error message.

Like most Linux commands, there are lots more options and lots more choices. If you enter ln --help you will see the complete list of options.