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