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