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