The ln Command

The ln command is used to create links between files.

ln's basic syntax is

ln [options] target link


ln [options] target(s) [directory]

The items in square brackets are optional. target is the file name to which the link is to be created.

By default ln creates hard links, which are merely additional names for an existing file. Hard links cannot be made to directories, nor can they cross filesystem and partition boundaries.

If two file names are provided as arguments, ln uses the second one, assuming that it is not the name of an existing file, as the name for the hard link to the first one. Thus, for example, the following would create a link named link1 to a file named file1:

ln file1 link1

If the last argument (i.e., input data) is the name of an existing directory, ln creates a hard link in that directory for each file name provided to it. Each such link has the same name as the file which it links. An error message is returned if there are more than two arguments and the last one is not the name of an existing directory. Thus, in the following example, with the first two arguments named file2 and file3 and the third argument a directory called dir1, two links would be created within dir1 with the names file2 and file3:

ln file2 file3 dir1

If the only argument is a single name, inclusive of its absolute path or its relative path, that is not located in the current directory (i.e., the directory in which the user is currently working), then ln creates a link to that file in the current directory. Thus, in the following example, ln would create a link named file4 in the current directory to a file whose absolute path is /usr/file4:

ln /usr/file4


The most useful of ln's several options is -s, which causes it to create a symbolic link, also called a soft link, instead of a hard link. Symbolic links have several advantages over hard links, including the ability to be made across filesystem and partition boundaries. Also, any changes made to the original file are also made automatically to the links, and visa versa.

By default, ln does not overwrite existing files with links having the same name, but instead displays an error message. However, . . .

Created October 30, 2007.
Copyright © 2007 The Linux Information Project. All Rights Reserved.