1.\" $OpenBSD: ln.1,v 1.31 2013/12/02 22:19:30 sobrado Exp $ 2.\" $NetBSD: ln.1,v 1.10 1995/07/25 19:37:04 jtc Exp $ 3.\" 4.\" Copyright (c) 1980, 1990, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" This code is derived from software contributed to Berkeley by 8.\" the Institute of Electrical and Electronics Engineers, Inc. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)ln.1 8.2 (Berkeley) 12/30/93 35.\" 36.Dd $Mdocdate: December 2 2013 $ 37.Dt LN 1 38.Os 39.Sh NAME 40.Nm ln 41.Nd make hard and symbolic links to files 42.Sh SYNOPSIS 43.Nm ln 44.Op Fl fhLnPs 45.Ar source 46.Op Ar target 47.Nm ln 48.Op Fl fLPs 49.Ar source ...\& 50.Op Ar directory 51.Sh DESCRIPTION 52The 53.Nm 54utility creates a new directory entry (linked file) which has the 55same modes as the original file. 56It is useful for maintaining multiple copies of a file in many places 57at once without using up storage for the copies; 58instead, a link 59.Dq points 60to the original copy. 61There are two types of links: hard links and symbolic links. 62How a link points 63to a file is one of the differences between a hard and symbolic link. 64.Pp 65The options are as follows: 66.Bl -tag -width Ds 67.It Fl f 68Unlink any already existing file, permitting the link to occur. 69.It Fl h 70If the target is a symlink to a directory, do not descend into it. 71.It Fl L 72When creating a hard link and the source is a symbolic link, 73link to the fully resolved target of the symbolic link. 74This is the default. 75The 76.Fl L 77option overrides any previous 78.Fl P 79options. 80.It Fl n 81An alias for 82.Fl h 83for compatibility with other operating systems. 84.It Fl P 85When creating a hard link and the source is a symbolic link, 86link to the symbolic link itself. 87The 88.Fl P 89option overrides any previous 90.Fl L 91options. 92.It Fl s 93Create a symbolic link. 94.El 95.Pp 96By default, 97.Nm 98makes 99.Dq hard 100links. 101A hard link to a file is indistinguishable from the original directory entry; 102any changes to a file are effectively independent of the name used to reference 103the file. 104Hard links may not normally refer to directories and may not span file systems. 105.Pp 106A symbolic link contains the name of the file to 107which it is linked. 108The referenced file is used when an 109.Xr open 2 110operation is performed on the link. 111A 112.Xr stat 2 113on a symbolic link will return the linked-to file; an 114.Xr lstat 2 115must be done to obtain information about the link. 116The 117.Xr readlink 2 118call may be used to read the contents of a symbolic link. 119Symbolic links may span file systems, refer to directories, and refer to 120non-existent files. 121.Pp 122Given one or two arguments, 123.Nm 124creates a link to an existing file 125.Ar source . 126If 127.Ar target 128is given, the link has that name; 129.Ar target 130may also be a directory in which to place the link. 131Otherwise, it is placed in the current directory. 132If only the directory is specified, the link will be made 133to the last component of 134.Ar source . 135.Pp 136Given more than two arguments, 137.Nm 138makes links in 139.Ar directory 140to all the named source files. 141The links made will have the same name as the files being linked to. 142.Sh EXIT STATUS 143.Ex -std ln 144.Sh EXAMPLES 145Create a symbolic link named 146.Pa /home/www 147and point it to 148.Pa /var/www : 149.Pp 150.Dl # ln -s /var/www /home/www 151.Pp 152Hard link 153.Pa /usr/local/bin/fooprog 154to file 155.Pa /usr/local/bin/fooprog-1.0 : 156.Pp 157.Dl # ln /usr/local/bin/fooprog-1.0 /usr/local/bin/fooprog 158.Pp 159As an exercise, try the following commands: 160.Bd -literal -offset indent 161$ ls -i /bin/[ 16211553 /bin/[ 163$ ls -i /bin/test 16411553 /bin/test 165.Ed 166.Pp 167Note that both files have the same inode; that is, 168.Pa /bin/[ 169is essentially an alias for the 170.Xr test 1 171command. 172This hard link exists so 173.Xr test 1 174may be invoked from shell scripts, for example, using the 175.Li "if [ ]" 176construct. 177.Pp 178In the next example, the second call to 179.Nm 180removes the original 181.Pa foo 182and creates a replacement pointing to 183.Pa baz : 184.Bd -literal -offset indent 185$ mkdir bar baz 186$ ln -s bar foo 187$ ln -shf baz foo 188.Ed 189.Pp 190Without the 191.Fl h 192option, this would instead leave 193.Pa foo 194pointing to 195.Pa bar 196and inside 197.Pa foo 198create a new symlink 199.Pa baz 200pointing to itself. 201This results from directory-walking. 202.Sh SEE ALSO 203.Xr linkat 2 , 204.Xr lstat 2 , 205.Xr readlink 2 , 206.Xr stat 2 , 207.Xr symlink 2 , 208.Xr symlink 7 209.Sh STANDARDS 210The 211.Nm 212utility is compliant with the 213.St -p1003.1-2008 214specification. 215.Pp 216The flags 217.Op Fl hn 218are extensions to that specification. 219.Sh HISTORY 220An 221.Nm 222utility appeared in 223.At v3 . 224.Sh CAVEATS 225Since the 226.Ar source 227file must have its link count incremented, a hard link cannot be created to a 228file which is flagged immutable or append-only (see 229.Xr chflags 1 ) . 230