]>
Commit | Line | Data |
---|---|---|
44a7a5ab A |
1 | .\" $NetBSD: symlink.7,v 1.7 1998/02/06 05:39:47 perry Exp $ |
2 | .\" | |
3 | .\" Copyright (c) 1992, 1993, 1994 | |
4 | .\" The Regents of the University of California. All rights reserved. | |
5 | .\" | |
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 | .\" 3. All advertising materials mentioning features or use of this software | |
15 | .\" must display the following acknowledgement: | |
16 | .\" This product includes software developed by the University of | |
17 | .\" California, Berkeley and its contributors. | |
18 | .\" 4. Neither the name of the University nor the names of its contributors | |
19 | .\" may be used to endorse or promote products derived from this software | |
20 | .\" without specific prior written permission. | |
21 | .\" | |
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | .\" SUCH DAMAGE. | |
33 | .\" | |
34 | .\" @(#)symlink.7 8.3 (Berkeley) 3/31/94 | |
35 | .\" | |
36 | .Dd March 31, 1994 | |
37 | .Dt SYMLINK 7 | |
38 | .Os BSD 4 | |
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 filesystem | |
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 | .sp | |
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 four system calls that do not follow links, and which operate | |
111 | on the symbolic link itself. | |
112 | They are: | |
113 | .Xr lstat 2 , | |
114 | .Xr readlink 2 , | |
115 | .Xr rename 2 , | |
116 | and | |
117 | .Xr unlink 2 . | |
118 | Because | |
119 | .Xr remove 3 | |
120 | is an alias for | |
121 | .Xr unlink 2 , | |
122 | it also does not follow symbolic links. | |
123 | .Pp | |
124 | The | |
125 | .Bx 4.4 | |
126 | system differs from historical | |
127 | .Bx 4 | |
128 | systems in that the system call | |
129 | .Xr chown 2 | |
130 | has been changed to follow symbolic links. | |
131 | .Ss Commands not traversing a file tree. | |
132 | The second area is symbolic links, specified as command line file | |
133 | name arguments, to commands which are not traversing a file tree. | |
134 | .Pp | |
135 | Except as noted below, commands follow symbolic links named as command | |
136 | line arguments. | |
137 | For example, if there were a symbolic link | |
138 | .Dq Li slink | |
139 | which pointed to a file named | |
140 | .Dq Li afile , | |
141 | the command | |
142 | .Dq Li cat slink | |
143 | would display the contents of the file | |
144 | .Dq Li afile . | |
145 | .Pp | |
146 | It is important to realize that this rule includes commands which may | |
147 | optionally traverse file trees, e.g. the command | |
148 | .Dq Li "chown file" | |
149 | is included in this rule, while the command | |
150 | .Dq Li "chown -R file" | |
151 | is not. | |
152 | (The latter is described in the third area, below.) | |
153 | .Pp | |
154 | If it is explicitly intended that the command operate on the symbolic | |
155 | link instead of following the symbolic link, e.g., it is desired that | |
156 | .Dq Li "file slink" | |
157 | display the type of file that | |
158 | .Dq Li slink | |
159 | is, whether it is a symbolic link or not, the | |
160 | .Fl h | |
161 | option should be used. | |
162 | In the above example, | |
163 | .Dq Li "file slink" | |
164 | would report the type of the file referenced by | |
165 | .Dq Li slink , | |
166 | while | |
167 | .Dq Li "file -h slink" | |
168 | would report that | |
169 | .Dq Li slink | |
170 | was a symbolic link. | |
171 | .Pp | |
172 | There are three exceptions to this rule. | |
173 | The | |
174 | .Xr mv 1 | |
175 | and | |
176 | .Xr rm 1 | |
177 | commands do not follow symbolic links named as arguments, | |
178 | but respectively attempt to rename and delete them. | |
179 | (Note, if the symbolic link references a file via a relative path, | |
180 | moving it to another directory may very well cause it to stop working, | |
181 | since the path may no longer be correct.) | |
182 | .Pp | |
183 | The | |
184 | .Xr ls 1 | |
185 | command is also an exception to this rule. | |
186 | For compatibility with historic systems (when | |
187 | .Nm ls | |
188 | is not doing a tree walk, i.e. the | |
189 | .Fl R | |
190 | option is not specified), | |
191 | the | |
192 | .Nm ls | |
193 | command follows symbolic links named as arguments if the | |
194 | .Fl L | |
195 | option is specified, | |
196 | or if the | |
197 | .Fl F , | |
198 | .Fl d | |
199 | or | |
200 | .Fl l | |
201 | options are not specified. | |
202 | (If the | |
203 | .Fl L | |
204 | option is specified, | |
205 | .Nm ls | |
206 | always follows symbolic links. | |
207 | .Nm ls | |
208 | is the only command where the | |
209 | .Fl L | |
210 | option affects its behavior even though it is not doing a walk of | |
211 | a file tree.) | |
212 | .Pp | |
213 | The | |
214 | .Bx 4.4 | |
215 | system differs from historical | |
216 | .Bx 4 | |
217 | systems in that the | |
218 | .Nm chown , | |
219 | .Nm chgrp | |
220 | and | |
221 | .Nm file | |
222 | commands follow symbolic links specified on the command line. | |
223 | .Ss Commands traversing a file tree. | |
224 | The following commands either optionally or always traverse file trees: | |
225 | .Xr chflags 1 , | |
226 | .Xr chgrp 1 , | |
227 | .Xr chmod 1 , | |
228 | .Xr cp 1 , | |
229 | .Xr du 1 , | |
230 | .Xr find 1 , | |
231 | .Xr ls 1 , | |
232 | .Xr pax 1 , | |
233 | .Xr rm 1 , | |
234 | .Xr tar 1 | |
235 | and | |
236 | .Xr chown 8 . | |
237 | .Pp | |
238 | It is important to realize that the following rules apply equally to | |
239 | symbolic links encountered during the file tree traversal and symbolic | |
240 | links listed as command line arguments. | |
241 | .Pp | |
242 | The first rule applies to symbolic links that reference files that are | |
243 | not of type directory. | |
244 | Operations that apply to symbolic links are performed on the links | |
245 | themselves, but otherwise the links are ignored. | |
246 | .Pp | |
247 | For example, the command | |
248 | .Dq Li "chown -R user slink directory" | |
249 | will ignore | |
250 | .Dq Li slink , | |
251 | because symbolic links in this system do not have owners. | |
252 | Any symbolic links encountered during the tree traversal will also be | |
253 | ignored. | |
254 | The command | |
255 | .Dq Li "rm -r slink directory" | |
256 | will remove | |
257 | .Dq Li slink , | |
258 | as well as any symbolic links encountered in the tree traversal of | |
259 | .Dq Li directory , | |
260 | because symbolic links may be removed. | |
261 | In no case will either | |
262 | .Nm chown | |
263 | or | |
264 | .Nm rm | |
265 | affect the file which | |
266 | .Dq Li slink | |
267 | references in any way. | |
268 | .Pp | |
269 | The second rule applies to symbolic links that reference files of type | |
270 | directory. | |
271 | Symbolic links which reference files of type directory are never | |
272 | .Dq followed | |
273 | by default. | |
274 | This is often referred to as a | |
275 | .Dq physical | |
276 | walk, as opposed to a | |
277 | .Dq logical | |
278 | walk (where symbolic links referencing directories are followed). | |
279 | .Pp | |
280 | As consistently as possible, you can make commands doing a file tree | |
281 | walk follow any symbolic links named on the command line, regardless | |
282 | of the type of file they reference, by specifying the | |
283 | .Fl H | |
284 | (for | |
285 | .Dq half\-logical ) | |
286 | flag. | |
287 | This flag is intended to make the command line name space look | |
288 | like the logical name space. | |
289 | (Note, for commands that do not always do file tree traversals, the | |
290 | .Fl H | |
291 | flag will be ignored if the | |
292 | .Fl R | |
293 | flag is not also specified.) | |
294 | .Pp | |
295 | For example, the command | |
296 | .Dq Li "chown -HR user slink" | |
297 | will traverse the file hierarchy rooted in the file pointed to by | |
298 | .Dq Li slink . | |
299 | Note, the | |
300 | .Fl H | |
301 | is not the same as the previously discussed | |
302 | .Fl h | |
303 | flag. | |
304 | The | |
305 | .Fl H | |
306 | flag causes symbolic links specified on the command line to be | |
307 | dereferenced both for the purposes of the action to be performed | |
308 | and the tree walk, and it is as if the user had specified the | |
309 | name of the file to which the symbolic link pointed. | |
310 | .Pp | |
311 | As consistently as possible, you can make commands doing a file tree | |
312 | walk follow any symbolic links named on the command line, as well as | |
313 | any symbolic links encountered during the traversal, regardless of | |
314 | the type of file they reference, by specifying the | |
315 | .Fl L | |
316 | (for | |
317 | .Dq logical ) | |
318 | flag. | |
319 | This flag is intended to make the entire name space look like | |
320 | the logical name space. | |
321 | (Note, for commands that do not always do file tree traversals, the | |
322 | .Fl L | |
323 | flag will be ignored if the | |
324 | .Fl R | |
325 | flag is not also specified.) | |
326 | .Pp | |
327 | For example, the command | |
328 | .Dq Li "chown -LR user slink" | |
329 | will change the owner of the file referenced by | |
330 | .Dq Li slink . | |
331 | If | |
332 | .Dq Li slink | |
333 | references a directory, | |
334 | .Nm chown | |
335 | will traverse the file hierarchy rooted in the directory that it | |
336 | references. | |
337 | In addition, if any symbolic links are encountered in any file tree that | |
338 | .Nm chown | |
339 | traverses, they will be treated in the same fashion as | |
340 | .Dq Li slink . | |
341 | .Pp | |
342 | As consistently as possible, you can specify the default behavior by | |
343 | specifying the | |
344 | .Fl P | |
345 | (for | |
346 | .Dq physical ) | |
347 | flag. | |
348 | This flag is intended to make the entire name space look like the | |
349 | physical name space. | |
350 | .Pp | |
351 | For commands that do not by default do file tree traversals, the | |
352 | .Fl H , | |
353 | .Fl L | |
354 | and | |
355 | .Fl P | |
356 | flags are ignored if the | |
357 | .Fl R | |
358 | flag is not also specified. | |
359 | In addition, you may specify the | |
360 | .Fl H , | |
361 | .Fl L | |
362 | and | |
363 | .Fl P | |
364 | options more than once; the last one specified determines the | |
365 | command's behavior. | |
366 | This is intended to permit you to alias commands to behave one way | |
367 | or the other, and then override that behavior on the command line. | |
368 | .Pp | |
369 | The | |
370 | .Xr ls 1 | |
371 | and | |
372 | .Xr rm 1 | |
373 | commands have exceptions to these rules. | |
374 | The | |
375 | .Nm rm | |
376 | command operates on the symbolic link, and not the file it references, | |
377 | and therefore never follows a symbolic link. | |
378 | The | |
379 | .Nm rm | |
380 | command does not support the | |
381 | .Fl H , | |
382 | .Fl L | |
383 | or | |
384 | .Fl P | |
385 | options. | |
386 | .Pp | |
387 | To maintain compatibility with historic systems, | |
388 | the | |
389 | .Nm ls | |
390 | command never follows symbolic links unless the | |
391 | .Fl L | |
392 | flag is specified. | |
393 | If the | |
394 | .Fl L | |
395 | flag is specified, | |
396 | .Nm ls | |
397 | follows all symbolic links, | |
398 | regardless of their type, | |
399 | whether specified on the command line or encountered in the tree walk. | |
400 | The | |
401 | .Nm ls | |
402 | command does not support the | |
403 | .Fl H | |
404 | or | |
405 | .Fl P | |
406 | options. | |
407 | .Sh SEE ALSO | |
408 | .Xr chflags 1 , | |
409 | .Xr chgrp 1 , | |
410 | .Xr chmod 1 , | |
411 | .Xr cp 1 , | |
412 | .Xr du 1 , | |
413 | .Xr find 1 , | |
414 | .Xr ln 1 , | |
415 | .Xr ls 1 , | |
416 | .Xr mv 1 , | |
417 | .Xr pax 1 , | |
418 | .Xr rm 1 , | |
419 | .Xr tar 1 , | |
420 | .Xr lstat 2 , | |
421 | .Xr readlink 2 , | |
422 | .Xr rename 2 , | |
423 | .Xr symlink 2 , | |
424 | .Xr unlink 2 , | |
425 | .Xr fts 3 , | |
426 | .Xr remove 3 , | |
427 | .Xr chown 8 |