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