xref: /freebsd/bin/ln/symlink.7 (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
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$
31.\"
32.Dd February 16, 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 ,
152.Xr fstatat 2
153and
154.Xr utimensat 2 .
155.Pp
156The owner and group of an existing symbolic link can be changed by
157means of the
158.Xr lchown 2
159system call.
160The flags, access permissions, owner/group and modification time of
161an existing symbolic link can be changed by means of the
162.Xr lchflags 2 ,
163.Xr lchmod 2 ,
164.Xr lchown 2 ,
165and
166.Xr lutimes 2
167system calls, respectively.
168Of these, only the flags and ownership are used by the system;
169the access permissions are ignored.
170.Pp
171The
172.Bx 4.4
173system differs from historical
174.Bx 4
175systems in that the system call
176.Xr chown 2
177has been changed to follow symbolic links.
178The
179.Xr lchown 2
180system call was added later when the limitations of the new
181.Xr chown 2
182became apparent.
183.Ss Commands not traversing a file tree.
184The second area is symbolic links, specified as command line file
185name arguments, to commands which are not traversing a file tree.
186.Pp
187Except as noted below, commands follow symbolic links named as command
188line arguments.
189For example, if there were a symbolic link
190.Dq Li slink
191which pointed to a file named
192.Dq Li afile ,
193the command
194.Dq Li cat slink
195would display the contents of the file
196.Dq Li afile .
197.Pp
198It is important to realize that this rule includes commands which may
199optionally traverse file trees, e.g.\& the command
200.Dq Li "chown file"
201is included in this rule, while the command
202.Dq Li "chown -R file"
203is not.
204(The latter is described in the third area, below.)
205.Pp
206If it is explicitly intended that the command operate on the symbolic
207link instead of following the symbolic link, e.g., it is desired that
208.Dq Li "chown slink"
209change the ownership of the file that
210.Dq Li slink
211is, whether it is a symbolic link or not, the
212.Fl h
213option should be used.
214In the above example,
215.Dq Li "chown root slink"
216would change the ownership of the file referenced by
217.Dq Li slink ,
218while
219.Dq Li "chown -h root slink"
220would change the ownership of
221.Dq Li slink
222itself.
223.Pp
224There are five exceptions to this rule.
225The
226.Xr mv 1
227and
228.Xr rm 1
229commands do not follow symbolic links named as arguments,
230but respectively attempt to rename and delete them.
231(Note, if the symbolic link references a file via a relative path,
232moving it to another directory may very well cause it to stop working,
233since the path may no longer be correct.)
234.Pp
235The
236.Xr ls 1
237command is also an exception to this rule.
238For compatibility with historic systems (when
239.Nm ls
240is not doing a tree walk, i.e., the
241.Fl R
242option is not specified),
243the
244.Nm ls
245command follows symbolic links named as arguments if the
246.Fl H
247or
248.Fl L
249option is specified,
250or if the
251.Fl F ,
252.Fl d
253or
254.Fl l
255options are not specified.
256(The
257.Nm ls
258command is the only command where the
259.Fl H
260and
261.Fl L
262options affect its behavior even though it is not doing a walk of
263a file tree.)
264.Pp
265The
266.Xr file 1
267and
268.Xr stat 1
269commands are also exceptions to this rule.
270These
271commands do not follow symbolic links named as argument by default,
272but do follow symbolic links named as argument if the
273.Fl L
274option is specified.
275.Pp
276The
277.Bx 4.4
278system differs from historical
279.Bx 4
280systems in that the
281.Nm chown
282and
283.Nm chgrp
284commands follow symbolic links specified on the command line.
285.Ss Commands traversing a file tree.
286The following commands either optionally or always traverse file trees:
287.Xr chflags 1 ,
288.Xr chgrp 1 ,
289.Xr chmod 1 ,
290.Xr cp 1 ,
291.Xr du 1 ,
292.Xr find 1 ,
293.Xr ls 1 ,
294.Xr pax 1 ,
295.Xr rm 1 ,
296.Xr tar 1
297and
298.Xr chown 8 .
299.Pp
300It is important to realize that the following rules apply equally to
301symbolic links encountered during the file tree traversal and symbolic
302links listed as command line arguments.
303.Pp
304The first rule applies to symbolic links that reference files that are
305not of type directory.
306Operations that apply to symbolic links are performed on the links
307themselves, but otherwise the links are ignored.
308.Pp
309The command
310.Dq Li "rm -r slink directory"
311will remove
312.Dq Li slink ,
313as well as any symbolic links encountered in the tree traversal of
314.Dq Li directory ,
315because symbolic links may be removed.
316In no case will
317.Nm rm
318affect the file which
319.Dq Li slink
320references in any way.
321.Pp
322The second rule applies to symbolic links that reference files of type
323directory.
324Symbolic links which reference files of type directory are never
325.Dq followed
326by default.
327This is often referred to as a
328.Dq physical
329walk, as opposed to a
330.Dq logical
331walk (where symbolic links referencing directories are followed).
332.Pp
333As consistently as possible, you can make commands doing a file tree
334walk follow any symbolic links named on the command line, regardless
335of the type of file they reference, by specifying the
336.Fl H
337(for
338.Dq half\-logical )
339flag.
340This flag is intended to make the command line name space look
341like the logical name space.
342(Note, for commands that do not always do file tree traversals, the
343.Fl H
344flag will be ignored if the
345.Fl R
346flag is not also specified.)
347.Pp
348For example, the command
349.Dq Li "chown -HR user slink"
350will traverse the file hierarchy rooted in the file pointed to by
351.Dq Li slink .
352Note, the
353.Fl H
354is not the same as the previously discussed
355.Fl h
356flag.
357The
358.Fl H
359flag causes symbolic links specified on the command line to be
360dereferenced both for the purposes of the action to be performed
361and the tree walk, and it is as if the user had specified the
362name of the file to which the symbolic link pointed.
363.Pp
364As consistently as possible, you can make commands doing a file tree
365walk follow any symbolic links named on the command line, as well as
366any symbolic links encountered during the traversal, regardless of
367the type of file they reference, by specifying the
368.Fl L
369(for
370.Dq logical )
371flag.
372This flag is intended to make the entire name space look like
373the logical name space.
374(Note, for commands that do not always do file tree traversals, the
375.Fl L
376flag will be ignored if the
377.Fl R
378flag is not also specified.)
379.Pp
380For example, the command
381.Dq Li "chown -LR user slink"
382will change the owner of the file referenced by
383.Dq Li slink .
384If
385.Dq Li slink
386references a directory,
387.Nm chown
388will traverse the file hierarchy rooted in the directory that it
389references.
390In addition, if any symbolic links are encountered in any file tree that
391.Nm chown
392traverses, they will be treated in the same fashion as
393.Dq Li slink .
394.Pp
395As consistently as possible, you can specify the default behavior by
396specifying the
397.Fl P
398(for
399.Dq physical )
400flag.
401This flag is intended to make the entire name space look like the
402physical name space.
403.Pp
404For commands that do not by default do file tree traversals, the
405.Fl H ,
406.Fl L
407and
408.Fl P
409flags are ignored if the
410.Fl R
411flag is not also specified.
412In addition, you may specify the
413.Fl H ,
414.Fl L
415and
416.Fl P
417options more than once; the last one specified determines the
418command's behavior.
419This is intended to permit you to alias commands to behave one way
420or the other, and then override that behavior on the command line.
421.Pp
422The
423.Xr ls 1
424and
425.Xr rm 1
426commands have exceptions to these rules.
427The
428.Nm rm
429command operates on the symbolic link, and not the file it references,
430and therefore never follows a symbolic link.
431The
432.Nm rm
433command does not support the
434.Fl H ,
435.Fl L
436or
437.Fl P
438options.
439.Pp
440To maintain compatibility with historic systems,
441the
442.Nm ls
443command acts a little differently.
444If you do not specify the
445.Fl F ,
446.Fl d
447or
448.Fl l
449options,
450.Nm ls
451will follow symbolic links specified on the command line.
452If the
453.Fl L
454flag is specified,
455.Nm ls
456follows all symbolic links,
457regardless of their type,
458whether specified on the command line or encountered in the tree walk.
459.Sh SEE ALSO
460.Xr chflags 1 ,
461.Xr chgrp 1 ,
462.Xr chmod 1 ,
463.Xr cp 1 ,
464.Xr du 1 ,
465.Xr find 1 ,
466.Xr ln 1 ,
467.Xr ls 1 ,
468.Xr mv 1 ,
469.Xr pax 1 ,
470.Xr rm 1 ,
471.Xr tar 1 ,
472.Xr lchflags 2 ,
473.Xr lchmod 2 ,
474.Xr lchown 2 ,
475.Xr lstat 2 ,
476.Xr lutimes 2 ,
477.Xr readlink 2 ,
478.Xr rename 2 ,
479.Xr symlink 2 ,
480.Xr unlink 2 ,
481.Xr fts 3 ,
482.Xr remove 3 ,
483.Xr chown 8
484