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