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