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