xref: /freebsd/bin/ln/ln.1 (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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.Dd June 12, 2017
33.Dt LN 1
34.Os
35.Sh NAME
36.Nm ln ,
37.Nm link
38.Nd link files
39.Sh SYNOPSIS
40.Nm
41.Op Fl L | Fl P | Fl s Op Fl F
42.Op Fl f | iw
43.Op Fl hnv
44.Ar source_file
45.Op Ar target_file
46.Nm
47.Op Fl L | Fl P | Fl s Op Fl F
48.Op Fl f | iw
49.Op Fl hnv
50.Ar source_file ...
51.Ar target_dir
52.Nm link
53.Ar source_file Ar target_file
54.Sh DESCRIPTION
55The
56.Nm
57utility creates a new directory entry (linked file) for the file name
58specified by
59.Ar target_file .
60The
61.Ar target_file
62will be created with the same file modes as the
63.Ar source_file .
64It is useful for maintaining multiple copies of a file in many places
65at once without using up storage for the
66.Dq copies ;
67instead, a link
68.Dq points
69to the original copy.
70There are two types of links; hard links and symbolic links.
71How a link
72.Dq points
73to a file is one of the differences between a hard and symbolic link.
74.Pp
75The options are as follows:
76.Bl -tag -width flag
77.It Fl F
78If the target file already exists and is a directory, then remove it
79so that the link may occur.
80The
81.Fl F
82option should be used with either
83.Fl f
84or
85.Fl i
86options.
87If neither
88.Fl f
89nor
90.Fl i
91is specified,
92.Fl f
93is implied.
94The
95.Fl F
96option is a no-op unless
97.Fl s
98is specified.
99.It Fl L
100When creating a hard link to a symbolic link,
101create a hard link to the target of the symbolic link.
102This is the default.
103This option cancels the
104.Fl P
105option.
106.It Fl P
107When creating a hard link to a symbolic link,
108create a hard link to the symbolic link itself.
109This option cancels the
110.Fl L
111option.
112.It Fl f
113If the target file already exists,
114then unlink it so that the link may occur.
115(The
116.Fl f
117option overrides any previous
118.Fl i
119and
120.Fl w
121options.)
122.It Fl h
123If the
124.Ar target_file
125or
126.Ar target_dir
127is a symbolic link, do not follow it.
128This is most useful with the
129.Fl f
130option, to replace a symlink which may point to a directory.
131.It Fl i
132Cause
133.Nm
134to write a prompt to standard error if the target file exists.
135If the response from the standard input begins with the character
136.Sq Li y
137or
138.Sq Li Y ,
139then unlink the target file so that the link may occur.
140Otherwise, do not attempt the link.
141(The
142.Fl i
143option overrides any previous
144.Fl f
145options.)
146.It Fl n
147Same as
148.Fl h ,
149for compatibility with other
150.Nm
151implementations.
152.It Fl s
153Create a symbolic link.
154.It Fl v
155Cause
156.Nm
157to be verbose, showing files as they are processed.
158.It Fl w
159Warn if the source of a symbolic link does not currently exist.
160.El
161.Pp
162By default,
163.Nm
164makes
165.Em hard
166links.
167A hard link to a file is indistinguishable from the original directory entry;
168any changes to a file are effectively independent of the name used to reference
169the file.
170Directories may not be hardlinked, and hard links may not span file systems.
171.Pp
172A symbolic link contains the name of the file to
173which it is linked.
174The referenced file is used when an
175.Xr open 2
176operation is performed on the link.
177A
178.Xr stat 2
179on a symbolic link will return the linked-to file; an
180.Xr lstat 2
181must be done to obtain information about the link.
182The
183.Xr readlink 2
184call may be used to read the contents of a symbolic link.
185Symbolic links may span file systems and may refer to directories.
186.Pp
187Given one or two arguments,
188.Nm
189creates a link to an existing file
190.Ar source_file .
191If
192.Ar target_file
193is given, the link has that name;
194.Ar target_file
195may also be a directory in which to place the link;
196otherwise it is placed in the current directory.
197If only the directory is specified, the link will be made
198to the last component of
199.Ar source_file .
200.Pp
201Given more than two arguments,
202.Nm
203makes links in
204.Ar target_dir
205to all the named source files.
206The links made will have the same name as the files being linked to.
207.Pp
208When the utility is called as
209.Nm link ,
210exactly two arguments must be supplied,
211neither of which may specify a directory.
212No options may be supplied in this simple mode of operation,
213which performs a
214.Xr link 2
215operation using the two passed arguments.
216.Sh EXAMPLES
217Create a symbolic link named
218.Pa /home/src
219and point it to
220.Pa /usr/src :
221.Pp
222.Dl # ln -s /usr/src /home/src
223.Pp
224Hard link
225.Pa /usr/local/bin/fooprog
226to file
227.Pa /usr/local/bin/fooprog-1.0 :
228.Pp
229.Dl # ln /usr/local/bin/fooprog-1.0 /usr/local/bin/fooprog
230.Pp
231As an exercise, try the following commands:
232.Bd -literal -offset indent
233# ls -i /bin/[
23411553 /bin/[
235# ls -i /bin/test
23611553 /bin/test
237.Ed
238.Pp
239Note that both files have the same inode; that is,
240.Pa /bin/[
241is essentially an alias for the
242.Xr test 1
243command.
244This hard link exists so
245.Xr test 1
246may be invoked from shell scripts, for example, using the
247.Li "if [ ]"
248construct.
249.Pp
250In the next example, the second call to
251.Nm
252removes the original
253.Pa foo
254and creates a replacement pointing to
255.Pa baz :
256.Bd -literal -offset indent
257# mkdir bar baz
258# ln -s bar foo
259# ln -shf baz foo
260.Ed
261.Pp
262Without the
263.Fl h
264option, this would instead leave
265.Pa foo
266pointing to
267.Pa bar
268and inside
269.Pa foo
270create a new symlink
271.Pa baz
272pointing to itself.
273This results from directory-walking.
274.Pp
275An easy rule to remember is that the argument order for
276.Nm
277is the same as for
278.Xr cp 1 :
279The first argument needs to exist, the second one is created.
280.Sh COMPATIBILITY
281The
282.Fl h ,
283.Fl i ,
284.Fl n ,
285.Fl v
286and
287.Fl w
288options are non-standard and their use in scripts is not recommended.
289They are provided solely for compatibility with other
290.Nm
291implementations.
292.Pp
293The
294.Fl F
295option is a
296.Fx
297extension and should not be used in portable scripts.
298.Sh SEE ALSO
299.Xr link 2 ,
300.Xr lstat 2 ,
301.Xr readlink 2 ,
302.Xr stat 2 ,
303.Xr symlink 2 ,
304.Xr symlink 7
305.Sh STANDARDS
306The
307.Nm
308utility conforms to
309.St -p1003.2-92 .
310.Pp
311The simplified
312.Nm link
313command conforms to
314.St -susv2 .
315.Sh HISTORY
316An
317.Nm
318command appeared in
319.At v1 .
320