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.\" 34.Dd March 31, 1994 35.Dt SYMLINK 7 36.Os BSD 4 37.Sh NAME 38.Nm symlink 39.Nd symbolic link handling 40.Sh SYMBOLIC LINK HANDLING 41Symbolic links are files that act as pointers to other files. 42To understand their behavior, you must first understand how hard links 43work. 44A hard link to a file is indistinguishable from the original file because 45it is a reference to the object underlying the original file name. 46Changes to a file are independent of the name used to reference the 47file. 48Hard links may not refer to directories and may not reference files 49on different file systems. 50A symbolic link contains the name of the file to which it is linked, 51i.e. it is a pointer to another name, and not to an underlying object. 52For this reason, symbolic links may reference directories and may span 53file systems. 54.Pp 55Because a symbolic link and its referenced object coexist in the filesystem 56name space, confusion can arise in distinguishing between the link itself 57and the referenced object. 58Historically, commands and system calls have adopted their own link 59following conventions in a somewhat ad-hoc fashion. 60Rules for more a uniform approach, as they are implemented in this system, 61are outlined here. 62It is important that local applications conform to these rules, too, 63so that the user interface can be as consistent as possible. 64.Pp 65Symbolic links are handled either by operating on the link itself, 66or by operating on the object referenced by the link. 67In the latter case, 68an application or system call is said to 69.Dq follow 70the link. 71Symbolic links may reference other symbolic links, 72in which case the links are dereferenced until an object that is 73not a symbolic link is found, 74a symbolic link which references a file which doesn't exist is found, 75or a loop is detected. 76(Loop detection is done by placing an upper limit on the number of 77links that may be followed, and an error results if this limit is 78exceeded.) 79.Pp 80There are three separate areas that need to be discussed. 81They are as follows: 82.sp 83.Bl -enum -compact -offset indent 84.It 85Symbolic links used as file name arguments for system calls. 86.It 87Symbolic links specified as command line arguments to utilities that 88are not traversing a file tree. 89.It 90Symbolic links encountered by utilities that are traversing a file tree 91(either specified on the command line or encountered as part of the 92file hierarchy walk). 93.El 94.Ss System calls. 95The first area is symbolic links used as file name arguments for 96system calls. 97.Pp 98Except as noted below, all system calls follow symbolic links. 99For example, if there were a symbolic link 100.Dq Li slink 101which pointed to a file named 102.Dq Li afile , 103the system call 104.Dq Li open("slink" ...) 105would return a file descriptor to the file 106.Dq afile . 107.Pp 108There are four system calls that do not follow links, and which operate 109on the symbolic link itself. 110They are: 111.Xr lstat 2 , 112.Xr readlink 2 , 113.Xr rename 2 , 114and 115.Xr unlink 2 . 116Because 117.Xr remove 3 118is an alias for 119.Xr unlink 2 , 120it also does not follow symbolic links. 121.Pp 122Unlike other filesystem objects, symbolic links do not have an owner, 123group, permissions, access and modification times, etc. 124The only attributes returned from an 125.Xr lstat 2 126that refer to the symbolic link itself are the file type (S_IFLNK), 127size, blocks, and link count (always 1). 128The other attributes are filled in from the directory that contains 129the link. 130For portability reasons, you should be aware that other implementations 131(including historic implementations of 4BSD), implement symbolic links 132such that they have the same attributes as any other file. 133.Pp 134The 135.Bx 4.4 136system differs from historical 4BSD systems in that the system call 137.Xr chown 2 138has been changed to follow symbolic links. 139.Ss Commands not traversing a file tree. 140The second area is symbolic links, specified as command line file 141name arguments, to commands which are not traversing a file tree. 142.Pp 143Except as noted below, commands follow symbolic links named as command 144line arguments. 145For example, if there were a symbolic link 146.Dq Li slink 147which pointed to a file named 148.Dq Li afile , 149the command 150.Dq Li cat slink 151would display the contents of the file 152.Dq Li afile . 153.Pp 154It is important to realize that this rule includes commands which may 155optionally traverse file trees, e.g. the command 156.Dq Li "chown file" 157is included in this rule, while the command 158.Dq Li "chown -R file" 159is not. 160(The latter is described in the third area, below.) 161.Pp 162If it is explicitly intended that the command operate on the symbolic 163link instead of following the symbolic link, e.g., it is desired that 164.Dq Li "file slink" 165display the type of file that 166.Dq Li slink 167is, whether it is a symbolic link or not, the 168.Fl h 169option should be used. 170In the above example, 171.Dq Li "file slink" 172would report the type of the file referenced by 173.Dq Li slink , 174while 175.Dq Li "file -h slink" 176would report that 177.Dq Li slink 178was a symbolic link. 179.Pp 180There are three exceptions to this rule. 181The 182.Xr mv 1 183and 184.Xr rm 1 185commands do not follow symbolic links named as arguments, 186but respectively attempt to rename and delete them. 187(Note, if the symbolic link references a file via a relative path, 188moving it to another directory may very well cause it to stop working, 189since the path may no longer be correct.) 190.Pp 191The 192.Xr ls 1 193command is also an exception to this rule. 194For compatibility with historic systems (when 195.Nm ls 196is not doing a tree walk, i.e. the 197.Fl R 198option is not specified), 199the 200.Nm ls 201command follows symbolic links named as arguments if the 202.Fl L 203option is specified, 204or if the 205.Fl F , 206.Fl d 207or 208.Fl l 209options are not specified. 210(If the 211.Fl L 212option is specified, 213.Nm ls 214always follows symbolic links. 215.Nm Ls 216is the only command where the 217.Fl L 218option affects its behavior even though it is not doing a walk of 219a file tree.) 220.Pp 221The 222.Bx 4.4 223system differs from historical 4BSD systems in that the 224.Nm chown , 225.Nm chgrp 226and 227.Nm file 228commands follow symbolic links specified on the command line. 229.Ss Commands traversing a file tree. 230The following commands either optionally or always traverse file trees: 231.Xr chflags 1 , 232.Xr chgrp 1 , 233.Xr chmod 1 , 234.Xr cp 1 , 235.Xr du 1 , 236.Xr find 1 , 237.Xr ls 1 , 238.Xr pax 1 , 239.Xr rm 1 , 240.Xr tar 1 241and 242.Xr chown 8 . 243.Pp 244It is important to realize that the following rules apply equally to 245symbolic links encountered during the file tree traversal and symbolic 246links listed as command line arguments. 247.Pp 248The first rule applies to symbolic links that reference files that are 249not of type directory. 250Operations that apply to symbolic links are performed on the links 251themselves, but otherwise the links are ignored. 252.Pp 253For example, the command 254.Dq Li "chown -R user slink directory" 255will ignore 256.Dq Li slink , 257because symbolic links in this system do not have owners. 258Any symbolic links encountered during the tree traversal will also be 259ignored. 260The command 261.Dq Li "rm -r slink directory" 262will remove 263.Dq Li slink , 264as well as any symbolic links encountered in the tree traversal of 265.Dq Li directory , 266because symbolic links may be removed. 267In no case will either 268.Nm chown 269or 270.Nm rm 271affect the file which 272.Dq Li slink 273references in any way. 274.Pp 275The second rule applies to symbolic links that reference files of type 276directory. 277Symbolic links which reference files of type directory are never 278.Dq followed 279by default. 280This is often referred to as a 281.Dq physical 282walk, as opposed to a 283.Dq logical 284walk (where symbolic links referencing directories are followed). 285.Pp 286As consistently as possible, you can make commands doing a file tree 287walk follow any symbolic links named on the command line, regardless 288of the type of file they reference, by specifying the 289.Fl H 290(for 291.Dq half\-logical ) 292flag. 293This flag is intended to make the command line name space look 294like the logical name space. 295(Note, for commands that do not always do file tree traversals, the 296.Fl H 297flag will be ignored if the 298.Fl R 299flag is not also specified.) 300.Pp 301For example, the command 302.Dq Li "chown -HR user slink" 303will traverse the file hierarchy rooted in the file pointed to by 304.Dq Li slink . 305Note, the 306.Fl H 307is not the same as the previously discussed 308.Fl h 309flag. 310The 311.Fl H 312flag causes symbolic links specified on the command line to be 313dereferenced both for the purposes of the action to be performed 314and the tree walk, and it is as if the user had specified the 315name of the file to which the symbolic link pointed. 316.Pp 317As consistently as possible, you can make commands doing a file tree 318walk follow any symbolic links named on the command line, as well as 319any symbolic links encountered during the traversal, regardless of 320the type of file they reference, by specifying the 321.Fl L 322(for 323.Dq logical ) 324flag. 325This flag is intended to make the entire name space look like 326the logical name space. 327(Note, for commands that do not always do file tree traversals, the 328.Fl L 329flag will be ignored if the 330.Fl R 331flag is not also specified.) 332.Pp 333For example, the command 334.Dq Li "chown -LR user slink" 335will change the owner of the file referenced by 336.Dq Li slink . 337If 338.Dq Li slink 339references a directory, 340.Nm chown 341will traverse the file hierarchy rooted in the directory that it 342references. 343In addition, if any symbolic links are encountered in any file tree that 344.Nm chown 345traverses, they will be treated in the same fashion as 346.Dq Li slink . 347.Pp 348As consistently as possible, you can specify the default behavior by 349specifying the 350.Fl P 351(for 352.Dq physical ) 353flag. 354This flag is intended to make the entire name space look like the 355physical name space. 356.Pp 357For commands that do not by default do file tree traversals, the 358.Fl H , 359.Fl L 360and 361.Fl P 362flags are ignored if the 363.Fl R 364flag is not also specified. 365In addition, you may specify the 366.Fl H , 367.Fl L 368and 369.Fl P 370options more than once; the last one specified determines the 371command's behavior. 372This is intended to permit you to alias commands to behave one way 373or the other, and then override that behavior on the command line. 374.Pp 375The 376.Xr ls 1 377and 378.Xr rm 1 379commands have exceptions to these rules. 380The 381.Nm rm 382command operates on the symbolic link, and not the file it references, 383and therefore never follows a symbolic link. 384The 385.Nm rm 386command does not support the 387.Fl H , 388.Fl L 389or 390.Fl P 391options. 392.Pp 393To maintain compatibility with historic systems, 394the 395.Nm ls 396command never follows symbolic links unless the 397.Fl L 398flag is specified. 399If the 400.Fl L 401flag is specified, 402.Nm ls 403follows all symbolic links, 404regardless of their type, 405whether specified on the command line or encountered in the tree walk. 406The 407.Nm ls 408command does not support the 409.Fl H 410or 411.Fl P 412options. 413.Sh SEE ALSO 414.Xr chflags 1 , 415.Xr chgrp 1 , 416.Xr chmod 1 , 417.Xr cp 1 , 418.Xr du 1 , 419.Xr find 1 , 420.Xr ln 1 , 421.Xr ls 1 , 422.Xr mv 1 , 423.Xr pax 1 , 424.Xr rm 1 , 425.Xr tar 1 , 426.Xr lstat 2 , 427.Xr readlink 2 , 428.Xr rename 2 , 429.Xr unlink 2 , 430.Xr fts 3 , 431.Xr remove 3 , 432.Xr chown 8 433