is a shell command for creating a link file to an existing file or directory. By default, the command creates a hard link, but with the command line option, it creates a symbolic link. Most systems disallow a hard link to a directory since such links could disrupt the structure of a file system and interfere with the operation of other utilities. The command can create a symbolic link to non-existent file.
The command appeared in Issue 2 of the X/Open Portability Guidelines. The version in GNU Core Utilities was written by Mike Parker and David MacKenzie. The command is available in Windows via UnxUtils and has been ported to IBM i.
A link allows more than one path to refer to the same file.
A hard link is a directory entry that refers to a file's inode (an internal reference). A file can have multiple hard links each referring to the same inode. Creating a hard link does not copy the contents of the file, but merely causes another name to be associated with the same content. When a hard link is created for a file, a counter in its inode is incremented, and removing a hard link results in decrementing the counter. A file is not deleted (to allow reusing its storage space) until its reference count reaches zero. A hard link can only refer to an object on the same file system.
A symbolic link refers to another file by its path (a symbolic reference). When encountered during pathname resolution, modify the pathname resolution to be taken to the location which the symbolic link contains. The content of a symbolic link is the target path, which can also be examined via the command. A symbolic link may specify a path that does not refer to an existing file. Resolving such a symbolic link will fail until a file is created at its specified path. Also, a symbolic link to an existing file will fail to resolve if the file is moved.
Per the Single Unix Specification, the command is specified in the Shell and Utilities (XCU) document. POSIX includes a similar specification.
If neither target file nor target directory are specified, links are created in the current working directory.
The specification indicates command line options that must be supported:
If more than one of the mutually-exclusive options and is specified, the last option specified determines the resulting behavior.
If the option is not specified and neither a nor a option is specified, the implementation defines which of the and options is used by default.
The command creates a new link to the path indicated by , stored at the path indicated by . The syntax is as follows:
The command creates a new link to each file specified by the operands, stored in an existing directory indicated by .
The following creates a hard link to file foo called bar.
ln foo bar
The following creates a symbolic link to file foo called bar.
ln -s foo bar