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