[Next] [Previous] [Up] [Top] [Contents]

8.1 Working With Files

8.1.7 ln - link to another file

The ln command creates a "link" or an additional way to access (or gives an additional name to) another file.


ln [options] source [target]

If not specified target defaults to a file of the same name in the present working directory.

Common Options

-f force a link regardless of target permissions; don't report errors (SVR4 only)

-s make a symbolic link


A symbolic link is used to create a new path to another file or directory. If a group of users, for example, is accustomed to using a command called chkmag, but the command has been rewritten and is now called chkit, creating a symbolic link so the users will automatically execute chkit when they enter the command chkmag will ease transition to the new command.

A symbolic link would be done in the following way:

% ln -s chkit chkmag

The long listing for these two files is now as follows:

16 -rwxr-x--- 1 lindadb acs 15927 Apr 23 04:10 chkit

1 lrwxrwxrwx 1 lindadb acs 5 Apr 23 04:11 chkmag -> chkit

Note that while the permissions for chkmag are open to all, since it is linked to chkit, the permissions, group and owner characteristics for chkit will be enforced when chkmag is run.

With a symbolic link, the link can exist without the file or directory it is linked to existing first.

A hard link can only be done to another file on the same file system, but not to a directory (except by the superuser). A hard link creates a new directory entry pointing to the same inode as the original file. The file linked to must exist before the hard link can be created. The file will not be deleted until all the hard links to it are removed. To link the two files above with a hard link to each other do:

% ln chkit chkmag

Then a long listing shows that the inode number (742) is the same for each:

% ls -il chkit chkmag

742 -rwxr-x--- 2 lindadb acs 15927 Apr 23 04:10 chkit

742 -rwxr-x--- 2 lindadb acs 15927 Apr 23 04:10 chkmag

Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]