ln command is a standard Unix command utility used to create a hard link (link) or a symbolic link (symlink) to an existing file. 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.
ln command by default creates hard links, and when called with the command line parameter
ln '-s' creates symbolic links. Most operating systems prevent hard links to directories from being created since such a capability could potentially disrupt the structure of a file system and interfere with the operation of other utilities. The
ln command can however be used to create symbolic links to non-existent files.
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
ln 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. Hard links can however only be created on the same file system; this can prove to be a particular 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 contents of the symbolic link is therefore the destination path string, which can also be examined using the
readlinkcommand 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).
The specification describes two ways of invoking the
ln utility. Specifically,
- In the first form of invocation the
lnutility creates a new hard link (Directory entry) for the source file specified by the
source_fileoperand at the destination path specified by the
target_fileoperand. However, if the
-soption is specified, a symbolic link is created.
ln [-fs] [-L|-P] source_file target_file
- In the second form of invocation the
lnutility creates a new hard link (Directory entry) or if the
-soption is specified a symbolic link, for each file specified by the
source_fileoperand, at a destination path in an existing directory named by operand
ln [-fs] [-L|-P] source_file... target_dir
- In the first form of invocation the
The specification also specifies that the command line options that must be supported:
-fForce existing destination pathnames to be removed to allow the link.
source_fileoperand that names a file that is a symbolic link, create a hard link to the file referenced by the symbolic link.
source_fileoperand that names a file that is a symbolic link, create a (hard) link to the symbolic link itself.
-sCreate 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
-Pis specified the last option specified determines the behavior of the utility.
- If the
-soption is not specified and neither a
-Poption is specified, it is implementation-defined which of the
-Poptions will be used as the default.
- Kernighan, Brian W.; Pike, Rob (1984). The Unix programming environment. Englewood Cliffs, N.J.: Prentice-Hall. p. 59. ISBN 013937681X.
- "GNU Coreutils: ln invocation". http://www.gnu.org. Free Software Foundation, Inc. Retrieved 7 August 2015.
- "ln - The Open Group Base Specifications Issue 7". http://pubs.opengroup.org. The IEEE and The Open Group. Retrieved 7 August 2015.
- "Why are hard links not allowed to directories in UNIX/Linux?". http://unix.stackexchange.com. Retrieved 7 August 2015.
- Loosemore, Sandra; Stallman, Richard M.; McGrath, Roland; Oram, Andrew; Dreppe, Ulrich. The GNU C Library Reference Manual (PDF). Free Software Foundation, Inc. p. 392. Retrieved 7 August 2015.
- "Speaking UNIX: It is all about the inode". http://www.ibm.com. IBM developerWorks. Retrieved 7 August 2015.
- Bovet, Daniel P.; Cesati, Marco (2005). Understanding the Linux Kernel (3rd ed.). Boston, MA: Safari Tech Books Online. p. 14. ISBN 0596005652.
- "The Single UNIX Specification, Version 4 - Overview". http://www.unix.org. http://www.unix.org. Retrieved 7 August 2015.
- "Shell & Utilities: Detailed Toc". http://pubs.opengroup.org. The IEEE and The Open Group. Retrieved 7 August 2015.
- "ln". http://pubs.opengroup.org. The IEEE and The Open Group. Retrieved 7 August 2015.
|The Wikibook Guide to Unix Computing has a page on the topic of: File System Utilities - ln|