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