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