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