Software:ln (Unix)

From HandWiki
Revision as of 11:43, 9 February 2024 by AstroAI (talk | contribs) (simplify)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Short description: Unix file management utility

ln
Ln-example-command.gif
Example usage of ln utility
Original author(s)AT&T Bell Laboratories
Developer(s)Various open-source and commercial developers
Initial releaseNovember 3, 1971; 53 years ago (1971-11-03)
Operating systemUnix, Unix-like, IBM i
PlatformCross-platform
TypeCommand
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

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]

Links

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

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

References

  1. 1.0 1.1 1.2 Kernighan, Brian W.; Pike, Rob (1984). The Unix programming environment.. Englewood Cliffs, N.J.: Prentice-Hall. p. 59. ISBN 013937681X. https://archive.org/details/unixprogramminge0000kern/page/59. 
  2. 2.0 2.1 "GNU Coreutils: ln invocation". Free Software Foundation, Inc.. https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html. 
  3. "ln - The Open Group Base Specifications Issue 7". The IEEE and The Open Group. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ln.html. 
  4. "Why are hard links not allowed to directories in UNIX/Linux?". http://unix.stackexchange.com/questions/22394/why-hard-links-not-allowed-to-directories-in-unix-linux?rq=1. 
  5. 5.0 5.1 ln – Commands & Utilities Reference, The Single UNIX Specification, Issue 7 from The Open Group
  6. ln(1) – Linux General Commands Manual
  7. "Native Win32 ports of some GNU utilities". http://unxutils.sourceforge.net/. 
  8. IBM. "IBM System i Version 7.2 Programming Qshell" (in en). https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc. 
  9. 9.0 9.1 Loosemore, Sandra; Stallman, Richard M.; McGrath, Roland; Oram, Andrew; Drepper, Ulrich. The GNU C Library Reference Manual. Free Software Foundation, Inc. p. 392. https://www.gnu.org/software/libc/manual/pdf/libc.pdf. Retrieved 7 August 2015. 
  10. "Speaking UNIX: It is all about the inode". IBM developerWorks. http://www.ibm.com/developerworks/aix/library/au-speakingunix14/. 
  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". http://linux.die.net/man/1/readlink. 
  13. "The Single UNIX Specification, Version 4 - Overview". unix.org. http://www.unix.org/version4/overview.html. 

External links