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