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