Ln (Unix)
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.
Links
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. 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 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. When encountered during pathname resolution, modify the 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.
Use
Per the Single Unix Specification, the command is specified in the Shell and Utilities document. POSIX includes a similar specification.If neither target file nor target directory are specified, links are created in the current working directory.
Options
The specification indicates command line options that must be supported:- Clobbers an existing file if the output path specifies an existing file
- If a source operand specifies a symbolic link, create a hard link to the link's target file
- If a source operand specifies a symbolic link, create a hard link to the symbolic link file
- Create symbolic links instead of hard links; causes and to be silently ignored
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.
Single file invocation
The command creates a new link to the path indicated by, stored at the path indicated by. The syntax is as follows:ln source target
Multiple file invocation
The command creates a new link to each file specified by the operands, stored in an existing directory indicated by.ln source_1 source_2... target_dir
Examples
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