Jump to content

Ln (Unix): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎History: no history given
 
(31 intermediate revisions by 16 users not shown)
Line 1: Line 1:
{{Short description|Unix file management utility}}
{{Lowercase title}}
{{Infobox software
| name = ln
| logo =
| screenshot = Ln-example-command.gif
| screenshot size =
| caption = Example usage of {{code|ln}} utility
| author = [[AT&T Bell Laboratories]]
| developer = Various [[open-source software|open-source]] and [[commercial software|commercial]] developers
| released = {{Start date and age|1971|11|3}}
| latest release version =
| latest release date =
| operating system = [[Unix]], [[Unix-like]], [[IBM i]]
| platform = [[Cross-platform]]
| genre = [[Command (computing)|Command]]
| license = [[coreutils]]: [[GPLv3+]]
| website =
}}
The '''{{code|ln}}''' command is a standard [[Unix command]] utility used to create a [[hard link]] or a [[symbolic link]] (symlink) to an existing file or directory.<ref name=UNIXKP>{{cite book|last1=Kernighan|first1=Brian W.|last2=Pike|first2=Rob|title=The Unix programming environment.|date=1984|publisher=Prentice-Hall|location=Englewood Cliffs, N.J.|isbn=013937681X|page=[https://archive.org/details/unixprogramminge0000kern/page/59 59]|url-access=registration|url=https://archive.org/details/unixprogramminge0000kern/page/59}}</ref> The use of a hard link allows multiple [[filenames]] to be associated with the same [[Computer file|file]] since a hard link points to the [[inode]] of a given file, the data of which is stored on [[Hard disk drive|disk]]. On the other hand, symbolic links are special files that refer to other files by [[Filename#References: absolute vs relative|name]].<ref name=GNULnman>{{cite web|title=GNU Coreutils: ln invocation|url=https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html|website=gnu.org|publisher=Free Software Foundation, Inc.|access-date=7 August 2015}}</ref>


The {{code|ln}} command by default creates hard links, and when called with the [[Command-line interface|command line]] [[Command-line interface#Application command-line interfaces|parameter]] {{code|ln -s}} creates symbolic links.<ref>{{cite web|title=ln - The Open Group Base Specifications Issue 7|url=http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ln.html|website=pubs.opengroup.org|publisher=The IEEE and The Open Group|access-date=7 August 2015}}</ref> Most [[operating systems]] prevent hard links to [[Directory (computing)|directories]] from being created since such a capability could disrupt the structure of a [[file system]] and interfere with the operation of other utilities.<ref>{{cite web|title=Why are hard links not allowed to directories in UNIX/Linux?|url=http://unix.stackexchange.com/questions/22394/why-hard-links-not-allowed-to-directories-in-unix-linux?rq=1|website=unix.stackexchange.com|access-date=7 August 2015}}</ref> The {{code|ln}} command can however be used to create symbolic links to non-existent files.<ref name=GNULnman />
The <code>'''ln'''</code> command is a standard [[Unix command]] utility used to create a [[hard link]] or a [[symbolic link]] (symlink) to an existing file.<ref name=UNIXKP>{{cite book|last1=Kernighan|first1=Brian W.|last2=Pike|first2=Rob|title=The Unix programming environment.|date=1984|publisher=Prentice-Hall|location=Englewood Cliffs, N.J.|isbn=013937681X|page=59}}</ref> The use of a hard link allows multiple [[filenames]] to be associated with the same [[Computer file|file]] since a hard link points to the [[inode]] of a given file, the data of which is stored on [[Hard disk drive|disk]]. On the other hand, symbolic links are special files that refer to other files by [[Filename#References: absolute vs relative|name]].<ref name=GNULnman>{{cite web|title=GNU Coreutils: ln invocation|url=https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html|website=gnu.org|publisher=Free Software Foundation, Inc.|accessdate=7 August 2015}}</ref>


==Versions==
The <code>ln</code> command by default creates hard links, and when called with the [[Command-line interface|command line]] [[Command-line interface#Application command-line interfaces|parameter]] <code>ln '-s'</code> creates symbolic links.<ref>{{cite web|title=ln - The Open Group Base Specifications Issue 7|url=http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ln.html|website=pubs.opengroup.org|publisher=The IEEE and The Open Group|accessdate=7 August 2015}}</ref> Most [[operating systems]] prevent hard links to [[Directory (computing)|directories]] from being created since such a capability could disrupt the structure of a [[file system]] and interfere with the operation of other utilities.<ref>{{cite web|title=Why are hard links not allowed to directories in UNIX/Linux?|url=http://unix.stackexchange.com/questions/22394/why-hard-links-not-allowed-to-directories-in-unix-linux?rq=1|website=unix.stackexchange.com|accessdate=7 August 2015}}</ref> The <code>ln</code> command can however be used to create symbolic links to non-existent files.<ref name=GNULnman />
{{code|ln}} appeared in Issue 2 of the [[X/Open]] Portability Guidelines.<ref name=SUS/> The version of {{code|ln}} bundled in [[GNU]] [[coreutils]] was written by Mike Parker and David MacKenzie.<ref>{{man|1|ln|ManKier}}</ref>
The command is available as a separate package for [[Microsoft Windows]] as part of the [[UnxUtils]] collection of [[Native (computing)|native]] [[Windows API|Win32]] [[porting|ports]] of common GNU Unix-like utilities.<ref>{{Cite web|url=http://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net}}</ref> The {{Mono|ln}} command has also been ported to the [[IBM i]] operating system.<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=[[IBM]] |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 }}</ref>


== Links ==
==Links==
Links allow more than one filename to refer to the same file as in the case of a [[hard link]] or act as [[Reference (computer science)|pointers]] to a filename as in the case of a [[Symbolic link|soft link]]. Both hard links and soft links can be created by the <code>ln</code> command. Specifically,
Links allow more than one filename to refer to the same file as in the case of a [[hard link]] or act as [[Reference (computer science)|pointers]] to a filename as in the case of a [[Symbolic link|soft link]]. Both hard links and soft links can be created by the {{code|ln}} command. Specifically,
#[[Hard link]]s, also known simply as links, are objects that associate the filename with the [[inode]], and therefore the file contents itself.<ref name=UNIXKP /> A given file on disk could have multiple links scattered through the [[Directory structure|directory hierarchy]], with all of the links being equivalent since they all associate with the same [[inode]].<ref name=libc /> 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 [[Reference counting#File systems|link counter]] that is a part of the [[inode#POSIX inode description|inode structure]] gets incremented; a file is not deleted until its reference count reaches zero.<ref name=UNIXKP /><ref name=IBMInode>{{cite web|title=Speaking UNIX: It is all about the inode|url=http://www.ibm.com/developerworks/aix/library/au-speakingunix14/|website=ibm.com|publisher=IBM developerWorks|accessdate=7 August 2015}}</ref> However, hard links can only be created on the same [[file system]]; this can prove to be a disadvantage.<ref>{{cite book|last1=Bovet|first1=Daniel P.|last2=Cesati|first2=Marco|title=Understanding the Linux Kernel|date=2005|publisher=Safari Tech Books Online|location=Boston, MA|isbn=0596005652|page=14|edition=3rd}}</ref>
#[[Hard link]]s, also known simply as links, are objects that associate the filename with the [[inode]], and therefore the file contents itself.<ref name=UNIXKP /> A given file on disk could have multiple links scattered through the [[Directory structure|directory hierarchy]], with all of the links being equivalent since they all associate with the same [[inode]].<ref name=libc /> 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 [[Reference counting#File systems|link counter]] that is a part of the [[inode#POSIX inode description|inode structure]] gets incremented; a file is not deleted until its reference count reaches zero.<ref name=UNIXKP /><ref name=IBMInode>{{cite web|title=Speaking UNIX: It is all about the inode|url=http://www.ibm.com/developerworks/aix/library/au-speakingunix14/|website=ibm.com|publisher=IBM developerWorks|access-date=7 August 2015}}</ref> However, hard links can only be created on the same [[file system]]; this can prove to be a disadvantage.<ref>{{cite book|last1=Bovet|first1=Daniel P.|last2=Cesati|first2=Marco|title=Understanding the Linux Kernel|date=2005|publisher=Safari Tech Books Online|location=Boston, MA|isbn=0596005652|page=14|edition=3rd}}</ref>
#[[Symbolic link]]s are special files which, when encountered during pathname resolution, modify the [http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13 pathname resolution] to be taken to the location which the symbolic link contains. The content of the symbolic link is therefore the destination [[Path (computing)|path]] string, which can also be examined using the <code>readlink</code> command line utility.<ref>{{cite web|title=readlink(1) - Linux man page|url=http://linux.die.net/man/1/readlink|website=linux.die.net|accessdate=7 August 2015|ref=ReadlinkMan}}</ref> 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).<ref name=libc>{{cite book|last1=Loosemore|first1=Sandra|last2=Stallman|first2=Richard M.|last3=McGrath|first3=Roland|last4=Oram|first4=Andrew|last5=Drepper|first5=Ulrich|title=The GNU C Library Reference Manual|publisher=Free Software Foundation, Inc|page=392|url=https://www.gnu.org/software/libc/manual/pdf/libc.pdf|accessdate=7 August 2015}}</ref>
#[[Symbolic link]]s are special files which, when encountered during pathname resolution, modify the [http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13 pathname resolution] to be taken to the location which the symbolic link contains. The content of the symbolic link is therefore the destination [[Path (computing)|path]] string, which can also be examined using the {{code|readlink}} command line utility.<ref>{{cite web|title=readlink(1) - Linux man page|url=http://linux.die.net/man/1/readlink|website=linux.die.net|access-date=7 August 2015|ref=ReadlinkMan}}</ref> 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).<ref name=libc>{{cite book|last1=Loosemore|first1=Sandra|last2=Stallman|first2=Richard M.|last3=McGrath|first3=Roland|last4=Oram|first4=Andrew|last5=Drepper|first5=Ulrich|title=The GNU C Library Reference Manual|publisher=Free Software Foundation, Inc|page=392|url=https://www.gnu.org/software/libc/manual/pdf/libc.pdf|access-date=7 August 2015}}</ref>


==Specification ==
==Specification ==
The <code>ln</code> 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.<ref name=SUSOverview>{{cite web|title=The Single UNIX Specification, Version 4 - Overview|url=http://www.unix.org/version4/overview.html|website=unix.org|publisher=unix.org|accessdate=7 August 2015}}</ref><ref name=XCUTOC>{{cite web|title=Shell & Utilities: Detailed Toc|url=http://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html|website=pubs.opengroup.org|publisher=The IEEE and The Open Group|accessdate=7 August 2015}}</ref>
The {{code|ln}} 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.<ref name=SUSOverview>{{cite web|title=The Single UNIX Specification, Version 4 - Overview|url=http://www.unix.org/version4/overview.html|website=unix.org|publisher=unix.org|access-date=7 August 2015}}</ref> A mostly identical document is part of [[POSIX]].<ref name=SUS>{{man|1|ln|SUS}}</ref>


The specification describes two ways of invoking the <code>ln</code> utility.<ref name=lnSpec>{{cite web|title=ln|url=http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ln.html#tag_20_67|website=pubs.opengroup.org|publisher=The IEEE and The Open Group|accessdate=7 August 2015}}</ref> Specifically,
The specification describes two ways of invoking the {{code|ln}} utility. Specifically,
::In the "single file" invocation the <code>ln</code> utility creates a new hard link (directory entry) for the source file specified by the <code>source_file</code> operand at the destination path specified by the <code>target_file</code> operand. However, if the <code>-s</code> option is specified, a symbolic link is created.
::In the "single file" invocation the {{code|ln}} utility creates a new hard link (directory entry) for the source file specified by the ''{{code|source_file}}'' operand at the destination path specified by the ''{{code|target_file}}'' operand. However, if the {{code|-s}} option is specified, a symbolic link is created.
::<syntaxhighlight lang="bash">
::<syntaxhighlight lang="bash">
ln [-fs] [-L|-P] source_file target_file
ln [-fs] [-L|-P] source_file target_file
</syntaxhighlight>
</syntaxhighlight>
::In the "multiple file" invocation the <code>ln</code> utility creates a new hard link ([http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_130 Directory entry]) or if the <code>-s</code> option is specified, a symbolic link, for each file specified by the <code>source_file</code> operand, at a destination path in an existing directory named by operand <code>target_dir</code>.
::In the "multiple file" invocation the {{code|ln}} utility creates a new hard link ([http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_130 directory entry]), or if the {{code|-s}} option is specified, a symbolic link, for each file specified by the ''{{code|source_file}}'' operand, at a destination path in an existing directory named by operand ''{{code|target_dir}}''.
::<syntaxhighlight lang="bash">
::<syntaxhighlight lang="bash">
ln [-fs] [-L|-P] source_file_1 source_file_2 ... target_dir
ln [-fs] [-L|-P] source_file_1 source_file_2 ... target_dir
</syntaxhighlight>
</syntaxhighlight>
The specification also specifies the command line options that must be supported:
The specification also specifies the command line options that must be supported:
::<code>-f</code> Force existing destination pathnames to be removed to allow the link.
::{{code|-f}} Force existing destination pathnames to be removed to allow the link.
::<code>-L</code> For each <code>source_file</code> operand that names a file that is a symbolic link, create a hard link to the file referenced by the symbolic link.
::{{code|-L}} For each ''{{code|source_file}}'' operand that names a file that is a symbolic link, create a hard link to the file referenced by the symbolic link.
::<code>-P</code> For each <code>source_file</code> operand that names a file that is a symbolic link, create a (hard) link to the symbolic link itself.
::{{code|-P}} For each ''{{code|source_file}}'' operand that names a file that is a symbolic link, create a (hard) link to the symbolic link itself.
::<code>-s</code> Create symbolic links instead of hard links. If the -s option is specified, the -L and -P options are silently ignored.
::{{code|-s}} 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 <code>-L</code> and <code>-P</code> is specified the last option specified determines the behavior of the utility.
::If more than one of the mutually-exclusive options {{code|-L}} and {{code|-P}} is specified the last option specified determines the behavior of the utility.
::If the <code>-s</code> option is not specified and neither a <code>-L</code> nor a <code>-P</code> option is specified, the implementation defines which of the <code>-L</code> and <code>-P</code> options will be used as the default.
::If the {{code|-s}} option is not specified and neither a {{code|-L}} nor a {{code|-P}} option is specified, the implementation defines which of the {{code|-L}} and {{code|-P}} 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]].
If neither target file nor target directory are specified, links will be created in the current [[working directory]].


==See also==
==See also==
*[[List of Unix programs]]
*[[List of Unix commands]]
*[[NTFS junction point]]
*[[NTFS junction point]]


Line 39: Line 61:


==External links==
==External links==
{{Wikibooks|Guide to Unix|Commands/File_System_Utilities#ln| File System Utilities - ln}}
* {{man|1|ln|die.net}}
* {{man|1|ln|die.net}}
* {{man|1|ln|FreeBSD}}
* {{man|1|ln|FreeBSD}}
Line 44: Line 67:
* {{man|1|ln|OpenBSD}}
* {{man|1|ln|OpenBSD}}
* {{man|1|ln|Darwin}}
* {{man|1|ln|Darwin}}
{{Wikibooks|Guide to Unix|Commands/File_System_Utilities#ln| File System Utilities - ln}}


{{Unix commands}}
{{Unix commands}}
{{Core Utilities commands}}


[[Category:Standard Unix programs]]
[[Category:Standard Unix programs]]
[[Category:Unix SUS2008 utilities]]
[[Category:Unix SUS2008 utilities]]
[[Category:IBM i Qshell commands]]

Latest revision as of 15:49, 4 September 2023

ln
Original author(s)AT&T Bell Laboratories
Developer(s)Various open-source and commercial developers
Initial releaseNovember 3, 1971; 52 years ago (1971-11-03)
Operating systemUnix, Unix-like, IBM i
PlatformCross-platform
TypCommand
Licensecoreutils: GPLv3+

The ln command is a standard Unix command utility used to create a hard link or a symbolic link (symlink) to an existing file or directory.[1] 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.[2]

The ln command by default creates hard links, and when called with the command line parameter ln -s creates symbolic links.[3] 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.[4] The ln command can however be used to create symbolic links to non-existent files.[2]

Versions

[edit]

ln appeared in Issue 2 of the X/Open Portability Guidelines.[5] The version of ln bundled in GNU coreutils was written by Mike Parker and David MacKenzie.[6] 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.[7] The ln command has also been ported to the IBM i operating system.[8]

[edit]

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,

  1. Hard links, also known simply as links, are objects that associate the filename with the inode, and therefore the file contents itself.[1] 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.[9] 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.[1][10] However, hard links can only be created on the same file system; this can prove to be a disadvantage.[11]
  2. 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 readlink command line utility.[12] 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).[9]

Specification

[edit]

The ln 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.[13] A mostly identical document is part of POSIX.[5]

The specification describes two ways of invoking the ln utility. Specifically,

In the "single file" invocation the ln utility creates a new hard link (directory entry) for the source file specified by the source_file operand at the destination path specified by the target_file operand. However, if the -s option is specified, a symbolic link is created.
ln [-fs] [-L|-P] source_file target_file
In the "multiple file" invocation the ln utility creates a new hard link (directory entry), or if the -s option is specified, a symbolic link, for each file specified by the source_file operand, at a destination path in an existing directory named by operand target_dir.
ln [-fs] [-L|-P] source_file_1 source_file_2 ... target_dir

The specification also specifies the command line options that must be supported:

-f Force existing destination pathnames to be removed to allow the link.
-L For each source_file operand that names a file that is a symbolic link, create a hard link to the file referenced by the symbolic link.
-P For each source_file operand that names a file that is a symbolic link, create a (hard) link to the symbolic link itself.
-s 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 -L and -P is specified the last option specified determines the behavior of the utility.
If the -s option is not specified and neither a -L nor a -P option is specified, the implementation defines which of the -L and -P 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

[edit]

References

[edit]
  1. ^ a b c Kernighan, Brian W.; Pike, Rob (1984). The Unix programming environment. Englewood Cliffs, N.J.: Prentice-Hall. p. 59. ISBN 013937681X.
  2. ^ a b "GNU Coreutils: ln invocation". gnu.org. Free Software Foundation, Inc. Retrieved 7 August 2015.
  3. ^ "ln - The Open Group Base Specifications Issue 7". pubs.opengroup.org. The IEEE and The Open Group. Retrieved 7 August 2015.
  4. ^ "Why are hard links not allowed to directories in UNIX/Linux?". unix.stackexchange.com. Retrieved 7 August 2015.
  5. ^ a b ln – Shell and Utilities Reference, The Single UNIX Specification, Version 4 from The Open Group
  6. ^ ln(1) - Linux General Commands Manual
  7. ^ "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
  8. ^ IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Retrieved 2020-09-05.
  9. ^ a b Loosemore, Sandra; Stallman, Richard M.; McGrath, Roland; Oram, Andrew; Drepper, Ulrich. The GNU C Library Reference Manual (PDF). Free Software Foundation, Inc. p. 392. Retrieved 7 August 2015.
  10. ^ "Speaking UNIX: It is all about the inode". ibm.com. IBM developerWorks. Retrieved 7 August 2015.
  11. ^ Bovet, Daniel P.; Cesati, Marco (2005). Understanding the Linux Kernel (3rd ed.). Boston, MA: Safari Tech Books Online. p. 14. ISBN 0596005652.
  12. ^ "readlink(1) - Linux man page". linux.die.net. Retrieved 7 August 2015.
  13. ^ "The Single UNIX Specification, Version 4 - Overview". unix.org. unix.org. Retrieved 7 August 2015.
[edit]