Jump to content

ln (Unix)

From Wikipedia, the free encyclopedia
(Redirected fromReadlink)
ln
Original author(s)AT&T Bell Laboratories
Developer(s)Variousopen-sourceandcommercialdevelopers
Initial releaseNovember 3, 1971;52 years ago(1971-11-03)
Operating systemUnix,Unix-like,IBM i
PlatformCross-platform
TypeCommand
Licensecoreutils:GPLv3+

Thelncommand is a standardUnix commandutility used to create ahard linkor asymbolic link(symlink) to an existing file or directory.[1]The use of a hard link allows multiplefilenamesto be associated with the samefilesince a hard link points to theinodeof a given file, the data of which is stored ondisk.On the other hand, symbolic links are special files that refer to other files byname.[2]

Thelncommand by default creates hard links, and when called with thecommand lineparameterln -screates symbolic links.[3]Mostoperating systemsprevent hard links todirectoriesfrom being created since such a capability could disrupt the structure of afile systemand interfere with the operation of other utilities.[4]Thelncommand can however be used to create symbolic links to non-existent files.[2]

Versions

[edit]

lnappeared in Issue 2 of theX/OpenPortability Guidelines.[5]The version oflnbundled inGNUcoreutilswas written by Mike Parker and David MacKenzie.[6] The command is available as a separate package forMicrosoft Windowsas part of theUnxUtilscollection ofnativeWin32portsof common GNU Unix-like utilities.[7]Thelncommand has also been ported to theIBM ioperating system.[8]

[edit]

Links allow more than one filename to refer to the same file as in the case of ahard linkor act aspointersto a filename as in the case of asoft link.Both hard links and soft links can be created by thelncommand. Specifically,

  1. Hard links,also known simply as links, are objects that associate the filename with theinode,and therefore the file contents itself.[1]A given file on disk could have multiple links scattered through thedirectory hierarchy,with all of the links being equivalent since they all associate with the sameinode.[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, alink counterthat is a part of theinode structuregets incremented; a file is not deleted until its reference count reaches zero.[1][10]However, hard links can only be created on the samefile system;this can prove to be a disadvantage.[11]
  2. Symbolic linksare special files which, when encountered during pathname resolution, modify thepathname resolutionto be taken to the location which the symbolic link contains. The content of the symbolic link is therefore the destinationpathstring, which can also be examined using thereadlinkcommand 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]

Thelnutility on systems compliant with theSingle Unix Specificationis specified in the Shell and Utilities (XCU) document, which forms a part of the Single Unix Specification.[13]A mostly identical document is part ofPOSIX.[5]

The specification describes two ways of invoking thelnutility. Specifically,

In the "single file" invocation thelnutility creates a new hard link (directory entry) for the source file specified by thesource_fileoperand at the destination path specified by thetarget_fileoperand. However, if the-soption is specified, a symbolic link is created.
ln[-fs][-L|-P]source_filetarget_file
In the "multiple file" invocation thelnutility creates a new hard link (directory entry), or if the-soption is specified, a symbolic link, for each file specified by thesource_fileoperand, at a destination path in an existing directory named by operandtarget_dir.
ln[-fs][-L|-P]source_file_1source_file_2...target_dir

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

-fForce existing destination pathnames to be removed to allow the link.
-LFor eachsource_fileoperand that names a file that is a symbolic link, create a hard link to the file referenced by the symbolic link.
-PFor eachsource_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-Land-Pis specified the last option specified determines the behavior of the utility.
If the-soption is not specified and neither a-Lnor a-Poption is specified, the implementation defines which of the-Land-Poptions will be used as the default.

If neither target file nor target directory are specified, links will be created in the currentworking directory.

See also

[edit]

References

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