1.\"- 2.\" Copyright (c) 1992, 1993, 1994 3.\" The Regents of the University of California. All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 3. Neither the name of the University nor the names of its contributors 14.\" may be used to endorse or promote products derived from this software 15.\" without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.\" @(#)symlink.7 8.3 (Berkeley) 3/31/94 30.\" 31.Dd February 16, 2015 32.Dt SYMLINK 7 33.Os 34.Sh NAME 35.Nm symlink 36.Nd symbolic link handling 37.Sh SYMBOLIC LINK HANDLING 38Symbolic links are files that act as pointers to other files. 39To understand their behavior, you must first understand how hard links 40work. 41A hard link to a file is indistinguishable from the original file because 42it is a reference to the object underlying the original file name. 43Changes to a file are independent of the name used to reference the 44file. 45Hard links may not refer to directories and may not reference files 46on different file systems. 47A symbolic link contains the name of the file to which it is linked, 48i.e., it is a pointer to another name, and not to an underlying object. 49For this reason, symbolic links may reference directories and may span 50file systems. 51.Pp 52Because a symbolic link and its referenced object coexist in the file system 53name space, confusion can arise in distinguishing between the link itself 54and the referenced object. 55Historically, commands and system calls have adopted their own link 56following conventions in a somewhat ad-hoc fashion. 57Rules for more a uniform approach, as they are implemented in this system, 58are outlined here. 59It is important that local applications conform to these rules, too, 60so that the user interface can be as consistent as possible. 61.Pp 62Symbolic links are handled either by operating on the link itself, 63or by operating on the object referenced by the link. 64In the latter case, 65an application or system call is said to 66.Dq follow 67the link. 68Symbolic links may reference other symbolic links, 69in which case the links are dereferenced until an object that is 70not a symbolic link is found, 71a symbolic link which references a file which does not exist is found, 72or a loop is detected. 73(Loop detection is done by placing an upper limit on the number of 74links that may be followed, and an error results if this limit is 75exceeded.) 76.Pp 77There are three separate areas that need to be discussed. 78They are as follows: 79.Pp 80.Bl -enum -compact -offset indent 81.It 82Symbolic links used as file name arguments for system calls. 83.It 84Symbolic links specified as command line arguments to utilities that 85are not traversing a file tree. 86.It 87Symbolic links encountered by utilities that are traversing a file tree 88(either specified on the command line or encountered as part of the 89file hierarchy walk). 90.El 91.Ss System calls. 92The first area is symbolic links used as file name arguments for 93system calls. 94.Pp 95Except as noted below, all system calls follow symbolic links. 96For example, if there were a symbolic link 97.Dq Li slink 98which pointed to a file named 99.Dq Li afile , 100the system call 101.Dq Li open("slink" ...\&) 102would return a file descriptor to the file 103.Dq afile . 104.Pp 105There are thirteen system calls that do not follow links, and which operate 106on the symbolic link itself. 107They are: 108.Xr lchflags 2 , 109.Xr lchmod 2 , 110.Xr lchown 2 , 111.Xr lpathconf 2 , 112.Xr lstat 2 , 113.Xr lutimes 2 , 114.Xr readlink 2 , 115.Xr readlinkat 2 , 116.Xr rename 2 , 117.Xr renameat 2 , 118.Xr rmdir 2 , 119.Xr unlink 2 , 120and 121.Xr unlinkat 2 . 122Because 123.Xr remove 3 124is an alias for 125.Xr unlink 2 , 126it also does not follow symbolic links. 127When 128.Xr rmdir 2 129or 130.Xr unlinkat 2 131with the 132.Dv AT_REMOVEDIR 133flag 134is applied to a symbolic link, it fails with the error 135.Er ENOTDIR . 136.Pp 137The 138.Xr linkat 2 139system call does not follow symbolic links 140unless given the 141.Dv AT_SYMLINK_FOLLOW 142flag. 143.Pp 144The following system calls follow symbolic links 145unless given the 146.Dv AT_SYMLINK_NOFOLLOW 147flag: 148.Xr chflagsat 2 , 149.Xr fchmodat 2 , 150.Xr fchownat 2 , 151.Xr fstatat 2 152and 153.Xr utimensat 2 . 154.Pp 155The owner and group of an existing symbolic link can be changed by 156means of the 157.Xr lchown 2 158system call. 159The flags, access permissions, owner/group and modification time of 160an existing symbolic link can be changed by means of the 161.Xr lchflags 2 , 162.Xr lchmod 2 , 163.Xr lchown 2 , 164and 165.Xr lutimes 2 166system calls, respectively. 167Of these, only the flags and ownership are used by the system; 168the access permissions are ignored. 169.Pp 170The 171.Bx 4.4 172system differs from historical 173.Bx 4 174systems in that the system call 175.Xr chown 2 176has been changed to follow symbolic links. 177The 178.Xr lchown 2 179system call was added later when the limitations of the new 180.Xr chown 2 181became apparent. 182.Ss Commands not traversing a file tree. 183The second area is symbolic links, specified as command line file 184name arguments, to commands which are not traversing a file tree. 185.Pp 186Except as noted below, commands follow symbolic links named as command 187line arguments. 188For example, if there were a symbolic link 189.Dq Li slink 190which pointed to a file named 191.Dq Li afile , 192the command 193.Dq Li cat slink 194would display the contents of the file 195.Dq Li afile . 196.Pp 197It is important to realize that this rule includes commands which may 198optionally traverse file trees, e.g.\& the command 199.Dq Li "chown file" 200is included in this rule, while the command 201.Dq Li "chown -R file" 202is not. 203(The latter is described in the third area, below.) 204.Pp 205If it is explicitly intended that the command operate on the symbolic 206link instead of following the symbolic link, e.g., it is desired that 207.Dq Li "chown slink" 208change the ownership of the file that 209.Dq Li slink 210is, whether it is a symbolic link or not, the 211.Fl h 212option should be used. 213In the above example, 214.Dq Li "chown root slink" 215would change the ownership of the file referenced by 216.Dq Li slink , 217while 218.Dq Li "chown -h root slink" 219would change the ownership of 220.Dq Li slink 221itself. 222.Pp 223There are five exceptions to this rule. 224The 225.Xr mv 1 226and 227.Xr rm 1 228commands do not follow symbolic links named as arguments, 229but respectively attempt to rename and delete them. 230(Note, if the symbolic link references a file via a relative path, 231moving it to another directory may very well cause it to stop working, 232since the path may no longer be correct.) 233.Pp 234The 235.Xr ls 1 236command is also an exception to this rule. 237For compatibility with historic systems (when 238.Nm ls 239is not doing a tree walk, i.e., the 240.Fl R 241option is not specified), 242the 243.Nm ls 244command follows symbolic links named as arguments if the 245.Fl H 246or 247.Fl L 248option is specified, 249or if the 250.Fl F , 251.Fl d 252or 253.Fl l 254options are not specified. 255(The 256.Nm ls 257command is the only command where the 258.Fl H 259and 260.Fl L 261options affect its behavior even though it is not doing a walk of 262a file tree.) 263.Pp 264The 265.Xr file 1 266and 267.Xr stat 1 268commands are also exceptions to this rule. 269These 270commands do not follow symbolic links named as argument by default, 271but do follow symbolic links named as argument if the 272.Fl L 273option is specified. 274.Pp 275The 276.Bx 4.4 277system differs from historical 278.Bx 4 279systems in that the 280.Nm chown 281and 282.Nm chgrp 283commands follow symbolic links specified on the command line. 284.Ss Commands traversing a file tree. 285The following commands either optionally or always traverse file trees: 286.Xr chflags 1 , 287.Xr chgrp 1 , 288.Xr chmod 1 , 289.Xr cp 1 , 290.Xr du 1 , 291.Xr find 1 , 292.Xr ls 1 , 293.Xr pax 1 , 294.Xr rm 1 , 295.Xr tar 1 296and 297.Xr chown 8 . 298.Pp 299It is important to realize that the following rules apply equally to 300symbolic links encountered during the file tree traversal and symbolic 301links listed as command line arguments. 302.Pp 303The first rule applies to symbolic links that reference files that are 304not of type directory. 305Operations that apply to symbolic links are performed on the links 306themselves, but otherwise the links are ignored. 307.Pp 308The command 309.Dq Li "rm -r slink directory" 310will remove 311.Dq Li slink , 312as well as any symbolic links encountered in the tree traversal of 313.Dq Li directory , 314because symbolic links may be removed. 315In no case will 316.Nm rm 317affect the file which 318.Dq Li slink 319references in any way. 320.Pp 321The second rule applies to symbolic links that reference files of type 322directory. 323Symbolic links which reference files of type directory are never 324.Dq followed 325by default. 326This is often referred to as a 327.Dq physical 328walk, as opposed to a 329.Dq logical 330walk (where symbolic links referencing directories are followed). 331.Pp 332As consistently as possible, you can make commands doing a file tree 333walk follow any symbolic links named on the command line, regardless 334of the type of file they reference, by specifying the 335.Fl H 336(for 337.Dq half\-logical ) 338flag. 339This flag is intended to make the command line name space look 340like the logical name space. 341(Note, for commands that do not always do file tree traversals, the 342.Fl H 343flag will be ignored if the 344.Fl R 345flag is not also specified.) 346.Pp 347For example, the command 348.Dq Li "chown -HR user slink" 349will traverse the file hierarchy rooted in the file pointed to by 350.Dq Li slink . 351Note, the 352.Fl H 353is not the same as the previously discussed 354.Fl h 355flag. 356The 357.Fl H 358flag causes symbolic links specified on the command line to be 359dereferenced both for the purposes of the action to be performed 360and the tree walk, and it is as if the user had specified the 361name of the file to which the symbolic link pointed. 362.Pp 363As consistently as possible, you can make commands doing a file tree 364walk follow any symbolic links named on the command line, as well as 365any symbolic links encountered during the traversal, regardless of 366the type of file they reference, by specifying the 367.Fl L 368(for 369.Dq logical ) 370flag. 371This flag is intended to make the entire name space look like 372the logical name space. 373(Note, for commands that do not always do file tree traversals, the 374.Fl L 375flag will be ignored if the 376.Fl R 377flag is not also specified.) 378.Pp 379For example, the command 380.Dq Li "chown -LR user slink" 381will change the owner of the file referenced by 382.Dq Li slink . 383If 384.Dq Li slink 385references a directory, 386.Nm chown 387will traverse the file hierarchy rooted in the directory that it 388references. 389In addition, if any symbolic links are encountered in any file tree that 390.Nm chown 391traverses, they will be treated in the same fashion as 392.Dq Li slink . 393.Pp 394As consistently as possible, you can specify the default behavior by 395specifying the 396.Fl P 397(for 398.Dq physical ) 399flag. 400This flag is intended to make the entire name space look like the 401physical name space. 402.Pp 403For commands that do not by default do file tree traversals, the 404.Fl H , 405.Fl L 406and 407.Fl P 408flags are ignored if the 409.Fl R 410flag is not also specified. 411In addition, you may specify the 412.Fl H , 413.Fl L 414and 415.Fl P 416options more than once; the last one specified determines the 417command's behavior. 418This is intended to permit you to alias commands to behave one way 419or the other, and then override that behavior on the command line. 420.Pp 421The 422.Xr ls 1 423and 424.Xr rm 1 425commands have exceptions to these rules. 426The 427.Nm rm 428command operates on the symbolic link, and not the file it references, 429and therefore never follows a symbolic link. 430The 431.Nm rm 432command does not support the 433.Fl H , 434.Fl L 435or 436.Fl P 437options. 438.Pp 439To maintain compatibility with historic systems, 440the 441.Nm ls 442command acts a little differently. 443If you do not specify the 444.Fl F , 445.Fl d 446or 447.Fl l 448options, 449.Nm ls 450will follow symbolic links specified on the command line. 451If the 452.Fl L 453flag is specified, 454.Nm ls 455follows all symbolic links, 456regardless of their type, 457whether specified on the command line or encountered in the tree walk. 458.Sh SEE ALSO 459.Xr chflags 1 , 460.Xr chgrp 1 , 461.Xr chmod 1 , 462.Xr cp 1 , 463.Xr du 1 , 464.Xr find 1 , 465.Xr ln 1 , 466.Xr ls 1 , 467.Xr mv 1 , 468.Xr pax 1 , 469.Xr rm 1 , 470.Xr tar 1 , 471.Xr lchflags 2 , 472.Xr lchmod 2 , 473.Xr lchown 2 , 474.Xr lstat 2 , 475.Xr lutimes 2 , 476.Xr readlink 2 , 477.Xr rename 2 , 478.Xr symlink 2 , 479.Xr unlink 2 , 480.Xr fts 3 , 481.Xr remove 3 , 482.Xr chown 8 483