OSDN Git Service

(split) LDP: Update original to LDP v3.54
[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 .\"
4 .\" %%%LICENSE_START(BSD_3_CLAUSE_UCB)
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 .\" %%%LICENSE_END
29 .\"
30 .\"     @(#)symlink.7   8.3 (Berkeley) 3/31/94
31 .\" $FreeBSD: src/bin/ln/symlink.7,v 1.30 2005/02/13 22:25:09 ru Exp $
32 .\"
33 .\" 2008-06-11, mtk, Taken from FreeBSD 6.2 and heavily edited for
34 .\"     specific Linux details, improved readability, and man-pages style.
35 .\"
36 .TH SYMLINK 7 2008-06-18 "Linux" "Linux Programmer's Manual"
37 .SH NAME
38 symlink \- symbolic link handling
39 .SH SYMBOLIC LINK HANDLING
40 Symbolic links are files that act as pointers to other files.
41 To understand their behavior, you must first understand how hard links
42 work.
43
44 A hard link to a file is indistinguishable from the original file because
45 it is a reference to the object underlying the original filename.
46 (To be precise: each of the hard links to a file is a reference to
47 the same
48 .IR "i-node number" ,
49 where an i-node number is an index into the i-node table,
50 which contains metadata about all files on a filesystem.
51 See
52 .BR stat (2).)
53 Changes to a file are independent of the name used to reference the file.
54 Hard links may not refer to directories
55 (to prevent the possibility of loops within the filesystem tree,
56 which would confuse many programs)
57 and may not refer to files on different filesystems
58 (because i-node numbers are not unique across filesystems).
59
60 A symbolic link is a special type of file whose contents are a string
61 that is the pathname another file, the file to which the link refers.
62 In other words, a symbolic link is a pointer to another name,
63 and not to an underlying object.
64 For this reason, symbolic links may refer to directories and may cross
65 filesystem boundaries.
66
67 There is no requirement that the pathname referred to by a symbolic link
68 should exist.
69 A symbolic link that refers to a pathname that does not exist is said
70 to be a
71 .IR "dangling link" .
72
73 Because a symbolic link and its referenced object coexist in the filesystem
74 name space, confusion can arise in distinguishing between the link itself
75 and the referenced object.
76 On historical systems,
77 commands and system calls adopted their own link-following
78 conventions in a somewhat ad-hoc fashion.
79 Rules for a more uniform approach,
80 as they are implemented on Linux and other systems,
81 are outlined here.
82 It is important that site-local applications also conform to these rules,
83 so that the user interface can be as consistent as possible.
84 .SS Symbolic link ownership, permissions, and timestamps
85 The owner and group of an existing symbolic link can be changed
86 using
87 .BR lchown (2).
88 The only time that the ownership of a symbolic link matters is
89 when the link is being removed or renamed in a directory that
90 has the sticky bit set (see
91 .BR stat (2)).
92
93 The last access and last modification timestamps
94 of a symbolic link can be changed using
95 .BR utimensat (2)
96 or
97 .BR lutimes (3).
98
99 On Linux, the permissions of a symbolic link are not used
100 in any operations; the permissions are always
101 0777 (read, write, and execute for all user categories),
102 .\" Linux does not currently implement an lchmod(2).
103 and can't be changed.
104 .\"
105 .\" The
106 .\" 4.4BSD
107 .\" system differs from historical
108 .\" 4BSD
109 .\" systems in that the system call
110 .\" .BR chown (2)
111 .\" has been changed to follow symbolic links.
112 .\" The
113 .\" .BR lchown (2)
114 .\" system call was added later when the limitations of the new
115 .\" .BR chown (2)
116 .\" became apparent.
117 .SS Handling of symbolic links by system calls and commands
118 Symbolic links are handled either by operating on the link itself,
119 or by operating on the object referred to by the link.
120 In the latter case,
121 an application or system call is said to
122 .I follow
123 the link.
124 Symbolic links may refer to other symbolic links,
125 in which case the links are dereferenced until an object that is
126 not a symbolic link is found,
127 a symbolic link that refers to a file which does not exist is found,
128 or a loop is detected.
129 (Loop detection is done by placing an upper limit on the number of
130 links that may be followed, and an error results if this limit is
131 exceeded.)
132
133 There are three separate areas that need to be discussed.
134 They are as follows:
135 .IP 1. 3
136 Symbolic links used as filename arguments for system calls.
137 .IP 2.
138 Symbolic links specified as command-line arguments to utilities that
139 are not traversing a file tree.
140 .IP 3.
141 Symbolic links encountered by utilities that are traversing a file tree
142 (either specified on the command line or encountered as part of the
143 file hierarchy walk).
144 .SS System calls
145 The first area is symbolic links used as filename arguments for
146 system calls.
147
148 Except as noted below, all system calls follow symbolic links.
149 For example, if there were a symbolic link
150 .I slink
151 which pointed to a file named
152 .IR afile ,
153 the system call
154 .I "open(""slink"" ...\&)"
155 would return a file descriptor referring to the file
156 .IR afile .
157
158 Various system calls do not follow links, and operate
159 on the symbolic link itself.
160 They are:
161 .BR lchown (2),
162 .BR lgetxattr (2),
163 .BR llistxattr (2),
164 .BR lremovexattr (2),
165 .BR lsetxattr (2),
166 .BR lstat (2),
167 .BR readlink (2),
168 .BR rename (2),
169 .BR rmdir (2),
170 and
171 .BR unlink (2).
172 Certain other system calls optionally follow symbolic links.
173 They are:
174 .BR faccessat (2),
175 .\" Maybe one day: .BR fchownat (2)
176 .BR fchownat (2),
177 .BR fstatat (2),
178 .BR linkat (2),
179 .BR open (2),
180 .BR openat (2),
181 and
182 .BR utimensat (2);
183 see their manual pages for details.
184 Because
185 .BR remove (3)
186 is an alias for
187 .BR unlink (2),
188 that library function also does not follow symbolic links.
189 When
190 .BR rmdir (2)
191 is applied to a symbolic link, it fails with the error
192 .BR ENOTDIR .
193 The
194 .BR link (2)
195 warrants special discussion.
196 POSIX.1-2001 specifies that
197 .BR link (2)
198 should dereference
199 .I oldpath
200 if it is a symbolic link.
201 However, Linux does not do this.
202 (By default Solaris is the same,
203 but the POSIX.1-2001 specified behavior can be obtained with
204 suitable compiler options.)
205 The upcoming POSIX.1 revision changes the specification to allow
206 either behavior in an implementation.
207 .SS Commands not traversing a file tree
208 The second area is symbolic links, specified as command-line
209 filename arguments, to commands which are not traversing a file tree.
210
211 Except as noted below, commands follow symbolic links named as
212 command-line arguments.
213 For example, if there were a symbolic link
214 .I slink
215 which pointed to a file named
216 .IR afile ,
217 the command
218 .I "cat slink"
219 would display the contents of the file
220 .IR afile .
221
222 It is important to realize that this rule includes commands which may
223 optionally traverse file trees, e.g., the command
224 .I "chown file"
225 is included in this rule, while the command
226 .IR "chown\ \-R file" ,
227 which performs a tree traversal, is not.
228 (The latter is described in the third area, below.)
229
230 If it is explicitly intended that the command operate on the symbolic
231 link instead of following the symbolic link, e.g., it is desired that
232 .I "chown slink"
233 change the ownership of the file that
234 .I slink
235 is, whether it is a symbolic link or not, the
236 .I \-h
237 option should be used.
238 In the above example,
239 .I "chown root slink"
240 would change the ownership of the file referred to by
241 .IR slink ,
242 while
243 .I "chown\ \-h root slink"
244 would change the ownership of
245 .I slink
246 itself.
247
248 There are some exceptions to this rule:
249 .IP * 2
250 The
251 .BR mv (1)
252 and
253 .BR rm (1)
254 commands do not follow symbolic links named as arguments,
255 but respectively attempt to rename and delete them.
256 (Note, if the symbolic link references a file via a relative path,
257 moving it to another directory may very well cause it to stop working,
258 since the path may no longer be correct.)
259 .IP *
260 The
261 .BR ls (1)
262 command is also an exception to this rule.
263 For compatibility with historic systems (when
264 .BR ls (1)
265 is not doing a tree walk, i.e., the
266 .I \-R
267 option is not specified),
268 the
269 .BR ls (1)
270 command follows symbolic links named as arguments if the
271 .I \-H
272 or
273 .I \-L
274 option is specified,
275 or if the
276 .IR \-F ,
277 .IR \-d ,
278 or
279 .I \-l
280 options are not specified.
281 (The
282 .BR ls (1)
283 command is the only command where the
284 .I \-H
285 and
286 .I \-L
287 options affect its behavior even though it is not doing a walk of
288 a file tree.)
289 .IP *
290 The
291 .BR file (1)
292 command is also an exception to this rule.
293 The
294 .BR file (1)
295 command does not follow symbolic links named as argument by default.
296 The
297 .BR file (1)
298 command does follow symbolic links named as argument if the
299 .I \-L
300 option is specified.
301 .\"
302 .\"The 4.4BSD system differs from historical 4BSD systems in that the
303 .\".BR chown (1)
304 .\"and
305 .\".BR chgrp (1)
306 .\"commands follow symbolic links specified on the command line.
307 .SS Commands traversing a file tree
308 The following commands either optionally or always traverse file trees:
309 .BR chgrp (1),
310 .BR chmod (1),
311 .BR chown (1),
312 .BR cp (1),
313 .BR du (1),
314 .BR find (1),
315 .BR ls (1),
316 .BR pax (1),
317 .BR rm (1),
318 and
319 .BR tar (1).
320
321 It is important to realize that the following rules apply equally to
322 symbolic links encountered during the file tree traversal and symbolic
323 links listed as command-line arguments.
324
325 The \fIfirst rule\fP applies to symbolic links that reference files other
326 than directories.
327 Operations that apply to symbolic links are performed on the links
328 themselves, but otherwise the links are ignored.
329
330 The command
331 .I "rm\ \-r slink directory"
332 will remove
333 .IR slink ,
334 as well as any symbolic links encountered in the tree traversal of
335 .IR directory ,
336 because symbolic links may be removed.
337 In no case will
338 .BR rm (1)
339 affect the file referred to by
340 .IR slink .
341
342 The \fIsecond rule\fP applies to symbolic links that refer to directories.
343 Symbolic links that refer to directories are never followed by default.
344 This is often referred to as a "physical" walk, as opposed to a "logical"
345 walk (where symbolic links the refer to directories are followed).
346
347 Certain conventions are (should be) followed as consistently as
348 possible by commands that perform file tree walks:
349 .IP * 2
350 A command can be made to follow
351 any symbolic links named on the command line,
352 regardless of the type of file they reference, by specifying the
353 .I \-H
354 (for "half-logical") flag.
355 This flag is intended to make the command-line name space look
356 like the logical name space.
357 (Note, for commands that do not always do file tree traversals, the
358 .I \-H
359 flag will be ignored if the
360 .I \-R
361 flag is not also specified.)
362
363 For example, the command
364 .I "chown\ \-HR user slink"
365 will traverse the file hierarchy rooted in the file pointed to by
366 .IR slink .
367 Note, the
368 .I \-H
369 is not the same as the previously discussed
370 .I \-h
371 flag.
372 The
373 .I \-H
374 flag causes symbolic links specified on the command line to be
375 dereferenced for the purposes of both the action to be performed
376 and the tree walk, and it is as if the user had specified the
377 name of the file to which the symbolic link pointed.
378 .IP *
379 A command can be made to
380 follow any symbolic links named on the command line,
381 as well as any symbolic links encountered during the traversal,
382 regardless of the type of file they reference, by specifying the
383 .I \-L
384 (for "logical") flag.
385 This flag is intended to make the entire name space look like
386 the logical name space.
387 (Note, for commands that do not always do file tree traversals, the
388 .I \-L
389 flag will be ignored if the
390 .I \-R
391 flag is not also specified.)
392
393 For example, the command
394 .I "chown\ \-LR user slink"
395 will change the owner of the file referred to by
396 .IR slink .
397 If
398 .I slink
399 refers to a directory,
400 .B chown
401 will traverse the file hierarchy rooted in the directory that it
402 references.
403 In addition, if any symbolic links are encountered in any file tree that
404 .B chown
405 traverses, they will be treated in the same fashion as
406 .IR slink .
407 .IP *
408 A command can be made to
409 provide the default behavior by specifying the
410 .I \-P
411 (for "physical") flag.
412 This flag is intended to make the entire name space look like the
413 physical name space.
414 .PP
415 For commands that do not by default do file tree traversals, the
416 .IR \-H ,
417 .IR \-L ,
418 and
419 .I \-P
420 flags are ignored if the
421 .I \-R
422 flag is not also specified.
423 In addition, you may specify the
424 .IR \-H ,
425 .IR \-L ,
426 and
427 .I \-P
428 options more than once;
429 the last one specified determines the command's behavior.
430 This is intended to permit you to alias commands to behave one way
431 or the other, and then override that behavior on the command line.
432
433 The
434 .BR ls (1)
435 and
436 .BR rm (1)
437 commands have exceptions to these rules:
438 .IP * 2
439 The
440 .BR rm (1)
441 command operates on the symbolic link, and not the file it references,
442 and therefore never follows a symbolic link.
443 The
444 .BR rm (1)
445 command does not support the
446 .IR \-H ,
447 .IR \-L ,
448 or
449 .I \-P
450 options.
451 .IP *
452 To maintain compatibility with historic systems,
453 the
454 .BR ls (1)
455 command acts a little differently.
456 If you do not specify the
457 .IR \-F ,
458 .IR \-d
459 or
460 .I \-l
461 options,
462 .BR ls (1)
463 will follow symbolic links specified on the command line.
464 If the
465 .I \-L
466 flag is specified,
467 .BR ls (1)
468 follows all symbolic links,
469 regardless of their type,
470 whether specified on the command line or encountered in the tree walk.
471 .SH SEE ALSO
472 .BR chgrp (1),
473 .BR chmod (1),
474 .BR find (1),
475 .BR ln (1),
476 .BR ls (1),
477 .BR mv (1),
478 .BR rm (1),
479 .BR lchown (2),
480 .BR link (2),
481 .BR lstat (2),
482 .BR readlink (2),
483 .BR rename (2),
484 .BR symlink (2),
485 .BR unlink (2),
486 .BR utimensat (2),
487 .BR lutimes (3),
488 .BR path_resolution (7)