Merge branch 'vendor/LIBEDIT'
[dragonfly.git] / bin / ln / symlink.7
1 .\"-
2 .\" Copyright (c) 1992, 1993, 1994
3 .\"     The Regents of the University of California.  All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\" 3. Neither the name of the University nor the names of its contributors
14 .\"    may be used to endorse or promote products derived from this software
15 .\"    without specific prior written permission.
16 .\"
17 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 .\" SUCH DAMAGE.
28 .\"
29 .\"     @(#)symlink.7   8.3 (Berkeley) 3/31/94
30 .\" $FreeBSD: head/bin/ln/symlink.7 278848 2015-02-16 12:56:55Z jilles $
31 .\"
32 .Dd March 23, 2015
33 .Dt SYMLINK 7
34 .Os
35 .Sh NAME
36 .Nm symlink
37 .Nd symbolic link handling
38 .Sh SYMBOLIC LINK HANDLING
39 Symbolic links are files that act as pointers to other files.
40 To understand their behavior, you must first understand how hard links
41 work.
42 A hard link to a file is indistinguishable from the original file because
43 it is a reference to the object underlying the original file name.
44 Changes to a file are independent of the name used to reference the
45 file.
46 Hard links may not refer to directories and may not reference files
47 on different file systems.
48 A symbolic link contains the name of the file to which it is linked,
49 i.e., it is a pointer to another name, and not to an underlying object.
50 For this reason, symbolic links may reference directories and may span
51 file systems.
52 .Pp
53 Because a symbolic link and its referenced object coexist in the file system
54 name space, confusion can arise in distinguishing between the link itself
55 and the referenced object.
56 Historically, commands and system calls have adopted their own link
57 following conventions in a somewhat ad-hoc fashion.
58 Rules for more a uniform approach, as they are implemented in this system,
59 are outlined here.
60 It is important that local applications conform to these rules, too,
61 so that the user interface can be as consistent as possible.
62 .Pp
63 Symbolic links are handled either by operating on the link itself,
64 or by operating on the object referenced by the link.
65 In the latter case,
66 an application or system call is said to
67 .Dq follow
68 the link.
69 Symbolic links may reference other symbolic links,
70 in which case the links are dereferenced until an object that is
71 not a symbolic link is found,
72 a symbolic link which references a file which does not exist is found,
73 or a loop is detected.
74 (Loop detection is done by placing an upper limit on the number of
75 links that may be followed, and an error results if this limit is
76 exceeded.)
77 .Pp
78 There are three separate areas that need to be discussed.
79 They are as follows:
80 .Pp
81 .Bl -enum -compact -offset indent
82 .It
83 Symbolic links used as file name arguments for system calls.
84 .It
85 Symbolic links specified as command line arguments to utilities that
86 are not traversing a file tree.
87 .It
88 Symbolic links encountered by utilities that are traversing a file tree
89 (either specified on the command line or encountered as part of the
90 file hierarchy walk).
91 .El
92 .Ss System calls.
93 The first area is symbolic links used as file name arguments for
94 system calls.
95 .Pp
96 Except as noted below, all system calls follow symbolic links.
97 For example, if there were a symbolic link
98 .Dq Li slink
99 which pointed to a file named
100 .Dq Li afile ,
101 the system call
102 .Dq Li open("slink" ...\&)
103 would return a file descriptor to the file
104 .Dq afile .
105 .Pp
106 There are thirteen system calls that do not follow links, and which operate
107 on the symbolic link itself.
108 They are:
109 .Xr lchflags 2 ,
110 .Xr lchmod 2 ,
111 .Xr lchown 2 ,
112 .Xr lpathconf 2 ,
113 .Xr lstat 2 ,
114 .Xr lutimes 2 ,
115 .Xr readlink 2 ,
116 .Xr readlinkat 2 ,
117 .Xr rename 2 ,
118 .Xr renameat 2 ,
119 .Xr rmdir 2 ,
120 .Xr unlink 2 ,
121 and
122 .Xr unlinkat 2 .
123 Because
124 .Xr remove 3
125 is an alias for
126 .Xr unlink 2 ,
127 it also does not follow symbolic links.
128 When
129 .Xr rmdir 2
130 or
131 .Xr unlinkat 2
132 with the
133 .Dv AT_REMOVEDIR
134 flag
135 is applied to a symbolic link, it fails with the error
136 .Er ENOTDIR .
137 .Pp
138 The
139 .Xr linkat 2
140 system call does not follow symbolic links
141 unless given the
142 .Dv AT_SYMLINK_FOLLOW
143 flag.
144 .Pp
145 The following system calls follow symbolic links
146 unless given the
147 .Dv AT_SYMLINK_NOFOLLOW
148 flag:
149 .Xr chflagsat 2 ,
150 .Xr fchmodat 2 ,
151 .Xr fchownat 2 ,
152 and
153 .Xr fstatat 2 .
154 .\"and
155 .\".Xr utimensat 2 .
156 .Pp
157 The owner and group of an existing symbolic link can be changed by
158 means of the
159 .Xr lchown 2
160 system call.
161 The flags, access permissions, owner/group and modification time of
162 an existing symbolic link can be changed by means of the
163 .Xr lchflags 2 ,
164 .Xr lchmod 2 ,
165 .Xr lchown 2 ,
166 and
167 .Xr lutimes 2
168 system calls, respectively.
169 Of these, only the flags and ownership are used by the system;
170 the access permissions are ignored.
171 .Pp
172 The
173 .Bx 4.4
174 system differs from historical
175 .Bx 4
176 systems in that the system call
177 .Xr chown 2
178 has been changed to follow symbolic links.
179 The
180 .Xr lchown 2
181 system call was added later when the limitations of the new
182 .Xr chown 2
183 became apparent.
184 .Ss Commands not traversing a file tree.
185 The second area is symbolic links, specified as command line file
186 name arguments, to commands which are not traversing a file tree.
187 .Pp
188 Except as noted below, commands follow symbolic links named as command
189 line arguments.
190 For example, if there were a symbolic link
191 .Dq Li slink
192 which pointed to a file named
193 .Dq Li afile ,
194 the command
195 .Dq Li cat slink
196 would display the contents of the file
197 .Dq Li afile .
198 .Pp
199 It is important to realize that this rule includes commands which may
200 optionally traverse file trees, e.g.\& the command
201 .Dq Li "chown file"
202 is included in this rule, while the command
203 .Dq Li "chown -R file"
204 is not.
205 (The latter is described in the third area, below.)
206 .Pp
207 If it is explicitly intended that the command operate on the symbolic
208 link instead of following the symbolic link, e.g., it is desired that
209 .Dq Li "chown slink"
210 change the ownership of the file that
211 .Dq Li slink
212 is, whether it is a symbolic link or not, the
213 .Fl h
214 option should be used.
215 In the above example,
216 .Dq Li "chown root slink"
217 would change the ownership of the file referenced by
218 .Dq Li slink ,
219 while
220 .Dq Li "chown -h root slink"
221 would change the ownership of
222 .Dq Li slink
223 itself.
224 .Pp
225 There are five exceptions to this rule.
226 The
227 .Xr mv 1
228 and
229 .Xr rm 1
230 commands do not follow symbolic links named as arguments,
231 but respectively attempt to rename and delete them.
232 (Note, if the symbolic link references a file via a relative path,
233 moving it to another directory may very well cause it to stop working,
234 since the path may no longer be correct.)
235 .Pp
236 The
237 .Xr ls 1
238 command is also an exception to this rule.
239 For compatibility with historic systems (when
240 .Nm ls
241 is not doing a tree walk, i.e., the
242 .Fl R
243 option is not specified),
244 the
245 .Nm ls
246 command follows symbolic links named as arguments if the
247 .Fl H
248 or
249 .Fl L
250 option is specified,
251 or if the
252 .Fl F ,
253 .Fl d
254 or
255 .Fl l
256 options are not specified.
257 (The
258 .Nm ls
259 command is the only command where the
260 .Fl H
261 and
262 .Fl L
263 options affect its behavior even though it is not doing a walk of
264 a file tree.)
265 .Pp
266 The
267 .Xr file 1
268 and
269 .Xr stat 1
270 commands are also exceptions to this rule.
271 These
272 commands do not follow symbolic links named as argument by default,
273 but do follow symbolic links named as argument if the
274 .Fl L
275 option is specified.
276 .Pp
277 The
278 .Bx 4.4
279 system differs from historical
280 .Bx 4
281 systems in that the
282 .Nm chown
283 and
284 .Nm chgrp
285 commands follow symbolic links specified on the command line.
286 .Ss Commands traversing a file tree.
287 The following commands either optionally or always traverse file trees:
288 .Xr chflags 1 ,
289 .Xr chgrp 1 ,
290 .Xr chmod 1 ,
291 .Xr cp 1 ,
292 .Xr du 1 ,
293 .Xr find 1 ,
294 .Xr ls 1 ,
295 .Xr pax 1 ,
296 .Xr rm 1 ,
297 .Xr tar 1
298 and
299 .Xr chown 8 .
300 .Pp
301 It is important to realize that the following rules apply equally to
302 symbolic links encountered during the file tree traversal and symbolic
303 links listed as command line arguments.
304 .Pp
305 The first rule applies to symbolic links that reference files that are
306 not of type directory.
307 Operations that apply to symbolic links are performed on the links
308 themselves, but otherwise the links are ignored.
309 .Pp
310 The command
311 .Dq Li "rm -r slink directory"
312 will remove
313 .Dq Li slink ,
314 as well as any symbolic links encountered in the tree traversal of
315 .Dq Li directory ,
316 because symbolic links may be removed.
317 In no case will
318 .Nm rm
319 affect the file which
320 .Dq Li slink
321 references in any way.
322 .Pp
323 The second rule applies to symbolic links that reference files of type
324 directory.
325 Symbolic links which reference files of type directory are never
326 .Dq followed
327 by default.
328 This is often referred to as a
329 .Dq physical
330 walk, as opposed to a
331 .Dq logical
332 walk (where symbolic links referencing directories are followed).
333 .Pp
334 As consistently as possible, you can make commands doing a file tree
335 walk follow any symbolic links named on the command line, regardless
336 of the type of file they reference, by specifying the
337 .Fl H
338 (for
339 .Dq half\-logical )
340 flag.
341 This flag is intended to make the command line name space look
342 like the logical name space.
343 (Note, for commands that do not always do file tree traversals, the
344 .Fl H
345 flag will be ignored if the
346 .Fl R
347 flag is not also specified.)
348 .Pp
349 For example, the command
350 .Dq Li "chown -HR user slink"
351 will traverse the file hierarchy rooted in the file pointed to by
352 .Dq Li slink .
353 Note, the
354 .Fl H
355 is not the same as the previously discussed
356 .Fl h
357 flag.
358 The
359 .Fl H
360 flag causes symbolic links specified on the command line to be
361 dereferenced both for the purposes of the action to be performed
362 and the tree walk, and it is as if the user had specified the
363 name of the file to which the symbolic link pointed.
364 .Pp
365 As consistently as possible, you can make commands doing a file tree
366 walk follow any symbolic links named on the command line, as well as
367 any symbolic links encountered during the traversal, regardless of
368 the type of file they reference, by specifying the
369 .Fl L
370 (for
371 .Dq logical )
372 flag.
373 This flag is intended to make the entire name space look like
374 the logical name space.
375 (Note, for commands that do not always do file tree traversals, the
376 .Fl L
377 flag will be ignored if the
378 .Fl R
379 flag is not also specified.)
380 .Pp
381 For example, the command
382 .Dq Li "chown -LR user slink"
383 will change the owner of the file referenced by
384 .Dq Li slink .
385 If
386 .Dq Li slink
387 references a directory,
388 .Nm chown
389 will traverse the file hierarchy rooted in the directory that it
390 references.
391 In addition, if any symbolic links are encountered in any file tree that
392 .Nm chown
393 traverses, they will be treated in the same fashion as
394 .Dq Li slink .
395 .Pp
396 As consistently as possible, you can specify the default behavior by
397 specifying the
398 .Fl P
399 (for
400 .Dq physical )
401 flag.
402 This flag is intended to make the entire name space look like the
403 physical name space.
404 .Pp
405 For commands that do not by default do file tree traversals, the
406 .Fl H ,
407 .Fl L
408 and
409 .Fl P
410 flags are ignored if the
411 .Fl R
412 flag is not also specified.
413 In addition, you may specify the
414 .Fl H ,
415 .Fl L
416 and
417 .Fl P
418 options more than once; the last one specified determines the
419 command's behavior.
420 This is intended to permit you to alias commands to behave one way
421 or the other, and then override that behavior on the command line.
422 .Pp
423 The
424 .Xr ls 1
425 and
426 .Xr rm 1
427 commands have exceptions to these rules.
428 The
429 .Nm rm
430 command operates on the symbolic link, and not the file it references,
431 and therefore never follows a symbolic link.
432 The
433 .Nm rm
434 command does not support the
435 .Fl H ,
436 .Fl L
437 or
438 .Fl P
439 options.
440 .Pp
441 To maintain compatibility with historic systems,
442 the
443 .Nm ls
444 command acts a little differently.
445 If you do not specify the
446 .Fl F ,
447 .Fl d
448 or
449 .Fl l
450 options,
451 .Nm ls
452 will follow symbolic links specified on the command line.
453 If the
454 .Fl L
455 flag is specified,
456 .Nm ls
457 follows all symbolic links,
458 regardless of their type,
459 whether specified on the command line or encountered in the tree walk.
460 .Sh SEE ALSO
461 .Xr chflags 1 ,
462 .Xr chgrp 1 ,
463 .Xr chmod 1 ,
464 .Xr cp 1 ,
465 .Xr du 1 ,
466 .Xr find 1 ,
467 .Xr ln 1 ,
468 .Xr ls 1 ,
469 .Xr mv 1 ,
470 .Xr pax 1 ,
471 .Xr rm 1 ,
472 .Xr tar 1 ,
473 .Xr lchflags 2 ,
474 .Xr lchmod 2 ,
475 .Xr lchown 2 ,
476 .Xr lstat 2 ,
477 .Xr lutimes 2 ,
478 .Xr readlink 2 ,
479 .Xr rename 2 ,
480 .Xr renameat 2 ,
481 .Xr symlink 2 ,
482 .Xr unlink 2 ,
483 .Xr fts 3 ,
484 .Xr remove 3 ,
485 .Xr chown 8