2 .\" Copyright (c) 1992, 1993, 1994
3 .\" The Regents of the University of California. All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
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 .\" 3. 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.
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
29 .\" @(#)symlink.7 8.3 (Berkeley) 3/31/94
30 .\" $FreeBSD: head/bin/ln/symlink.7 278848 2015-02-16 12:56:55Z jilles $
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
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
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
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,
60 It is important that local applications conform to these rules, too,
61 so that the user interface can be as consistent as possible.
63 Symbolic links are handled either by operating on the link itself,
64 or by operating on the object referenced by the link.
66 an application or system call is said to
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
78 There are three separate areas that need to be discussed.
81 .Bl -enum -compact -offset indent
83 Symbolic links used as file name arguments for system calls.
85 Symbolic links specified as command line arguments to utilities that
86 are not traversing a file tree.
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
93 The first area is symbolic links used as file name arguments for
96 Except as noted below, all system calls follow symbolic links.
97 For example, if there were a symbolic link
99 which pointed to a file named
102 .Dq Li open("slink" ...\&)
103 would return a file descriptor to the file
106 There are thirteen system calls that do not follow links, and which operate
107 on the symbolic link itself.
127 it also does not follow symbolic links.
135 is applied to a symbolic link, it fails with the error
140 system call does not follow symbolic links
142 .Dv AT_SYMLINK_FOLLOW
145 The following system calls follow symbolic links
147 .Dv AT_SYMLINK_NOFOLLOW
157 The owner and group of an existing symbolic link can be changed by
161 The flags, access permissions, owner/group and modification time of
162 an existing symbolic link can be changed by means of the
168 system calls, respectively.
169 Of these, only the flags and ownership are used by the system;
170 the access permissions are ignored.
174 system differs from historical
176 systems in that the system call
178 has been changed to follow symbolic links.
181 system call was added later when the limitations of the new
184 .Ss Commands not traversing a file tree.
185 The second area is symbolic links, specified as command line file
186 name arguments, to commands which are not traversing a file tree.
188 Except as noted below, commands follow symbolic links named as command
190 For example, if there were a symbolic link
192 which pointed to a file named
196 would display the contents of the file
199 It is important to realize that this rule includes commands which may
200 optionally traverse file trees, e.g.\& the command
202 is included in this rule, while the command
203 .Dq Li "chown -R file"
205 (The latter is described in the third area, below.)
207 If it is explicitly intended that the command operate on the symbolic
208 link instead of following the symbolic link, e.g., it is desired that
210 change the ownership of the file that
212 is, whether it is a symbolic link or not, the
214 option should be used.
215 In the above example,
216 .Dq Li "chown root slink"
217 would change the ownership of the file referenced by
220 .Dq Li "chown -h root slink"
221 would change the ownership of
225 There are five exceptions to this rule.
230 commands do not follow symbolic links named as arguments,
231 but respectively attempt to rename and delete them.
232 (Note, if the symbolic link references a file via a relative path,
233 moving it to another directory may very well cause it to stop working,
234 since the path may no longer be correct.)
238 command is also an exception to this rule.
239 For compatibility with historic systems (when
241 is not doing a tree walk, i.e., the
243 option is not specified),
246 command follows symbolic links named as arguments if the
256 options are not specified.
259 command is the only command where the
263 options affect its behavior even though it is not doing a walk of
270 commands are also exceptions to this rule.
272 commands do not follow symbolic links named as argument by default,
273 but do follow symbolic links named as argument if the
279 system differs from historical
285 commands follow symbolic links specified on the command line.
286 .Ss Commands traversing a file tree.
287 The following commands either optionally or always traverse file trees:
301 It is important to realize that the following rules apply equally to
302 symbolic links encountered during the file tree traversal and symbolic
303 links listed as command line arguments.
305 The first rule applies to symbolic links that reference files that are
306 not of type directory.
307 Operations that apply to symbolic links are performed on the links
308 themselves, but otherwise the links are ignored.
311 .Dq Li "rm -r slink directory"
314 as well as any symbolic links encountered in the tree traversal of
316 because symbolic links may be removed.
319 affect the file which
321 references in any way.
323 The second rule applies to symbolic links that reference files of type
325 Symbolic links which reference files of type directory are never
328 This is often referred to as a
330 walk, as opposed to a
332 walk (where symbolic links referencing directories are followed).
334 As consistently as possible, you can make commands doing a file tree
335 walk follow any symbolic links named on the command line, regardless
336 of the type of file they reference, by specifying the
341 This flag is intended to make the command line name space look
342 like the logical name space.
343 (Note, for commands that do not always do file tree traversals, the
345 flag will be ignored if the
347 flag is not also specified.)
349 For example, the command
350 .Dq Li "chown -HR user slink"
351 will traverse the file hierarchy rooted in the file pointed to by
355 is not the same as the previously discussed
360 flag causes symbolic links specified on the command line to be
361 dereferenced both for the purposes of the action to be performed
362 and the tree walk, and it is as if the user had specified the
363 name of the file to which the symbolic link pointed.
365 As consistently as possible, you can make commands doing a file tree
366 walk follow any symbolic links named on the command line, as well as
367 any symbolic links encountered during the traversal, regardless of
368 the type of file they reference, by specifying the
373 This flag is intended to make the entire name space look like
374 the logical name space.
375 (Note, for commands that do not always do file tree traversals, the
377 flag will be ignored if the
379 flag is not also specified.)
381 For example, the command
382 .Dq Li "chown -LR user slink"
383 will change the owner of the file referenced by
387 references a directory,
389 will traverse the file hierarchy rooted in the directory that it
391 In addition, if any symbolic links are encountered in any file tree that
393 traverses, they will be treated in the same fashion as
396 As consistently as possible, you can specify the default behavior by
402 This flag is intended to make the entire name space look like the
405 For commands that do not by default do file tree traversals, the
410 flags are ignored if the
412 flag is not also specified.
413 In addition, you may specify the
418 options more than once; the last one specified determines the
420 This is intended to permit you to alias commands to behave one way
421 or the other, and then override that behavior on the command line.
427 commands have exceptions to these rules.
430 command operates on the symbolic link, and not the file it references,
431 and therefore never follows a symbolic link.
434 command does not support the
441 To maintain compatibility with historic systems,
444 command acts a little differently.
445 If you do not specify the
452 will follow symbolic links specified on the command line.
457 follows all symbolic links,
458 regardless of their type,
459 whether specified on the command line or encountered in the tree walk.