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: symlink.7,v 1.7 1997/06/19 01:07:05 jdp Exp $ 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. The 136.Xr lchown 2 137system call was added later when the limitations of the new 138.Xr chown 2 139became apparent. 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 H 204or 205.Fl L 206option is specified, 207or if the 208.Fl F , 209.Fl d 210or 211.Fl l 212options are not specified. (The 213.Nm ls 214command is the only command where the 215.Fl H 216and 217.Fl L 218options affect 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 acts a little differently. If you do not specify the 397.Fl F , 398.Fl d or 399.Fl l 400options, 401.Nm ls 402will follow symbolic links specified on the command line. If the 403.Fl L 404flag is specified, 405.Nm ls 406follows all symbolic links, 407regardless of their type, 408whether specified on the command line or encountered in the tree walk. 409.Sh SEE ALSO 410.Xr chflags 1 , 411.Xr chgrp 1 , 412.Xr chmod 1 , 413.Xr cp 1 , 414.Xr du 1 , 415.Xr find 1 , 416.Xr ln 1 , 417.Xr ls 1 , 418.Xr mv 1 , 419.Xr pax 1 , 420.Xr rm 1 , 421.Xr tar 1 , 422.Xr lchown 2 , 423.Xr lstat 2 , 424.Xr readlink 2 , 425.Xr rename 2 , 426.Xr symlink 2 , 427.Xr unlink 2 , 428.Xr fts 3 , 429.Xr remove 3 , 430.Xr chown 8 431