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