]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - bin/ln/symlink.7
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.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 .\" 4. 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$
31 .\"
32 .Dd April 25, 2010
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 fchmodat 2 ,
150 .Xr fchownat 2
151 and
152 .Xr fstatat 2 .
153 .Pp
154 The owner and group of an existing symbolic link can be changed by
155 means of the
156 .Xr lchown 2
157 system call.
158 The flags, access permissions, owner/group and modification time of
159 an existing symbolic link can be changed by means of the
160 .Xr lchflags 2 ,
161 .Xr lchmod 2 ,
162 .Xr lchown 2 ,
163 and
164 .Xr lutimes 2
165 system calls, respectively.
166 Of these, only the flags and ownership are used by the system;
167 the access permissions are ignored.
168 .Pp
169 The
170 .Bx 4.4
171 system differs from historical
172 .Bx 4
173 systems in that the system call
174 .Xr chown 2
175 has been changed to follow symbolic links.
176 The
177 .Xr lchown 2
178 system call was added later when the limitations of the new
179 .Xr chown 2
180 became apparent.
181 .Ss Commands not traversing a file tree.
182 The second area is symbolic links, specified as command line file
183 name arguments, to commands which are not traversing a file tree.
184 .Pp
185 Except as noted below, commands follow symbolic links named as command
186 line arguments.
187 For example, if there were a symbolic link
188 .Dq Li slink
189 which pointed to a file named
190 .Dq Li afile ,
191 the command
192 .Dq Li cat slink
193 would display the contents of the file
194 .Dq Li afile .
195 .Pp
196 It is important to realize that this rule includes commands which may
197 optionally traverse file trees, e.g.\& the command
198 .Dq Li "chown file"
199 is included in this rule, while the command
200 .Dq Li "chown -R file"
201 is not.
202 (The latter is described in the third area, below.)
203 .Pp
204 If it is explicitly intended that the command operate on the symbolic
205 link instead of following the symbolic link, e.g., it is desired that
206 .Dq Li "chown slink"
207 change the ownership of the file that
208 .Dq Li slink
209 is, whether it is a symbolic link or not, the
210 .Fl h
211 option should be used.
212 In the above example,
213 .Dq Li "chown root slink"
214 would change the ownership of the file referenced by
215 .Dq Li slink ,
216 while
217 .Dq Li "chown -h root slink"
218 would change the ownership of
219 .Dq Li slink
220 itself.
221 .Pp
222 There are four exceptions to this rule.
223 The
224 .Xr mv 1
225 and
226 .Xr rm 1
227 commands do not follow symbolic links named as arguments,
228 but respectively attempt to rename and delete them.
229 (Note, if the symbolic link references a file via a relative path,
230 moving it to another directory may very well cause it to stop working,
231 since the path may no longer be correct.)
232 .Pp
233 The
234 .Xr ls 1
235 command is also an exception to this rule.
236 For compatibility with historic systems (when
237 .Nm ls
238 is not doing a tree walk, i.e., the
239 .Fl R
240 option is not specified),
241 the
242 .Nm ls
243 command follows symbolic links named as arguments if the
244 .Fl H
245 or
246 .Fl L
247 option is specified,
248 or if the
249 .Fl F ,
250 .Fl d
251 or
252 .Fl l
253 options are not specified.
254 (The
255 .Nm ls
256 command is the only command where the
257 .Fl H
258 and
259 .Fl L
260 options affect its behavior even though it is not doing a walk of
261 a file tree.)
262 .Pp
263 The
264 .Xr file 1
265 command is also an exception to this rule.
266 The
267 .Xr file 1
268 command does not follow symbolic links named as argument by default.
269 The
270 .Xr file 1
271 command does follow symbolic links named as argument if
272 .Fl L
273 option is specified.
274 .Pp
275 The
276 .Bx 4.4
277 system differs from historical
278 .Bx 4
279 systems in that the
280 .Nm chown
281 and
282 .Nm chgrp
283 commands follow symbolic links specified on the command line.
284 .Ss Commands traversing a file tree.
285 The following commands either optionally or always traverse file trees:
286 .Xr chflags 1 ,
287 .Xr chgrp 1 ,
288 .Xr chmod 1 ,
289 .Xr cp 1 ,
290 .Xr du 1 ,
291 .Xr find 1 ,
292 .Xr ls 1 ,
293 .Xr pax 1 ,
294 .Xr rm 1 ,
295 .Xr tar 1
296 and
297 .Xr chown 8 .
298 .Pp
299 It is important to realize that the following rules apply equally to
300 symbolic links encountered during the file tree traversal and symbolic
301 links listed as command line arguments.
302 .Pp
303 The first rule applies to symbolic links that reference files that are
304 not of type directory.
305 Operations that apply to symbolic links are performed on the links
306 themselves, but otherwise the links are ignored.
307 .Pp
308 The command
309 .Dq Li "rm -r slink directory"
310 will remove
311 .Dq Li slink ,
312 as well as any symbolic links encountered in the tree traversal of
313 .Dq Li directory ,
314 because symbolic links may be removed.
315 In no case will
316 .Nm rm
317 affect the file which
318 .Dq Li slink
319 references in any way.
320 .Pp
321 The second rule applies to symbolic links that reference files of type
322 directory.
323 Symbolic links which reference files of type directory are never
324 .Dq followed
325 by default.
326 This is often referred to as a
327 .Dq physical
328 walk, as opposed to a
329 .Dq logical
330 walk (where symbolic links referencing directories are followed).
331 .Pp
332 As consistently as possible, you can make commands doing a file tree
333 walk follow any symbolic links named on the command line, regardless
334 of the type of file they reference, by specifying the
335 .Fl H
336 (for
337 .Dq half\-logical )
338 flag.
339 This flag is intended to make the command line name space look
340 like the logical name space.
341 (Note, for commands that do not always do file tree traversals, the
342 .Fl H
343 flag will be ignored if the
344 .Fl R
345 flag is not also specified.)
346 .Pp
347 For example, the command
348 .Dq Li "chown -HR user slink"
349 will traverse the file hierarchy rooted in the file pointed to by
350 .Dq Li slink .
351 Note, the
352 .Fl H
353 is not the same as the previously discussed
354 .Fl h
355 flag.
356 The
357 .Fl H
358 flag causes symbolic links specified on the command line to be
359 dereferenced both for the purposes of the action to be performed
360 and the tree walk, and it is as if the user had specified the
361 name of the file to which the symbolic link pointed.
362 .Pp
363 As consistently as possible, you can make commands doing a file tree
364 walk follow any symbolic links named on the command line, as well as
365 any symbolic links encountered during the traversal, regardless of
366 the type of file they reference, by specifying the
367 .Fl L
368 (for
369 .Dq logical )
370 flag.
371 This flag is intended to make the entire name space look like
372 the logical name space.
373 (Note, for commands that do not always do file tree traversals, the
374 .Fl L
375 flag will be ignored if the
376 .Fl R
377 flag is not also specified.)
378 .Pp
379 For example, the command
380 .Dq Li "chown -LR user slink"
381 will change the owner of the file referenced by
382 .Dq Li slink .
383 If
384 .Dq Li slink
385 references a directory,
386 .Nm chown
387 will traverse the file hierarchy rooted in the directory that it
388 references.
389 In addition, if any symbolic links are encountered in any file tree that
390 .Nm chown
391 traverses, they will be treated in the same fashion as
392 .Dq Li slink .
393 .Pp
394 As consistently as possible, you can specify the default behavior by
395 specifying the
396 .Fl P
397 (for
398 .Dq physical )
399 flag.
400 This flag is intended to make the entire name space look like the
401 physical name space.
402 .Pp
403 For commands that do not by default do file tree traversals, the
404 .Fl H ,
405 .Fl L
406 and
407 .Fl P
408 flags are ignored if the
409 .Fl R
410 flag is not also specified.
411 In addition, you may specify the
412 .Fl H ,
413 .Fl L
414 and
415 .Fl P
416 options more than once; the last one specified determines the
417 command's behavior.
418 This is intended to permit you to alias commands to behave one way
419 or the other, and then override that behavior on the command line.
420 .Pp
421 The
422 .Xr ls 1
423 and
424 .Xr rm 1
425 commands have exceptions to these rules.
426 The
427 .Nm rm
428 command operates on the symbolic link, and not the file it references,
429 and therefore never follows a symbolic link.
430 The
431 .Nm rm
432 command does not support the
433 .Fl H ,
434 .Fl L
435 or
436 .Fl P
437 options.
438 .Pp
439 To maintain compatibility with historic systems,
440 the
441 .Nm ls
442 command acts a little differently.
443 If you do not specify the
444 .Fl F ,
445 .Fl d
446 or
447 .Fl l
448 options,
449 .Nm ls
450 will follow symbolic links specified on the command line.
451 If the
452 .Fl L
453 flag is specified,
454 .Nm ls
455 follows all symbolic links,
456 regardless of their type,
457 whether specified on the command line or encountered in the tree walk.
458 .Sh SEE ALSO
459 .Xr chflags 1 ,
460 .Xr chgrp 1 ,
461 .Xr chmod 1 ,
462 .Xr cp 1 ,
463 .Xr du 1 ,
464 .Xr find 1 ,
465 .Xr ln 1 ,
466 .Xr ls 1 ,
467 .Xr mv 1 ,
468 .Xr pax 1 ,
469 .Xr rm 1 ,
470 .Xr tar 1 ,
471 .Xr lchflags 2 ,
472 .Xr lchmod 2 ,
473 .Xr lchown 2 ,
474 .Xr lstat 2 ,
475 .Xr lutimes 2 ,
476 .Xr readlink 2 ,
477 .Xr rename 2 ,
478 .Xr symlink 2 ,
479 .Xr unlink 2 ,
480 .Xr fts 3 ,
481 .Xr remove 3 ,
482 .Xr chown 8