xref: /freebsd/bin/ln/ln.1 (revision 0957b409)
1.\"-
2.\" Copyright (c) 1980, 1990, 1993
3.\"	The Regents of the University of California.  All rights reserved.
4.\"
5.\" This code is derived from software contributed to Berkeley by
6.\" the Institute of Electrical and Electronics Engineers, Inc.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	@(#)ln.1	8.2 (Berkeley) 12/30/93
33.\" $FreeBSD$
34.\"
35.Dd June 12, 2017
36.Dt LN 1
37.Os
38.Sh NAME
39.Nm ln ,
40.Nm link
41.Nd link files
42.Sh SYNOPSIS
43.Nm
44.Op Fl L | Fl P | Fl s Op Fl F
45.Op Fl f | iw
46.Op Fl hnv
47.Ar source_file
48.Op Ar target_file
49.Nm
50.Op Fl L | Fl P | Fl s Op Fl F
51.Op Fl f | iw
52.Op Fl hnv
53.Ar source_file ...
54.Ar target_dir
55.Nm link
56.Ar source_file Ar target_file
57.Sh DESCRIPTION
58The
59.Nm
60utility creates a new directory entry (linked file) for the file name
61specified by
62.Ar target_file .
63The
64.Ar target_file
65will be created with the same file modes as the
66.Ar source_file .
67It is useful for maintaining multiple copies of a file in many places
68at once without using up storage for the
69.Dq copies ;
70instead, a link
71.Dq points
72to the original copy.
73There are two types of links; hard links and symbolic links.
74How a link
75.Dq points
76to a file is one of the differences between a hard and symbolic link.
77.Pp
78The options are as follows:
79.Bl -tag -width flag
80.It Fl F
81If the target file already exists and is a directory, then remove it
82so that the link may occur.
83The
84.Fl F
85option should be used with either
86.Fl f
87or
88.Fl i
89options.
90If neither
91.Fl f
92nor
93.Fl i
94is specified,
95.Fl f
96is implied.
97The
98.Fl F
99option is a no-op unless
100.Fl s
101is specified.
102.It Fl L
103When creating a hard link to a symbolic link,
104create a hard link to the target of the symbolic link.
105This is the default.
106This option cancels the
107.Fl P
108option.
109.It Fl P
110When creating a hard link to a symbolic link,
111create a hard link to the symbolic link itself.
112This option cancels the
113.Fl L
114option.
115.It Fl f
116If the target file already exists,
117then unlink it so that the link may occur.
118(The
119.Fl f
120option overrides any previous
121.Fl i
122and
123.Fl w
124options.)
125.It Fl h
126If the
127.Ar target_file
128or
129.Ar target_dir
130is a symbolic link, do not follow it.
131This is most useful with the
132.Fl f
133option, to replace a symlink which may point to a directory.
134.It Fl i
135Cause
136.Nm
137to write a prompt to standard error if the target file exists.
138If the response from the standard input begins with the character
139.Sq Li y
140or
141.Sq Li Y ,
142then unlink the target file so that the link may occur.
143Otherwise, do not attempt the link.
144(The
145.Fl i
146option overrides any previous
147.Fl f
148options.)
149.It Fl n
150Same as
151.Fl h ,
152for compatibility with other
153.Nm
154implementations.
155.It Fl s
156Create a symbolic link.
157.It Fl v
158Cause
159.Nm
160to be verbose, showing files as they are processed.
161.It Fl w
162Warn if the source of a symbolic link does not currently exist.
163.El
164.Pp
165By default,
166.Nm
167makes
168.Em hard
169links.
170A hard link to a file is indistinguishable from the original directory entry;
171any changes to a file are effectively independent of the name used to reference
172the file.
173Directories may not be hardlinked, and hard links may not span file systems.
174.Pp
175A symbolic link contains the name of the file to
176which it is linked.
177The referenced file is used when an
178.Xr open 2
179operation is performed on the link.
180A
181.Xr stat 2
182on a symbolic link will return the linked-to file; an
183.Xr lstat 2
184must be done to obtain information about the link.
185The
186.Xr readlink 2
187call may be used to read the contents of a symbolic link.
188Symbolic links may span file systems and may refer to directories.
189.Pp
190Given one or two arguments,
191.Nm
192creates a link to an existing file
193.Ar source_file .
194If
195.Ar target_file
196is given, the link has that name;
197.Ar target_file
198may also be a directory in which to place the link;
199otherwise it is placed in the current directory.
200If only the directory is specified, the link will be made
201to the last component of
202.Ar source_file .
203.Pp
204Given more than two arguments,
205.Nm
206makes links in
207.Ar target_dir
208to all the named source files.
209The links made will have the same name as the files being linked to.
210.Pp
211When the utility is called as
212.Nm link ,
213exactly two arguments must be supplied,
214neither of which may specify a directory.
215No options may be supplied in this simple mode of operation,
216which performs a
217.Xr link 2
218operation using the two passed arguments.
219.Sh EXAMPLES
220Create a symbolic link named
221.Pa /home/src
222and point it to
223.Pa /usr/src :
224.Pp
225.Dl # ln -s /usr/src /home/src
226.Pp
227Hard link
228.Pa /usr/local/bin/fooprog
229to file
230.Pa /usr/local/bin/fooprog-1.0 :
231.Pp
232.Dl # ln /usr/local/bin/fooprog-1.0 /usr/local/bin/fooprog
233.Pp
234As an exercise, try the following commands:
235.Bd -literal -offset indent
236# ls -i /bin/[
23711553 /bin/[
238# ls -i /bin/test
23911553 /bin/test
240.Ed
241.Pp
242Note that both files have the same inode; that is,
243.Pa /bin/[
244is essentially an alias for the
245.Xr test 1
246command.
247This hard link exists so
248.Xr test 1
249may be invoked from shell scripts, for example, using the
250.Li "if [ ]"
251construct.
252.Pp
253In the next example, the second call to
254.Nm
255removes the original
256.Pa foo
257and creates a replacement pointing to
258.Pa baz :
259.Bd -literal -offset indent
260# mkdir bar baz
261# ln -s bar foo
262# ln -shf baz foo
263.Ed
264.Pp
265Without the
266.Fl h
267option, this would instead leave
268.Pa foo
269pointing to
270.Pa bar
271and inside
272.Pa foo
273create a new symlink
274.Pa baz
275pointing to itself.
276This results from directory-walking.
277.Pp
278An easy rule to remember is that the argument order for
279.Nm
280is the same as for
281.Xr cp 1 :
282The first argument needs to exist, the second one is created.
283.Sh COMPATIBILITY
284The
285.Fl h ,
286.Fl i ,
287.Fl n ,
288.Fl v
289and
290.Fl w
291options are non-standard and their use in scripts is not recommended.
292They are provided solely for compatibility with other
293.Nm
294implementations.
295.Pp
296The
297.Fl F
298option is a
299.Fx
300extension and should not be used in portable scripts.
301.Sh SEE ALSO
302.Xr link 2 ,
303.Xr lstat 2 ,
304.Xr readlink 2 ,
305.Xr stat 2 ,
306.Xr symlink 2 ,
307.Xr symlink 7
308.Sh STANDARDS
309The
310.Nm
311utility conforms to
312.St -p1003.2-92 .
313.Pp
314The simplified
315.Nm link
316command conforms to
317.St -susv2 .
318.Sh HISTORY
319An
320.Nm
321command appeared in
322.At v1 .
323