OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man7 / symlink.7
1 .\" Copyright (c) 1992, 1993, 1994
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\" and Copyright (C) 2008, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(BSD_3_CLAUSE_UCB)
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\" 4. Neither the name of the University nor the names of its contributors
15 .\"    may be used to endorse or promote products derived from this software
16 .\"    without specific prior written permission.
17 .\"
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" SUCH DAMAGE.
29 .\" %%%LICENSE_END
30 .\"
31 .\"     @(#)symlink.7   8.3 (Berkeley) 3/31/94
32 .\" $FreeBSD: src/bin/ln/symlink.7,v 1.30 2005/02/13 22:25:09 ru Exp $
33 .\"
34 .\" 2008-06-11, mtk, Taken from FreeBSD 6.2 and heavily edited for
35 .\"     specific Linux details, improved readability, and man-pages style.
36 .\"
37 .TH SYMLINK 7 2014-04-06 "Linux" "Linux Programmer's Manual"
38 .SH NAME
39 symlink \- symbolic link handling
40 .SH DESCRIPTION
41 Symbolic links are files that act as pointers to other files.
42 To understand their behavior, you must first understand how hard links
43 work.
44
45 A hard link to a file is indistinguishable from the original file because
46 it is a reference to the object underlying the original filename.
47 (To be precise: each of the hard links to a file is a reference to
48 the same
49 .IR "inode number" ,
50 where an inode number is an index into the inode table,
51 which contains metadata about all files on a filesystem.
52 See
53 .BR stat (2).)
54 Changes to a file are independent of the name used to reference the file.
55 Hard links may not refer to directories
56 (to prevent the possibility of loops within the filesystem tree,
57 which would confuse many programs)
58 and may not refer to files on different filesystems
59 (because inode numbers are not unique across filesystems).
60
61 A symbolic link is a special type of file whose contents are a string
62 that is the pathname of another file, the file to which the link refers.
63 (The contents of a symbolic link can be read using
64 .BR readlink (2).)
65 In other words, a symbolic link is a pointer to another name,
66 and not to an underlying object.
67 For this reason, symbolic links may refer to directories and may cross
68 filesystem boundaries.
69
70 There is no requirement that the pathname referred to by a symbolic link
71 should exist.
72 A symbolic link that refers to a pathname that does not exist is said
73 to be a
74 .IR "dangling link" .
75
76 Because a symbolic link and its referenced object coexist in the filesystem
77 name space, confusion can arise in distinguishing between the link itself
78 and the referenced object.
79 On historical systems,
80 commands and system calls adopted their own link-following
81 conventions in a somewhat ad-hoc fashion.
82 Rules for a more uniform approach,
83 as they are implemented on Linux and other systems,
84 are outlined here.
85 It is important that site-local applications also conform to these rules,
86 so that the user interface can be as consistent as possible.
87 .SS Symbolic link ownership, permissions, and timestamps
88 The owner and group of an existing symbolic link can be changed
89 using
90 .BR lchown (2).
91 The only time that the ownership of a symbolic link matters is
92 when the link is being removed or renamed in a directory that
93 has the sticky bit set (see
94 .BR stat (2)).
95
96 The last access and last modification timestamps
97 of a symbolic link can be changed using
98 .BR utimensat (2)
99 or
100 .BR lutimes (3).
101
102 On Linux, the permissions of a symbolic link are not used
103 in any operations; the permissions are always
104 0777 (read, write, and execute for all user categories),
105 .\" Linux does not currently implement an lchmod(2).
106 and can't be changed.
107 .\"
108 .\" The
109 .\" 4.4BSD
110 .\" system differs from historical
111 .\" 4BSD
112 .\" systems in that the system call
113 .\" .BR chown (2)
114 .\" has been changed to follow symbolic links.
115 .\" The
116 .\" .BR lchown (2)
117 .\" system call was added later when the limitations of the new
118 .\" .BR chown (2)
119 .\" became apparent.
120 .SS Obtaining a file descriptor that refers to a symbolic link
121 Using the combination of the
122 .B O_PATH
123 and
124 .BR O_NOFOLLOW
125 flags to
126 .BR open (2)
127 yields a file descriptor that can be passed as the
128 .IR dirfd
129 argument in system calls such as
130 .BR fstatat (2),
131 .BR fchownat (2),
132 .BR fchmodat (2),
133 .BR linkat (2),
134 and
135 .BR readlinkat (2),
136 in order to operate on the symbolic link itself
137 (rather than the file to which it refers).
138
139 By default
140 (i.e., if the
141 .BR AT_SYMLINK_FOLLOW
142 flag is not specified), if
143 .BR name_to_handle_at (2)
144 is applied to a symbolic link, it yields a handle for the symbolic link
145 (rather than the file to which it refers).
146 One can then obtain a file descriptor for the symbolic link
147 (rather than the file to which it refers)
148 by specifying the
149 .B O_PATH
150 flag in a subsequent call to
151 .BR open_by_handle_at (2).
152 Again, that file descriptor can be used in the
153 aforementioned system calls to operate on the symbolic link itself.
154 .SS Handling of symbolic links by system calls and commands
155 Symbolic links are handled either by operating on the link itself,
156 or by operating on the object referred to by the link.
157 In the latter case,
158 an application or system call is said to
159 .I follow
160 the link.
161 Symbolic links may refer to other symbolic links,
162 in which case the links are dereferenced until an object that is
163 not a symbolic link is found,
164 a symbolic link that refers to a file which does not exist is found,
165 or a loop is detected.
166 (Loop detection is done by placing an upper limit on the number of
167 links that may be followed, and an error results if this limit is
168 exceeded.)
169
170 There are three separate areas that need to be discussed.
171 They are as follows:
172 .IP 1. 3
173 Symbolic links used as filename arguments for system calls.
174 .IP 2.
175 Symbolic links specified as command-line arguments to utilities that
176 are not traversing a file tree.
177 .IP 3.
178 Symbolic links encountered by utilities that are traversing a file tree
179 (either specified on the command line or encountered as part of the
180 file hierarchy walk).
181 .SS System calls
182 The first area is symbolic links used as filename arguments for
183 system calls.
184
185 Except as noted below, all system calls follow symbolic links.
186 For example, if there were a symbolic link
187 .I slink
188 which pointed to a file named
189 .IR afile ,
190 the system call
191 .I "open(""slink"" ...\&)"
192 would return a file descriptor referring to the file
193 .IR afile .
194
195 Various system calls do not follow links, and operate
196 on the symbolic link itself.
197 They are:
198 .BR lchown (2),
199 .BR lgetxattr (2),
200 .BR llistxattr (2),
201 .BR lremovexattr (2),
202 .BR lsetxattr (2),
203 .BR lstat (2),
204 .BR readlink (2),
205 .BR rename (2),
206 .BR rmdir (2),
207 and
208 .BR unlink (2).
209
210 Certain other system calls optionally follow symbolic links.
211 They are:
212 .BR faccessat (2),
213 .\" Maybe one day: .BR fchownat (2)
214 .BR fchownat (2),
215 .BR fstatat (2),
216 .BR linkat (2),
217 .BR name_to_handle_at (2),
218 .BR open (2),
219 .BR openat (2),
220 .BR open_by_handle_at (2),
221 and
222 .BR utimensat (2);
223 see their manual pages for details.
224 Because
225 .BR remove (3)
226 is an alias for
227 .BR unlink (2),
228 that library function also does not follow symbolic links.
229 When
230 .BR rmdir (2)
231 is applied to a symbolic link, it fails with the error
232 .BR ENOTDIR .
233
234 The
235 .BR link (2)
236 warrants special discussion.
237 POSIX.1-2001 specifies that
238 .BR link (2)
239 should dereference
240 .I oldpath
241 if it is a symbolic link.
242 However, Linux does not do this.
243 (By default Solaris is the same,
244 but the POSIX.1-2001 specified behavior can be obtained with
245 suitable compiler options.)
246 The upcoming POSIX.1 revision changes the specification to allow
247 either behavior in an implementation.
248 .SS Commands not traversing a file tree
249 The second area is symbolic links, specified as command-line
250 filename arguments, to commands which are not traversing a file tree.
251
252 Except as noted below, commands follow symbolic links named as
253 command-line arguments.
254 For example, if there were a symbolic link
255 .I slink
256 which pointed to a file named
257 .IR afile ,
258 the command
259 .I "cat slink"
260 would display the contents of the file
261 .IR afile .
262
263 It is important to realize that this rule includes commands which may
264 optionally traverse file trees; for example, the command
265 .I "chown file"
266 is included in this rule, while the command
267 .IR "chown\ \-R file" ,
268 which performs a tree traversal, is not.
269 (The latter is described in the third area, below.)
270
271 If it is explicitly intended that the command operate on the symbolic
272 link instead of following the symbolic link\(emfor example, it is desired that
273 .I "chown slink"
274 change the ownership of the file that
275 .I slink
276 is, whether it is a symbolic link or not\(emthe
277 .I \-h
278 option should be used.
279 In the above example,
280 .I "chown root slink"
281 would change the ownership of the file referred to by
282 .IR slink ,
283 while
284 .I "chown\ \-h root slink"
285 would change the ownership of
286 .I slink
287 itself.
288
289 There are some exceptions to this rule:
290 .IP * 2
291 The
292 .BR mv (1)
293 and
294 .BR rm (1)
295 commands do not follow symbolic links named as arguments,
296 but respectively attempt to rename and delete them.
297 (Note, if the symbolic link references a file via a relative path,
298 moving it to another directory may very well cause it to stop working,
299 since the path may no longer be correct.)
300 .IP *
301 The
302 .BR ls (1)
303 command is also an exception to this rule.
304 For compatibility with historic systems (when
305 .BR ls (1)
306 is not doing a tree walk\(emthat is,
307 .I \-R
308 option is not specified),
309 the
310 .BR ls (1)
311 command follows symbolic links named as arguments if the
312 .I \-H
313 or
314 .I \-L
315 option is specified,
316 or if the
317 .IR \-F ,
318 .IR \-d ,
319 or
320 .I \-l
321 options are not specified.
322 (The
323 .BR ls (1)
324 command is the only command where the
325 .I \-H
326 and
327 .I \-L
328 options affect its behavior even though it is not doing a walk of
329 a file tree.)
330 .IP *
331 The
332 .BR file (1)
333 command is also an exception to this rule.
334 The
335 .BR file (1)
336 command does not follow symbolic links named as argument by default.
337 The
338 .BR file (1)
339 command does follow symbolic links named as argument if the
340 .I \-L
341 option is specified.
342 .\"
343 .\"The 4.4BSD system differs from historical 4BSD systems in that the
344 .\".BR chown (1)
345 .\"and
346 .\".BR chgrp (1)
347 .\"commands follow symbolic links specified on the command line.
348 .SS Commands traversing a file tree
349 The following commands either optionally or always traverse file trees:
350 .BR chgrp (1),
351 .BR chmod (1),
352 .BR chown (1),
353 .BR cp (1),
354 .BR du (1),
355 .BR find (1),
356 .BR ls (1),
357 .BR pax (1),
358 .BR rm (1),
359 and
360 .BR tar (1).
361
362 It is important to realize that the following rules apply equally to
363 symbolic links encountered during the file tree traversal and symbolic
364 links listed as command-line arguments.
365
366 The \fIfirst rule\fP applies to symbolic links that reference files other
367 than directories.
368 Operations that apply to symbolic links are performed on the links
369 themselves, but otherwise the links are ignored.
370
371 The command
372 .I "rm\ \-r slink directory"
373 will remove
374 .IR slink ,
375 as well as any symbolic links encountered in the tree traversal of
376 .IR directory ,
377 because symbolic links may be removed.
378 In no case will
379 .BR rm (1)
380 affect the file referred to by
381 .IR slink .
382
383 The \fIsecond rule\fP applies to symbolic links that refer to directories.
384 Symbolic links that refer to directories are never followed by default.
385 This is often referred to as a "physical" walk, as opposed to a "logical"
386 walk (where symbolic links the refer to directories are followed).
387
388 Certain conventions are (should be) followed as consistently as
389 possible by commands that perform file tree walks:
390 .IP * 2
391 A command can be made to follow
392 any symbolic links named on the command line,
393 regardless of the type of file they reference, by specifying the
394 .I \-H
395 (for "half-logical") flag.
396 This flag is intended to make the command-line name space look
397 like the logical name space.
398 (Note, for commands that do not always do file tree traversals, the
399 .I \-H
400 flag will be ignored if the
401 .I \-R
402 flag is not also specified.)
403
404 For example, the command
405 .I "chown\ \-HR user slink"
406 will traverse the file hierarchy rooted in the file pointed to by
407 .IR slink .
408 Note, the
409 .I \-H
410 is not the same as the previously discussed
411 .I \-h
412 flag.
413 The
414 .I \-H
415 flag causes symbolic links specified on the command line to be
416 dereferenced for the purposes of both the action to be performed
417 and the tree walk, and it is as if the user had specified the
418 name of the file to which the symbolic link pointed.
419 .IP *
420 A command can be made to
421 follow any symbolic links named on the command line,
422 as well as any symbolic links encountered during the traversal,
423 regardless of the type of file they reference, by specifying the
424 .I \-L
425 (for "logical") flag.
426 This flag is intended to make the entire name space look like
427 the logical name space.
428 (Note, for commands that do not always do file tree traversals, the
429 .I \-L
430 flag will be ignored if the
431 .I \-R
432 flag is not also specified.)
433
434 For example, the command
435 .I "chown\ \-LR user slink"
436 will change the owner of the file referred to by
437 .IR slink .
438 If
439 .I slink
440 refers to a directory,
441 .B chown
442 will traverse the file hierarchy rooted in the directory that it
443 references.
444 In addition, if any symbolic links are encountered in any file tree that
445 .B chown
446 traverses, they will be treated in the same fashion as
447 .IR slink .
448 .IP *
449 A command can be made to
450 provide the default behavior by specifying the
451 .I \-P
452 (for "physical") flag.
453 This flag is intended to make the entire name space look like the
454 physical name space.
455 .PP
456 For commands that do not by default do file tree traversals, the
457 .IR \-H ,
458 .IR \-L ,
459 and
460 .I \-P
461 flags are ignored if the
462 .I \-R
463 flag is not also specified.
464 In addition, you may specify the
465 .IR \-H ,
466 .IR \-L ,
467 and
468 .I \-P
469 options more than once;
470 the last one specified determines the command's behavior.
471 This is intended to permit you to alias commands to behave one way
472 or the other, and then override that behavior on the command line.
473
474 The
475 .BR ls (1)
476 and
477 .BR rm (1)
478 commands have exceptions to these rules:
479 .IP * 2
480 The
481 .BR rm (1)
482 command operates on the symbolic link, and not the file it references,
483 and therefore never follows a symbolic link.
484 The
485 .BR rm (1)
486 command does not support the
487 .IR \-H ,
488 .IR \-L ,
489 or
490 .I \-P
491 options.
492 .IP *
493 To maintain compatibility with historic systems,
494 the
495 .BR ls (1)
496 command acts a little differently.
497 If you do not specify the
498 .IR \-F ,
499 .IR \-d
500 or
501 .I \-l
502 options,
503 .BR ls (1)
504 will follow symbolic links specified on the command line.
505 If the
506 .I \-L
507 flag is specified,
508 .BR ls (1)
509 follows all symbolic links,
510 regardless of their type,
511 whether specified on the command line or encountered in the tree walk.
512 .SH SEE ALSO
513 .BR chgrp (1),
514 .BR chmod (1),
515 .BR find (1),
516 .BR ln (1),
517 .BR ls (1),
518 .BR mv (1),
519 .BR rm (1),
520 .BR lchown (2),
521 .BR link (2),
522 .BR lstat (2),
523 .BR readlink (2),
524 .BR rename (2),
525 .BR symlink (2),
526 .BR unlink (2),
527 .BR utimensat (2),
528 .BR lutimes (3),
529 .BR path_resolution (7)
530 .SH COLOPHON
531 This page is part of release 3.79 of the Linux
532 .I man-pages
533 project.
534 A description of the project,
535 information about reporting bugs,
536 and the latest version of this page,
537 can be found at
538 \%http://www.kernel.org/doc/man\-pages/.