Ln (Unix)

Revision as of 14:28, 16 May 2025 by imported>Stevebroshar (→‎Use: Add examples)
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Template:Short description Template:Lowercase title {{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1 | unknown = Template:Main other | preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y | AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid }}Template:Main other <syntaxhighlight lang="text" class="" style="" inline="1">ln</syntaxhighlight> is a shell command for creating a link file to an existing file or directory.<ref name=UNIXKP>Template:Cite book</ref> By default, the command creates a hard link, but with the <syntaxhighlight lang="text" class="" style="" inline="1">-s</syntaxhighlight> command line option, it creates a symbolic link.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> 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.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> The command can create a symbolic link to non-existent file.<ref name=GNULnman />

The command appeared in Issue 2 of the X/Open Portability Guidelines.<ref name=SUS/> The version in GNU Core Utilities was written by Mike Parker and David MacKenzie.<ref>Template:Man</ref> The command is available in Windows via UnxUtils<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and has been ported to IBM i.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

LinksEdit

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 Template:Endash each referring to the same inode.<ref name=libc /> 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.<ref name=UNIXKP /><ref name=IBMInode>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> A hard link can only refer to an object on the same file system.<ref>Template:Cite book</ref>

A symbolic link refers to another file by its path (a symbolic reference).<ref name=GNULnman>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> 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 <syntaxhighlight lang="text" class="" style="" inline="1">readlink</syntaxhighlight> command.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> 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.<ref name=libc>Template:Cite book</ref>

UseEdit

Per the Single Unix Specification, the <syntaxhighlight lang="text" class="" style="" inline="1">ln</syntaxhighlight> command is specified in the Shell and Utilities (XCU) document.<ref name=SUSOverview>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> POSIX includes a similar specification.<ref name=SUS>Template:Man</ref>

If neither target file nor target directory are specified, links are created in the current working directory.

OptionsEdit

The specification indicates command line options that must be supported:

  • <syntaxhighlight lang="text" class="" style="" inline="1">-f</syntaxhighlight> (force) Clobbers an existing file if the output path specifies an existing file
  • <syntaxhighlight lang="text" class="" style="" inline="1">-L</syntaxhighlight> If a source operand specifies a symbolic link, create a hard link to the link's target file
  • <syntaxhighlight lang="text" class="" style="" inline="1">-P</syntaxhighlight> If a source operand specifies a symbolic link, create a hard link to the symbolic link file
  • <syntaxhighlight lang="text" class="" style="" inline="1">-s</syntaxhighlight> Create symbolic links instead of hard links; causes <syntaxhighlight lang="text" class="" style="" inline="1">-L</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">-P</syntaxhighlight> to be silently ignored

If more than one of the mutually-exclusive options <syntaxhighlight lang="text" class="" style="" inline="1">-L</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">-P</syntaxhighlight> is specified, the last option specified determines the resulting behavior.

If the <syntaxhighlight lang="text" class="" style="" inline="1">-s</syntaxhighlight> option is not specified and neither a <syntaxhighlight lang="text" class="" style="" inline="1">-L</syntaxhighlight> nor a <syntaxhighlight lang="text" class="" style="" inline="1">-P</syntaxhighlight> option is specified, the implementation defines which of the <syntaxhighlight lang="text" class="" style="" inline="1">-L</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">-P</syntaxhighlight> options is used by default.

Single file invocationEdit

The command creates a new link to the path indicated by <syntaxhighlight lang="text" class="" style="" inline="1">source</syntaxhighlight>; stored at the path indicated by <syntaxhighlight lang="text" class="" style="" inline="1">target</syntaxhighlight>. The syntax is as follows:

<syntaxhighlight lang="terminal"> ln [-fs] [-L|-P] source target </syntaxhighlight>

Multiple file invocationEdit

The command creates a new link to each file specified by the <syntaxhighlight lang="text" class="" style="" inline="1">source_#</syntaxhighlight> operands; stored in an existing directory indicated by <syntaxhighlight lang="text" class="" style="" inline="1">target_dir</syntaxhighlight>.

<syntaxhighlight lang="terminal"> ln [-fs] [-L|-P] source_1 source_2 ... target_dir </syntaxhighlight>

ExamplesEdit

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

See alsoEdit

ReferencesEdit

Template:Reflist

External linksEdit

Template:Sister project

Template:Unix commands Template:Core Utilities commands