1.\" Copyright (c) 1992, 1993, 1994 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)symlink.7 8.3 (Berkeley) 3/31/94 33.\" $FreeBSD$ 34.\" 35.Dd March 31, 1994 36.Dt SYMLINK 7 37.Os 38.Sh NAME 39.Nm symlink 40.Nd symbolic link handling 41.Sh SYMBOLIC LINK HANDLING 42Symbolic links are files that act as pointers to other files. 43To understand their behavior, you must first understand how hard links 44work. 45A hard link to a file is indistinguishable from the original file because 46it is a reference to the object underlying the original file name. 47Changes to a file are independent of the name used to reference the 48file. 49Hard links may not refer to directories and may not reference files 50on different filesystems. 51A symbolic link contains the name of the file to which it is linked, 52i.e. it is a pointer to another name, and not to an underlying object. 53For this reason, symbolic links may reference directories and may span 54filesystems. 55.Pp 56Because a symbolic link and its referenced object coexist in the filesystem 57name space, confusion can arise in distinguishing between the link itself 58and the referenced object. 59Historically, commands and system calls have adopted their own link 60following conventions in a somewhat ad-hoc fashion. 61Rules for more a uniform approach, as they are implemented in this system, 62are outlined here. 63It is important that local applications conform to these rules, too, 64so that the user interface can be as consistent as possible. 65.Pp 66Symbolic links are handled either by operating on the link itself, 67or by operating on the object referenced by the link. 68In the latter case, 69an application or system call is said to 70.Dq follow 71the link. 72Symbolic links may reference other symbolic links, 73in which case the links are dereferenced until an object that is 74not a symbolic link is found, 75a symbolic link which references a file which doesn't exist is found, 76or a loop is detected. 77(Loop detection is done by placing an upper limit on the number of 78links that may be followed, and an error results if this limit is 79exceeded.) 80.Pp 81There are three separate areas that need to be discussed. 82They are as follows: 83.Pp 84.Bl -enum -compact -offset indent 85.It 86Symbolic links used as file name arguments for system calls. 87.It 88Symbolic links specified as command line arguments to utilities that 89are not traversing a file tree. 90.It 91Symbolic links encountered by utilities that are traversing a file tree 92(either specified on the command line or encountered as part of the 93file hierarchy walk). 94.El 95.Ss System calls. 96The first area is symbolic links used as file name arguments for 97system calls. 98.Pp 99Except as noted below, all system calls follow symbolic links. 100For example, if there were a symbolic link 101.Dq Li slink 102which pointed to a file named 103.Dq Li afile , 104the system call 105.Dq Li open("slink" ...\&) 106would return a file descriptor to the file 107.Dq afile . 108.Pp 109There are nine system calls that do not follow links, and which operate 110on the symbolic link itself. 111They are: 112.Xr lchflags 2 , 113.Xr lchmod 2 , 114.Xr lchown 2 , 115.Xr lstat 2 , 116.Xr lutimes 2 , 117.Xr readlink 2 , 118.Xr rename 2 , 119.Xr rmdir 2 , 120and 121.Xr unlink 2 . 122Because 123.Xr remove 3 124is an alias for 125.Xr unlink 2 , 126it also does not follow symbolic links. 127When 128.Xr rmdir 2 129is applied to a symbolic link, it fails with the error 130.Er ENOTDIR . 131.Pp 132The owner and group of an existing symbolic link can be changed by 133means of the 134.Xr lchown 2 135system call. 136The flags, access permissions, owner/group and modification time of 137an existing symbolic link can be changed by means of the 138.Xr lchflags 2 , 139.Xr lchmod 2 , 140.Xr lchown 2 , 141and 142.Xr lutimes 2 143system calls, respectively. 144Of these, only the flags are used by the system; 145the access permissions and ownership are ignored. 146.Pp 147The 148.Bx 4.4 149system differs from historical 150.Bx 4 151systems in that the system call 152.Xr chown 2 153has been changed to follow symbolic links. 154The 155.Xr lchown 2 156system call was added later when the limitations of the new 157.Xr chown 2 158became apparent. 159.Ss Commands not traversing a file tree. 160The second area is symbolic links, specified as command line file 161name arguments, to commands which are not traversing a file tree. 162.Pp 163Except as noted below, commands follow symbolic links named as command 164line arguments. 165For example, if there were a symbolic link 166.Dq Li slink 167which pointed to a file named 168.Dq Li afile , 169the command 170.Dq Li cat slink 171would display the contents of the file 172.Dq Li afile . 173.Pp 174It is important to realize that this rule includes commands which may 175optionally traverse file trees, e.g. the command 176.Dq Li "chown file" 177is included in this rule, while the command 178.Dq Li "chown -R file" 179is not. 180(The latter is described in the third area, below.) 181.Pp 182If it is explicitly intended that the command operate on the symbolic 183link instead of following the symbolic link, e.g., it is desired that 184.Dq Li "chown slink" 185change the ownership of the file that 186.Dq Li slink 187is, whether it is a symbolic link or not, the 188.Fl h 189option should be used. 190In the above example, 191.Dq Li "chown root slink" 192would change the ownership of the file referenced by 193.Dq Li slink , 194while 195.Dq Li "chown -h root slink" 196would change the ownership of 197.Dq Li slink 198itself. 199.Pp 200There are four exceptions to this rule. 201The 202.Xr mv 1 203and 204.Xr rm 1 205commands do not follow symbolic links named as arguments, 206but respectively attempt to rename and delete them. 207(Note, if the symbolic link references a file via a relative path, 208moving it to another directory may very well cause it to stop working, 209since the path may no longer be correct.) 210.Pp 211The 212.Xr ls 1 213command is also an exception to this rule. 214For compatibility with historic systems (when 215.Nm ls 216is not doing a tree walk, i.e. the 217.Fl R 218option is not specified), 219the 220.Nm ls 221command follows symbolic links named as arguments if the 222.Fl H 223or 224.Fl L 225option is specified, 226or if the 227.Fl F , 228.Fl d 229or 230.Fl l 231options are not specified. (The 232.Nm ls 233command is the only command where the 234.Fl H 235and 236.Fl L 237options affect its behavior even though it is not doing a walk of 238a file tree.) 239.Pp 240The 241.Xr file 1 242command is also an exception to this rule. 243The 244.Xr file 1 245command does not follow symbolic links named as argument by default. 246The 247.Xr file 1 248command does follow symbolic links named as argument if 249.Fl L 250option is specified. 251.Pp 252The 253.Bx 4.4 254system differs from historical 255.Bx 4 256systems in that the 257.Nm chown 258and 259.Nm chgrp 260commands follow symbolic links specified on the command line. 261.Ss Commands traversing a file tree. 262The following commands either optionally or always traverse file trees: 263.Xr chflags 1 , 264.Xr chgrp 1 , 265.Xr chmod 1 , 266.Xr cp 1 , 267.Xr du 1 , 268.Xr find 1 , 269.Xr ls 1 , 270.Xr pax 1 , 271.Xr rm 1 , 272.Xr tar 1 273and 274.Xr chown 8 . 275.Pp 276It is important to realize that the following rules apply equally to 277symbolic links encountered during the file tree traversal and symbolic 278links listed as command line arguments. 279.Pp 280The first rule applies to symbolic links that reference files that are 281not of type directory. 282Operations that apply to symbolic links are performed on the links 283themselves, but otherwise the links are ignored. 284.Pp 285The command 286.Dq Li "rm -r slink directory" 287will remove 288.Dq Li slink , 289as well as any symbolic links encountered in the tree traversal of 290.Dq Li directory , 291because symbolic links may be removed. 292In no case will 293.Nm rm 294affect the file which 295.Dq Li slink 296references in any way. 297.Pp 298The second rule applies to symbolic links that reference files of type 299directory. 300Symbolic links which reference files of type directory are never 301.Dq followed 302by default. 303This is often referred to as a 304.Dq physical 305walk, as opposed to a 306.Dq logical 307walk (where symbolic links referencing directories are followed). 308.Pp 309As consistently as possible, you can make commands doing a file tree 310walk follow any symbolic links named on the command line, regardless 311of the type of file they reference, by specifying the 312.Fl H 313(for 314.Dq half\-logical ) 315flag. 316This flag is intended to make the command line name space look 317like the logical name space. 318(Note, for commands that do not always do file tree traversals, the 319.Fl H 320flag will be ignored if the 321.Fl R 322flag is not also specified.) 323.Pp 324For example, the command 325.Dq Li "chown -HR user slink" 326will traverse the file hierarchy rooted in the file pointed to by 327.Dq Li slink . 328Note, the 329.Fl H 330is not the same as the previously discussed 331.Fl h 332flag. 333The 334.Fl H 335flag causes symbolic links specified on the command line to be 336dereferenced both for the purposes of the action to be performed 337and the tree walk, and it is as if the user had specified the 338name of the file to which the symbolic link pointed. 339.Pp 340As consistently as possible, you can make commands doing a file tree 341walk follow any symbolic links named on the command line, as well as 342any symbolic links encountered during the traversal, regardless of 343the type of file they reference, by specifying the 344.Fl L 345(for 346.Dq logical ) 347flag. 348This flag is intended to make the entire name space look like 349the logical name space. 350(Note, for commands that do not always do file tree traversals, the 351.Fl L 352flag will be ignored if the 353.Fl R 354flag is not also specified.) 355.Pp 356For example, the command 357.Dq Li "chown -LR user slink" 358will change the owner of the file referenced by 359.Dq Li slink . 360If 361.Dq Li slink 362references a directory, 363.Nm chown 364will traverse the file hierarchy rooted in the directory that it 365references. 366In addition, if any symbolic links are encountered in any file tree that 367.Nm chown 368traverses, they will be treated in the same fashion as 369.Dq Li slink . 370.Pp 371As consistently as possible, you can specify the default behavior by 372specifying the 373.Fl P 374(for 375.Dq physical ) 376flag. 377This flag is intended to make the entire name space look like the 378physical name space. 379.Pp 380For commands that do not by default do file tree traversals, the 381.Fl H , 382.Fl L 383and 384.Fl P 385flags are ignored if the 386.Fl R 387flag is not also specified. 388In addition, you may specify the 389.Fl H , 390.Fl L 391and 392.Fl P 393options more than once; the last one specified determines the 394command's behavior. 395This is intended to permit you to alias commands to behave one way 396or the other, and then override that behavior on the command line. 397.Pp 398The 399.Xr ls 1 400and 401.Xr rm 1 402commands have exceptions to these rules. 403The 404.Nm rm 405command operates on the symbolic link, and not the file it references, 406and therefore never follows a symbolic link. 407The 408.Nm rm 409command does not support the 410.Fl H , 411.Fl L 412or 413.Fl P 414options. 415.Pp 416To maintain compatibility with historic systems, 417the 418.Nm ls 419command acts a little differently. If you do not specify the 420.Fl F , 421.Fl d 422or 423.Fl l 424options, 425.Nm ls 426will follow symbolic links specified on the command line. If the 427.Fl L 428flag is specified, 429.Nm ls 430follows all symbolic links, 431regardless of their type, 432whether specified on the command line or encountered in the tree walk. 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 , 446.Xr lchflags 2 , 447.Xr lchmod 2 , 448.Xr lchown 2 , 449.Xr lstat 2 , 450.Xr lutimes 2 , 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 458