]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/symlink.7
sched.7: Note error that occurs when writing invalid value to /proc/PID/autogroup
[thirdparty/man-pages.git] / man7 / symlink.7
CommitLineData
eae3689b
MK
1.\" Copyright (c) 1992, 1993, 1994
2.\" The Regents of the University of California. All rights reserved.
828b6e3d 3.\" and Copyright (C) 2008, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
eae3689b 4.\"
491b7776 5.\" %%%LICENSE_START(BSD_3_CLAUSE_UCB)
eae3689b
MK
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 4. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
8c9302dc 29.\" %%%LICENSE_END
eae3689b
MK
30.\"
31.\" @(#)symlink.7 8.3 (Berkeley) 3/31/94
32.\" $FreeBSD: src/bin/ln/symlink.7,v 1.30 2005/02/13 22:25:09 ru Exp $
33.\"
34.\" 2008-06-11, mtk, Taken from FreeBSD 6.2 and heavily edited for
35.\" specific Linux details, improved readability, and man-pages style.
36.\"
b8efb414 37.TH SYMLINK 7 2016-10-08 "Linux" "Linux Programmer's Manual"
eae3689b
MK
38.SH NAME
39symlink \- symbolic link handling
7aa6eb89 40.SH DESCRIPTION
eae3689b
MK
41Symbolic links are files that act as pointers to other files.
42To understand their behavior, you must first understand how hard links
43work.
7eb11a9f 44
eae3689b
MK
45A hard link to a file is indistinguishable from the original file because
46it is a reference to the object underlying the original filename.
7eb11a9f
MK
47(To be precise: each of the hard links to a file is a reference to
48the same
e7e006f2
MK
49.IR "inode number" ,
50where an inode number is an index into the inode table,
9ee4a2b6 51which contains metadata about all files on a filesystem.
99d8a684 52See
7eb11a9f 53.BR stat (2).)
eae3689b
MK
54Changes to a file are independent of the name used to reference the file.
55Hard links may not refer to directories
9ee4a2b6 56(to prevent the possibility of loops within the filesystem tree,
eae3689b 57which would confuse many programs)
9ee4a2b6 58and may not refer to files on different filesystems
e7e006f2 59(because inode numbers are not unique across filesystems).
7eb11a9f 60
eae3689b 61A symbolic link is a special type of file whose contents are a string
39c56b84 62that is the pathname of another file, the file to which the link refers.
ed171400
MK
63(The contents of a symbolic link can be read using
64.BR readlink (2).)
eae3689b
MK
65In other words, a symbolic link is a pointer to another name,
66and not to an underlying object.
67For this reason, symbolic links may refer to directories and may cross
9ee4a2b6 68filesystem boundaries.
eae3689b
MK
69
70There is no requirement that the pathname referred to by a symbolic link
71should exist.
72A symbolic link that refers to a pathname that does not exist is said
7eb11a9f
MK
73to be a
74.IR "dangling link" .
eae3689b 75
9ee4a2b6 76Because a symbolic link and its referenced object coexist in the filesystem
eae3689b
MK
77name space, confusion can arise in distinguishing between the link itself
78and the referenced object.
79On historical systems,
7eb11a9f
MK
80commands and system calls adopted their own link-following
81conventions in a somewhat ad-hoc fashion.
eae3689b
MK
82Rules for a more uniform approach,
83as they are implemented on Linux and other systems,
84are outlined here.
85It is important that site-local applications also conform to these rules,
86so that the user interface can be as consistent as possible.
87.SS Symbolic link ownership, permissions, and timestamps
88The owner and group of an existing symbolic link can be changed
89using
90.BR lchown (2).
91The only time that the ownership of a symbolic link matters is
92when the link is being removed or renamed in a directory that
93has the sticky bit set (see
94.BR stat (2)).
95
96The last access and last modification timestamps
97of a symbolic link can be changed using
98.BR utimensat (2)
99or
100.BR lutimes (3).
101
102On Linux, the permissions of a symbolic link are not used
103in any operations; the permissions are always
1040777 (read, write, and execute for all user categories),
105.\" Linux does not currently implement an lchmod(2).
106and can't be changed.
92a2239f
MK
107(Note that there are some "magic" symbolic links in the
108.I /proc
109directory tree\(emfor example, the
750653a8 110.IR /proc/[pid]/fd/*
92a2239f 111files\(emthat have different permissions.)
eae3689b
MK
112.\"
113.\" The
114.\" 4.4BSD
115.\" system differs from historical
116.\" 4BSD
117.\" systems in that the system call
118.\" .BR chown (2)
119.\" has been changed to follow symbolic links.
120.\" The
121.\" .BR lchown (2)
122.\" system call was added later when the limitations of the new
123.\" .BR chown (2)
124.\" became apparent.
d33b6ed2
MK
125.SS Obtaining a file descriptor that refers to a symbolic link
126Using the combination of the
127.B O_PATH
128and
129.BR O_NOFOLLOW
130flags to
131.BR open (2)
132yields a file descriptor that can be passed as the
133.IR dirfd
134argument in system calls such as
135.BR fstatat (2),
136.BR fchownat (2),
137.BR fchmodat (2),
138.BR linkat (2),
139and
140.BR readlinkat (2),
141in order to operate on the symbolic link itself
142(rather than the file to which it refers).
143
144By default
145(i.e., if the
146.BR AT_SYMLINK_FOLLOW
147flag is not specified), if
148.BR name_to_handle_at (2)
149is applied to a symbolic link, it yields a handle for the symbolic link
150(rather than the file to which it refers).
151One can then obtain a file descriptor for the symbolic link
152(rather than the file to which it refers)
153by specifying the
154.B O_PATH
58be64a4 155flag in a subsequent call to
d33b6ed2
MK
156.BR open_by_handle_at (2).
157Again, that file descriptor can be used in the
158aforementioned system calls to operate on the symbolic link itself.
eae3689b
MK
159.SS Handling of symbolic links by system calls and commands
160Symbolic links are handled either by operating on the link itself,
161or by operating on the object referred to by the link.
162In the latter case,
7eb11a9f
MK
163an application or system call is said to
164.I follow
165the link.
eae3689b
MK
166Symbolic links may refer to other symbolic links,
167in which case the links are dereferenced until an object that is
168not a symbolic link is found,
169a symbolic link that refers to a file which does not exist is found,
170or a loop is detected.
171(Loop detection is done by placing an upper limit on the number of
172links that may be followed, and an error results if this limit is
173exceeded.)
174
175There are three separate areas that need to be discussed.
176They are as follows:
177.IP 1. 3
178Symbolic links used as filename arguments for system calls.
179.IP 2.
180Symbolic links specified as command-line arguments to utilities that
181are not traversing a file tree.
182.IP 3.
183Symbolic links encountered by utilities that are traversing a file tree
184(either specified on the command line or encountered as part of the
185file hierarchy walk).
186.SS System calls
187The first area is symbolic links used as filename arguments for
188system calls.
189
190Except as noted below, all system calls follow symbolic links.
191For example, if there were a symbolic link
7eb11a9f 192.I slink
eae3689b 193which pointed to a file named
7eb11a9f 194.IR afile ,
eae3689b 195the system call
e6e731d1 196.I "open(""slink"" ...\&)"
eae3689b 197would return a file descriptor referring to the file
7eb11a9f 198.IR afile .
eae3689b
MK
199
200Various system calls do not follow links, and operate
201on the symbolic link itself.
202They are:
203.BR lchown (2),
204.BR lgetxattr (2),
205.BR llistxattr (2),
206.BR lremovexattr (2),
207.BR lsetxattr (2),
208.BR lstat (2),
eae3689b
MK
209.BR readlink (2),
210.BR rename (2),
211.BR rmdir (2),
212and
213.BR unlink (2).
19d9c23c 214
eae3689b
MK
215Certain other system calls optionally follow symbolic links.
216They are:
217.BR faccessat (2),
218.\" Maybe one day: .BR fchownat (2)
219.BR fchownat (2),
220.BR fstatat (2),
221.BR linkat (2),
df63d3e8 222.BR name_to_handle_at (2),
eae3689b
MK
223.BR open (2),
224.BR openat (2),
df63d3e8 225.BR open_by_handle_at (2),
eae3689b
MK
226and
227.BR utimensat (2);
228see their manual pages for details.
229Because
230.BR remove (3)
231is an alias for
232.BR unlink (2),
233that library function also does not follow symbolic links.
234When
235.BR rmdir (2)
236is applied to a symbolic link, it fails with the error
237.BR ENOTDIR .
19d9c23c 238
eae3689b
MK
239.BR link (2)
240warrants special discussion.
241POSIX.1-2001 specifies that
242.BR link (2)
243should dereference
244.I oldpath
245if it is a symbolic link.
246However, Linux does not do this.
a902a39f 247(By default, Solaris is the same,
eae3689b
MK
248but the POSIX.1-2001 specified behavior can be obtained with
249suitable compiler options.)
cad7ef53 250POSIX.1-2008 changed the specification to allow
eae3689b
MK
251either behavior in an implementation.
252.SS Commands not traversing a file tree
253The second area is symbolic links, specified as command-line
254filename arguments, to commands which are not traversing a file tree.
255
256Except as noted below, commands follow symbolic links named as
257command-line arguments.
258For example, if there were a symbolic link
7eb11a9f 259.I slink
eae3689b 260which pointed to a file named
7eb11a9f 261.IR afile ,
eae3689b
MK
262the command
263.I "cat slink"
264would display the contents of the file
7eb11a9f 265.IR afile .
eae3689b
MK
266
267It is important to realize that this rule includes commands which may
10850212 268optionally traverse file trees; for example, the command
eae3689b
MK
269.I "chown file"
270is included in this rule, while the command
2f6b8245 271.IR "chown\ \-R file" ,
eae3689b
MK
272which performs a tree traversal, is not.
273(The latter is described in the third area, below.)
274
275If it is explicitly intended that the command operate on the symbolic
10850212 276link instead of following the symbolic link\(emfor example, it is desired that
eae3689b
MK
277.I "chown slink"
278change the ownership of the file that
7eb11a9f 279.I slink
10850212 280is, whether it is a symbolic link or not\(emthe
90850708 281.I \-h
eae3689b
MK
282option should be used.
283In the above example,
284.I "chown root slink"
285would change the ownership of the file referred to by
7eb11a9f 286.IR slink ,
eae3689b 287while
2f6b8245 288.I "chown\ \-h root slink"
eae3689b 289would change the ownership of
7eb11a9f 290.I slink
eae3689b
MK
291itself.
292
293There are some exceptions to this rule:
294.IP * 2
295The
296.BR mv (1)
297and
298.BR rm (1)
299commands do not follow symbolic links named as arguments,
300but respectively attempt to rename and delete them.
301(Note, if the symbolic link references a file via a relative path,
302moving it to another directory may very well cause it to stop working,
303since the path may no longer be correct.)
304.IP *
305The
306.BR ls (1)
307command is also an exception to this rule.
308For compatibility with historic systems (when
309.BR ls (1)
88879aeb 310is not doing a tree walk\(emthat is,
90850708 311.I \-R
eae3689b
MK
312option is not specified),
313the
314.BR ls (1)
315command follows symbolic links named as arguments if the
90850708 316.I \-H
eae3689b 317or
90850708 318.I \-L
eae3689b
MK
319option is specified,
320or if the
90850708
MK
321.IR \-F ,
322.IR \-d ,
eae3689b 323or
90850708 324.I \-l
eae3689b
MK
325options are not specified.
326(The
327.BR ls (1)
328command is the only command where the
90850708 329.I \-H
eae3689b 330and
90850708 331.I \-L
eae3689b
MK
332options affect its behavior even though it is not doing a walk of
333a file tree.)
334.IP *
335The
336.BR file (1)
337command is also an exception to this rule.
338The
339.BR file (1)
340command does not follow symbolic links named as argument by default.
341The
342.BR file (1)
343command does follow symbolic links named as argument if the
90850708 344.I \-L
eae3689b
MK
345option is specified.
346.\"
347.\"The 4.4BSD system differs from historical 4BSD systems in that the
348.\".BR chown (1)
349.\"and
350.\".BR chgrp (1)
351.\"commands follow symbolic links specified on the command line.
352.SS Commands traversing a file tree
353The following commands either optionally or always traverse file trees:
354.BR chgrp (1),
355.BR chmod (1),
356.BR chown (1),
357.BR cp (1),
358.BR du (1),
359.BR find (1),
360.BR ls (1),
361.BR pax (1),
362.BR rm (1),
363and
364.BR tar (1).
365
366It is important to realize that the following rules apply equally to
367symbolic links encountered during the file tree traversal and symbolic
368links listed as command-line arguments.
369
370The \fIfirst rule\fP applies to symbolic links that reference files other
371than directories.
372Operations that apply to symbolic links are performed on the links
373themselves, but otherwise the links are ignored.
374
375The command
2f6b8245 376.I "rm\ \-r slink directory"
eae3689b 377will remove
7eb11a9f 378.IR slink ,
eae3689b 379as well as any symbolic links encountered in the tree traversal of
7eb11a9f 380.IR directory ,
eae3689b
MK
381because symbolic links may be removed.
382In no case will
383.BR rm (1)
384affect the file referred to by
7eb11a9f 385.IR slink .
eae3689b
MK
386
387The \fIsecond rule\fP applies to symbolic links that refer to directories.
388Symbolic links that refer to directories are never followed by default.
389This is often referred to as a "physical" walk, as opposed to a "logical"
d5a31e0c 390walk (where symbolic links that refer to directories are followed).
eae3689b
MK
391
392Certain conventions are (should be) followed as consistently as
393possible by commands that perform file tree walks:
394.IP * 2
a1d5601b 395A command can be made to follow
eae3689b
MK
396any symbolic links named on the command line,
397regardless of the type of file they reference, by specifying the
90850708
MK
398.I \-H
399(for "half-logical") flag.
eae3689b
MK
400This flag is intended to make the command-line name space look
401like the logical name space.
402(Note, for commands that do not always do file tree traversals, the
90850708 403.I \-H
eae3689b 404flag will be ignored if the
90850708 405.I \-R
eae3689b
MK
406flag is not also specified.)
407
408For example, the command
2f6b8245 409.I "chown\ \-HR user slink"
eae3689b 410will traverse the file hierarchy rooted in the file pointed to by
7eb11a9f 411.IR slink .
eae3689b 412Note, the
90850708 413.I \-H
eae3689b 414is not the same as the previously discussed
90850708 415.I \-h
eae3689b
MK
416flag.
417The
90850708 418.I \-H
eae3689b
MK
419flag causes symbolic links specified on the command line to be
420dereferenced for the purposes of both the action to be performed
421and the tree walk, and it is as if the user had specified the
422name of the file to which the symbolic link pointed.
423.IP *
424A command can be made to
425follow any symbolic links named on the command line,
426as well as any symbolic links encountered during the traversal,
427regardless of the type of file they reference, by specifying the
90850708 428.I \-L
eae3689b
MK
429(for "logical") flag.
430This flag is intended to make the entire name space look like
431the logical name space.
432(Note, for commands that do not always do file tree traversals, the
90850708 433.I \-L
eae3689b 434flag will be ignored if the
90850708 435.I \-R
eae3689b
MK
436flag is not also specified.)
437
438For example, the command
2f6b8245 439.I "chown\ \-LR user slink"
7eb11a9f
MK
440will change the owner of the file referred to by
441.IR slink .
442If
443.I slink
444refers to a directory,
445.B chown
446will traverse the file hierarchy rooted in the directory that it
eae3689b
MK
447references.
448In addition, if any symbolic links are encountered in any file tree that
7eb11a9f
MK
449.B chown
450traverses, they will be treated in the same fashion as
451.IR slink .
eae3689b
MK
452.IP *
453A command can be made to
454provide the default behavior by specifying the
90850708 455.I \-P
7eb11a9f 456(for "physical") flag.
eae3689b
MK
457This flag is intended to make the entire name space look like the
458physical name space.
459.PP
460For commands that do not by default do file tree traversals, the
90850708
MK
461.IR \-H ,
462.IR \-L ,
eae3689b 463and
90850708 464.I \-P
eae3689b 465flags are ignored if the
90850708 466.I \-R
eae3689b
MK
467flag is not also specified.
468In addition, you may specify the
90850708
MK
469.IR \-H ,
470.IR \-L ,
eae3689b 471and
90850708 472.I \-P
7eb11a9f
MK
473options more than once;
474the last one specified determines the command's behavior.
eae3689b
MK
475This is intended to permit you to alias commands to behave one way
476or the other, and then override that behavior on the command line.
477
478The
479.BR ls (1)
480and
481.BR rm (1)
482commands have exceptions to these rules:
483.IP * 2
484The
485.BR rm (1)
486command operates on the symbolic link, and not the file it references,
487and therefore never follows a symbolic link.
488The
489.BR rm (1)
490command does not support the
90850708
MK
491.IR \-H ,
492.IR \-L ,
eae3689b 493or
90850708 494.I \-P
eae3689b
MK
495options.
496.IP *
497To maintain compatibility with historic systems,
498the
499.BR ls (1)
500command acts a little differently.
501If you do not specify the
90850708
MK
502.IR \-F ,
503.IR \-d
eae3689b 504or
90850708 505.I \-l
eae3689b
MK
506options,
507.BR ls (1)
508will follow symbolic links specified on the command line.
509If the
90850708 510.I \-L
eae3689b
MK
511flag is specified,
512.BR ls (1)
513follows all symbolic links,
514regardless of their type,
515whether specified on the command line or encountered in the tree walk.
516.SH SEE ALSO
eae3689b
MK
517.BR chgrp (1),
518.BR chmod (1),
519.BR find (1),
520.BR ln (1),
521.BR ls (1),
522.BR mv (1),
523.BR rm (1),
524.BR lchown (2),
525.BR link (2),
526.BR lstat (2),
527.BR readlink (2),
528.BR rename (2),
529.BR symlink (2),
530.BR unlink (2),
531.BR utimensat (2),
7eb11a9f
MK
532.BR lutimes (3),
533.BR path_resolution (7)