Ln (Unix)

Last updated
ln
Original author(s) AT&T Bell Laboratories
Developer(s) Various open-source and commercial developers
Initial releaseNovember 3, 1971;51 years ago (1971-11-03)
Operating system Unix, Unix-like, IBM i
Platform Cross-platform
Type Command
License coreutils: 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]

Contents

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 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_filetarget_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_1source_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

Related Research Articles

In Unix and Unix-like operating systems, chmod is the command and system call used to change the access permissions and the special mode flags of file system objects. Collectively these were originally called its modes, and the name chmod was chosen as an abbreviation of change mode.

In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from "tape archive", as it was originally developed to write data to sequential I/O devices with no file system of their own. The archive data sets created by tar contain various file system parameters, such as name, timestamps, ownership, file-access permissions, and directory organization. POSIX abandoned tar in favor of pax, yet tar sees continued widespread use.

ls Command to list files and directories in Unix and Unix-like operating systems

In computing, ls is a command to list computer files and directories in Unix and Unix-like operating systems. It is specified by POSIX and the Single UNIX Specification.

In computing, a symbolic link is a file whose purpose is to point to a file or directory by specifying a path thereto.

In computing, touch is a command used to update the access date and/or modification date of a computer file or directory. It is included in Unix and Unix-like operating systems, TSC's FLEX, Digital Research/Novell DR DOS, the AROS shell, the Microware OS-9 shell, and ReactOS. The command is also available for FreeDOS and Microsoft Windows.

The archiver, also known simply as ar, is a Unix utility that maintains groups of files as a single archive file. Today, ar is generally used only to create and update static library files that the link editor or linker uses and for generating .deb packages for the Debian family; it can be used to create archives for any purpose, but has been largely replaced by tar for purposes other than static libraries. An implementation of ar is included as one of the GNU Binutils.

In computing, a hard link is a directory entry that associates a name with a file. Thus, each file must have at least one hard link. Creating additional hard links for a file makes the contents of that file accessible via additional paths. This causes an alias effect: a process can open the file by any one of its paths and change its content. By contrast, a soft link or “shortcut” to a file is not a direct link to the data itself, but rather a reference to a hard link or another soft link.

dd is a command-line utility for Unix, Plan 9, Inferno, and Unix-like operating systems and beyond, the primary purpose of which is to convert and copy files. On Unix, device drivers for hardware and special device files appear in the file system just like normal files; dd can also read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data. The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.

wc (Unix) Unix command utility

wc is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow.

cp (Unix) Unix command utility

In computing, cp is a command in various Unix and Unix-like operating systems for copying files and directories. The command has three principal modes of operation, expressed by the types of arguments presented to the program for copying a file to another file, one or more files to a directory, or for copying entire directories to another directory.

split is a utility on Unix, Plan 9, and Unix-like operating systems most commonly used to split a computer file into two or more smaller files.

du (Unix) Standard Unix program

du is a standard Unix program used to estimate file space usage—space used under a particular directory or files on a file system. A Windows commandline version of this program is part of Sysinternals suite by Mark Russinovich.

mv is a Unix command that moves one or more files or directories from one place to another. If both filenames are on the same filesystem, this results in a simple file rename; otherwise the file content is copied to the new location and the old file is removed. Using mv requires the user to have write permission for the directories the file will move between. This is because mv changes the content of both directories involved in the move. When using the mv command on files located on the same filesystem, the file's timestamp is not updated.

file (command) Standard Unix program

The file command is a standard program of Unix and Unix-like operating systems for recognizing the type of data contained in a computer file.

In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.

rm (Unix) Unix command utility

rm is a basic command on Unix and Unix-like operating systems used to remove objects such as computer files, directories and symbolic links from file systems and also special files such as device nodes, pipes and sockets, similar to the del command in MS-DOS, OS/2, and Microsoft Windows. The command is also available in the EFI shell.

test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [.

cpio is a general file archiver utility and its associated file format. It is primarily installed on Unix-like computer operating systems. The software utility was originally intended as a tape archiving program as part of the Programmer's Workbench (PWB/UNIX), and has been a component of virtually every Unix operating system released thereafter. Its name is derived from the phrase copy in and out, in close description of the program's use of standard input and standard output in its operation.

The link utility is a Unix command line program that creates a hard link from an existing directory entry to a new directory entry. It does no more than call the link system function. It does not perform error checking before attempting to create the link. It returns an exit status that indicates whether the link was created. Creating a link to a directory entry that is itself a directory requires elevated privileges.

dirname is a standard computer program on Unix and Unix-like operating systems. When dirname is given a pathname, it will delete any suffix beginning with the last slash ('/') character and return the result. dirname is described in the Single UNIX Specification and is primarily used in shell scripts.

References

  1. 1 2 3 Kernighan, Brian W.; Pike, Rob (1984). The Unix programming environment . Englewood Cliffs, N.J.: Prentice-Hall. p.  59. ISBN   013937681X.
  2. 1 2 "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. 1 2 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. 1 2 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.