Previous Table of Contents Next

Module 76


The external ln command links a new filename to an existing file. It is used to provide multiple filenames to the same physical data. It has two formats that allow you to:

*  Create an additional filename for an existing file.
*  Create corresponding filenames in a different directory for a set of existing files.

The link name (new filename) must reside in the same file system. Directory names cannot be linked.

There is another type of link called a "symbolic link." A symbolic link allows you to link filenames across different file systems. It also allows the linking of directory names.

Symbolic links are often referred to as soft links. This is because the link is not made using the actual inode number. Instead, it is performed by creating a simple name reference to the existing file or directory.


The general formats of the ln command follow.

     ln [ -fns ] filename linkname
     ln [ -fns ] filename_list directory

BSD (Berkeley)
ln [ -s ] filename [ linkname ]
ln [ -s ] filename_list directory


The options that may be used to control how ln functions follow.

-f Force the ln to occur. The response of "yes" is assumed and the link is performed. No interactive response is requested. Only works on hard links.
If the standard input is not the terminal keyboard, then the -f option is assumed.
-n If the link name is an existing file, do not overwrite the contents of the file. The -f option overrides this option.
-s Causes a symbolic link to be created. A symbolic link contains the name of the file to which it is linked, unlike a normal link which contains the inode number. Symbolic links may span file systems and refer to directories.

BSD (Berkeley)
-s Same as SV


The following list describes the arguments that may be passed to the ln command.

filename The existing file that will be linked to with linkname.
filename_list @T2 = The list of files that will be linked to in a different directory.
linkname The new filename linked to the existing file.
directory The directory where links are created to each file in the filename_list.


The first format creates an additional filename for an existing file. The linkname is entered into the directory with the same inode number as the existing file.

The new linkname and the existing filename cannot be the same full pathname. They may have the same filename, providing they reside in different directories. If the new linkname is an existing filename, its contents are lost.

BSD (Berkeley)
The BSD version of link does not allow the destruction of existing data. If a filename already exists and you try to use it for a linkname, ln complains that the file already exists.

The second format creates a link for one or more files in a specified directory. A link is created in the directory for each file listed in the filename_list.

A link is an entry in a directory pointing to an inode that exists and has a filename. Therefore, no new disk space is used other than the directory entry. Once a link has been created, the two filenames point to the same inode and are equivalent except in their symbolic name.

Before ln performs the link, it checks the linkname file for write permissions. If you do not have write permissions on the destination, ln will display the mode (chmod; Module 17) and request a response from standard input. If the input line you type starts with a "y," ln attempts to link the linkname to the existing file. If the ownership permissions allow you to write to the target, the move will occur. If you do not have permission to write to the target, the mv will fail.

Some common formats are:

ln personal_ltr pl # links the "pl" name to the personal_ltr file.
ln memo* ../grp/memos # links all memo files to directory @H2 =


The ln command by default will not link across file systems. This is because file systems may be added or removed at random and inode numbers are only unique within a single file system. Symbolic links circumvent this problem.


Refer to the cp, mv, rm, chmod, chown, and ls commands described in modules 24, 91, 114, 17, 18, and 84, respectively.


The ln command provides an alias for filenames. You can link a short filename to an existing file that has a long filename. ln can be used to access files in different directories so full pathnames are not required. It is commonly used by users in the same group to access the same file without having to know the full path.

It can also be used to provide different names to the same program. This way a program can examine the command line to find out what command you wanted and perform the required tasks. For example, vi and ex are the same executable program, but when the program begins execution it checks the command line. If it finds vi was called, then it enters visual mode; otherwise, it enters the line editor mode.


In this activity you use the ln command to create a new name for an existing file. Begin at the shell prompt.

1.  Type ln file1 letters and press Return. This will create a new filename in your letters directory named file1.
2.  To see that letters/file1 and file1 are linked, type ls -i letters/file1 file1 and press Return. Notice the numbers to the left of each filename. These are the inode numbers, which should be the same.
     cj> ls -i letters/file1 file1
     1652     letters/file1
     1652     file1
3.  Turn to Module 24 to continue the learning sequence.

Previous Table of Contents Next