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