ln Command

Purpose

Links files.

Syntax

To Link a File to a File

ln [ -f | -n ] [ -s ] SourceFile [ TargetFile ]

To Link a File or Files to a Directory

ln [ -f | -n ] [ -s ] SourceFile ... TargetDirectory

Description

The ln command links the file designated in the SourceFile parameter to the file designated by the TargetFile parameter or to the same file name in another directory specified by the TargetDirectory parameter. By default, the ln command creates hard links. To use the ln command to create symbolic links, designate the -s flag.

A symbolic link is an indirect pointer to a file; its directory entry contains the name of the file to which it is linked. Symbolic links may span file systems and may refer to directories.

If you are linking a file to a new name, you can list only one file. If you are linking to a directory, you can list more than one file.

The TargetFile parameter is optional. If you do not designate a target file, the ln command creates a new file in your current directory. The new file inherits the name of the file designated in the SourceFile parameter. See example 5.

Notes:
  1. You cannot link files across file systems without using the -s flag.
  2. If TargetDirectory is already a symbolic link to a directory, then the ln command treats the existing target as a file. This means that a command such as ln -fs somepath/lname symdir will not follow the existing symbolic link of symdir, instead it will create a new symbolic link from somepath/lname to symdir.

Flags

Item Description
-f Causes the ln command to replace any destination paths that already exist. If a destination path already exists and the -f flag is not specified, the ln command writes a diagnostic message to standard error without creating a new link and continues to link the remaining SourceFiles.
-n Specifies that if the link is an existing file, do not overwrite the contents of the file. The -f flag overrides this flag. This is the default behavior.
-s Causes the ln command to create symbolic links. A symbolic link contains the name of the file to which it is linked. The referenced file is used when an open operation is performed on the link. A stat call on a symbolic link returns the linked-to file; an lstat call must be done to obtain information about the link. The readlink call may be used to read the contents of a symbolic link. Symbolic links can span file systems and refer to directories.
Note: Absolute path names must be used when specifying the SourceFile parameter for the -s flag. If the absolute path name is not designated, unexpected results may occur when the SourceFile and the TargetFile parameters are located in different directories. The source file does not need to exist before creating the symbolic link.

Exit Status

This command returns the following exit values:

Item Description
0 All specified files were successfully linked.
>0 An error occurred.

Examples

  1. To create another link (alias) to a file, enter:
    ln  -f chap1  intro
    This links chap1 to the new name, intro. If intro does not already exist, the file name is created. If intro does exist, the file is replaced by a link to chap1. Then both the chap1 and intro file names will refer to the same file. Any changes made to one also appear in the other. If one file name is deleted with the rm command, the file is not completely deleted since it remains under the other name.
  2. To link a file to the same name in another directory, enter:
    ln  index  manual
    This links index to the new name, manual/index.
    Note: intro in example 1 is the name of a file; manual in example 2 is a directory that already exists.
  3. To link several files to names in another directory, enter:
    ln  chap2  jim/chap3  /home/manual
    This links chap2 to the new name /home/manual/chap2 and jim/chap3 to /home/manual/chap3.
  4. To use the ln command with pattern-matching characters, enter:
    ln  manual/* .
    This links all files in the manual directory into the current directory, . (dot), giving them the same names they have in the manual directory.
    Note: You must type a space between the asterisk and the period.
  5. To create a symbolic link, enter:
    ln -s /tmp/toc toc
    This creates the symbolic link, toc, in the current directory. The toc file points to the /tmp/toc file. If the /tmp/toc file exists, the cat toc command lists its contents.

    To achieve identical results without designating the TargetFile parameter, enter:

    ln -s /tmp/toc

Files

Item Description
/usr/bin/ln Contains the ln command.