Ln (Unix)

Ln (Unix)
The command is a standard Unix command utility used to create a hard link or a symbolic link (symlink) to an existing file or directory. The use of a hard link allows multiple filenames to be associated with the same file since a hard link points to the inode of a given file, the data of which is stored on disk. On the other hand, symbolic links are special files that refer to other files by name.
The command by default creates hard links, and when called with the command line parameter creates symbolic links. Most operating systems prevent hard links to directories from being created since such a capability could disrupt the structure of a file system and interfere with the operation of other utilities. The command can however be used to create symbolic links to non-existent files.
Versions
appeared in Issue 2 of the X/Open Portability Guidelines. The version of bundled in GNU coreutils was written by Mike Parker and David MacKenzie.
The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities. The command has also been ported to the IBM i operating system.
Links
Links allow more than one filename to refer to the same file as in the case of a hard link or act as pointers to a filename as in the case of a soft link. Both hard links and soft links can be created by the command. Specifically,
Hard links, also known simply as links, are objects that associate the filename with the inode, and therefore the file contents itself. A given file on disk could have multiple links scattered through the directory hierarchy, with all of the links being equivalent since they all associate with the same inode. Creating a link therefore does not copy the contents of the file but merely causes another name to be associated with the same contents. Each time a hard link is created, a link counter that is a part of the inode structure gets incremented; a file is not deleted until its reference count reaches zero. However, hard links can only be created on the same file system; this can prove to be a disadvantage.
Symbolic links are special files which, when encountered during pathname resolution, modify the pathname resolution to be taken to the location which the symbolic link contains. The content of the symbolic link is therefore the destination path string, which can also be examined using the command line utility. The symbolic link may contain an arbitrary string which does not refer to the location of an existing file. Such a symbolic link will fail until a file is created at the location which is contained by the symbolic link. By contrast, a symbolic link to an existing file will fail if the existing file is moved to a different location (or renamed).
Specification
The utility on systems compliant with the Single Unix Specification is specified in the Shell and Utilities (XCU) document, which forms a part of the Single Unix Specification. A mostly identical document is part of POSIX.
The specification describes two ways of invoking the utility. Specifically,
In the "single file" invocation the utility creates a new hard link (directory entry) for the source file specified by the operand at the destination path specified by the operand. However, if the option is specified, a symbolic link is created.
ln [-fs] [-L|-P] source_file target_file
In the "multiple file" invocation the utility creates a new hard link (directory entry), or if the option is specified, a symbolic link, for each file specified by the operand, at a destination path in an existing directory named by operand .
ln [-fs] [-L|-P] source_file_1 source_file_2 ... target_dir
The specification also specifies the command line options that must be supported:
Force existing destination pathnames to be removed to allow the link.
For each operand that names a file that is a symbolic link, create a hard link to the file referenced by the symbolic link.
For each operand that names a file that is a symbolic link, create a (hard) link to the symbolic link itself.
Create symbolic links instead of hard links. If the -s option is specified, the -L and -P options are silently ignored.
If more than one of the mutually-exclusive options and is specified the last option specified determines the behavior of the utility.
If the option is not specified and neither a nor a option is specified, the implementation defines which of the and options will be used as the default.
If neither target file nor target directory are specified, links will be created in the current working directory.
See also
List of Unix commands
NTFS junction point
References
External links
Standard Unix programs
Unix SUS2008 utilities
IBM i Qshell commands