xref: /freebsd/bin/ln/symlink.7 (revision 1718c8b73586ee5f53a53b387929fa9575581d83)
1.\" Copyright (c) 1992, 1993, 1994
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	@(#)symlink.7	8.3 (Berkeley) 3/31/94
33.\" $FreeBSD$
34.\"
35.Dd March 31, 1994
36.Dt SYMLINK 7
37.Os BSD 4
38.Sh NAME
39.Nm symlink
40.Nd symbolic link handling
41.Sh SYMBOLIC LINK HANDLING
42Symbolic links are files that act as pointers to other files.
43To understand their behavior, you must first understand how hard links
44work.
45A hard link to a file is indistinguishable from the original file because
46it is a reference to the object underlying the original file name.
47Changes to a file are independent of the name used to reference the
48file.
49Hard links may not refer to directories and may not reference files
50on different file systems.
51A symbolic link contains the name of the file to which it is linked,
52i.e. it is a pointer to another name, and not to an underlying object.
53For this reason, symbolic links may reference directories and may span
54file systems.
55.Pp
56Because a symbolic link and its referenced object coexist in the filesystem
57name space, confusion can arise in distinguishing between the link itself
58and the referenced object.
59Historically, commands and system calls have adopted their own link
60following conventions in a somewhat ad-hoc fashion.
61Rules for more a uniform approach, as they are implemented in this system,
62are outlined here.
63It is important that local applications conform to these rules, too,
64so that the user interface can be as consistent as possible.
65.Pp
66Symbolic links are handled either by operating on the link itself,
67or by operating on the object referenced by the link.
68In the latter case,
69an application or system call is said to
70.Dq follow
71the link.
72Symbolic links may reference other symbolic links,
73in which case the links are dereferenced until an object that is
74not a symbolic link is found,
75a symbolic link which references a file which doesn't exist is found,
76or a loop is detected.
77(Loop detection is done by placing an upper limit on the number of
78links that may be followed, and an error results if this limit is
79exceeded.)
80.Pp
81There are three separate areas that need to be discussed.
82They are as follows:
83.Pp
84.Bl -enum -compact -offset indent
85.It
86Symbolic links used as file name arguments for system calls.
87.It
88Symbolic links specified as command line arguments to utilities that
89are not traversing a file tree.
90.It
91Symbolic links encountered by utilities that are traversing a file tree
92(either specified on the command line or encountered as part of the
93file hierarchy walk).
94.El
95.Ss System calls.
96The first area is symbolic links used as file name arguments for
97system calls.
98.Pp
99Except as noted below, all system calls follow symbolic links.
100For example, if there were a symbolic link
101.Dq Li slink
102which pointed to a file named
103.Dq Li afile ,
104the system call
105.Dq Li open("slink" ...\&)
106would return a file descriptor to the file
107.Dq afile .
108.Pp
109There are six system calls that do not follow links, and which operate
110on the symbolic link itself.
111They are:
112.Xr lchown 2 ,
113.Xr lstat 2 ,
114.Xr readlink 2 ,
115.Xr rename 2 ,
116.Xr rmdir 2 ,
117and
118.Xr unlink 2 .
119Because
120.Xr remove 3
121is an alias for
122.Xr unlink 2 ,
123it also does not follow symbolic links.
124When
125.Xr rmdir 2
126is applied to a symbolic link, it fails with the error
127.Er ENOTDIR .
128.Pp
129The owner and group of an existing symbolic link can be changed by
130means of the
131.Xr lchown 2
132system call.
133The other file attributes, such as the modification time and access
134permissions, are not used by the system and cannot be changed.
135.Pp
136The
137.Bx 4.4
138system differs from historical 4BSD systems in that the system call
139.Xr chown 2
140has been changed to follow symbolic links.
141The
142.Xr lchown 2
143system call was added later when the limitations of the new
144.Xr chown 2
145became apparent.
146.Ss Commands not traversing a file tree.
147The second area is symbolic links, specified as command line file
148name arguments, to commands which are not traversing a file tree.
149.Pp
150Except as noted below, commands follow symbolic links named as command
151line arguments.
152For example, if there were a symbolic link
153.Dq Li slink
154which pointed to a file named
155.Dq Li afile ,
156the command
157.Dq Li cat slink
158would display the contents of the file
159.Dq Li afile .
160.Pp
161It is important to realize that this rule includes commands which may
162optionally traverse file trees, e.g. the command
163.Dq Li "chown file"
164is included in this rule, while the command
165.Dq Li "chown -R file"
166is not.
167(The latter is described in the third area, below.)
168.Pp
169If it is explicitly intended that the command operate on the symbolic
170link instead of following the symbolic link, e.g., it is desired that
171.Dq Li "chown slink"
172change the ownership of the file that
173.Dq Li slink
174is, whether it is a symbolic link or not, the
175.Fl h
176option should be used.
177In the above example,
178.Dq Li "chown root slink"
179would change the ownership of the file referenced by
180.Dq Li slink ,
181while
182.Dq Li "chown -h root slink"
183would change the ownership of
184.Dq Li slink
185itself.
186.Pp
187There are four exceptions to this rule.
188The
189.Xr mv 1
190and
191.Xr rm 1
192commands do not follow symbolic links named as arguments,
193but respectively attempt to rename and delete them.
194(Note, if the symbolic link references a file via a relative path,
195moving it to another directory may very well cause it to stop working,
196since the path may no longer be correct.)
197.Pp
198The
199.Xr ls 1
200command is also an exception to this rule.
201For compatibility with historic systems (when
202.Nm ls
203is not doing a tree walk, i.e. the
204.Fl R
205option is not specified),
206the
207.Nm ls
208command follows symbolic links named as arguments if the
209.Fl H
210or
211.Fl L
212option is specified,
213or if the
214.Fl F ,
215.Fl d
216or
217.Fl l
218options are not specified.  (The
219.Nm ls
220command is the only command where the
221.Fl H
222and
223.Fl L
224options affect its behavior even though it is not doing a walk of
225a file tree.)
226.Pp
227The
228.Xr file 1
229command is also an exception to this rule.
230The
231.Xr file 1
232command does not follow symbolic links named as argument by default.
233The
234.Xr file 1
235command does follow symbolic links named as argument if
236.Fl L
237option is specified.
238.Pp
239The
240.Bx 4.4
241system differs from historical 4BSD systems in that the
242.Nm chown
243and
244.Nm chgrp
245commands follow symbolic links specified on the command line.
246.Ss Commands traversing a file tree.
247The following commands either optionally or always traverse file trees:
248.Xr chflags 1 ,
249.Xr chgrp 1 ,
250.Xr chmod 1 ,
251.Xr cp 1 ,
252.Xr du 1 ,
253.Xr find 1 ,
254.Xr ls 1 ,
255.Xr pax 1 ,
256.Xr rm 1 ,
257.Xr tar 1
258and
259.Xr chown 8 .
260.Pp
261It is important to realize that the following rules apply equally to
262symbolic links encountered during the file tree traversal and symbolic
263links listed as command line arguments.
264.Pp
265The first rule applies to symbolic links that reference files that are
266not of type directory.
267Operations that apply to symbolic links are performed on the links
268themselves, but otherwise the links are ignored.
269.Pp
270For example, the command
271.Dq Li "chown -R user slink directory"
272will ignore
273.Dq Li slink ,
274because symbolic links in this system do not have owners.
275Any symbolic links encountered during the tree traversal will also be
276ignored.
277The command
278.Dq Li "rm -r slink directory"
279will remove
280.Dq Li slink ,
281as well as any symbolic links encountered in the tree traversal of
282.Dq Li directory ,
283because symbolic links may be removed.
284In no case will either
285.Nm chown
286or
287.Nm rm
288affect the file which
289.Dq Li slink
290references in any way.
291.Pp
292The second rule applies to symbolic links that reference files of type
293directory.
294Symbolic links which reference files of type directory are never
295.Dq followed
296by default.
297This is often referred to as a
298.Dq physical
299walk, as opposed to a
300.Dq logical
301walk (where symbolic links referencing directories are followed).
302.Pp
303As consistently as possible, you can make commands doing a file tree
304walk follow any symbolic links named on the command line, regardless
305of the type of file they reference, by specifying the
306.Fl H
307(for
308.Dq half\-logical )
309flag.
310This flag is intended to make the command line name space look
311like the logical name space.
312(Note, for 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 .
322Note, the
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, you can 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, for commands that do not always do file tree traversals, the
345.Fl L
346flag will be ignored if the
347.Fl R
348flag is not also specified.)
349.Pp
350For example, the command
351.Dq Li "chown -LR user slink"
352will change the owner of the file referenced by
353.Dq Li slink .
354If
355.Dq Li slink
356references a directory,
357.Nm chown
358will traverse the file hierarchy rooted in the directory that it
359references.
360In addition, if any symbolic links are encountered in any file tree that
361.Nm chown
362traverses, they will be treated in the same fashion as
363.Dq Li slink .
364.Pp
365As consistently as possible, you can specify the default behavior by
366specifying the
367.Fl P
368(for
369.Dq physical )
370flag.
371This flag is intended to make the entire name space look like the
372physical name space.
373.Pp
374For commands that do not by default do file tree traversals, the
375.Fl H ,
376.Fl L
377and
378.Fl P
379flags are ignored if the
380.Fl R
381flag is not also specified.
382In addition, you may specify the
383.Fl H ,
384.Fl L
385and
386.Fl P
387options more than once; the last one specified determines the
388command's behavior.
389This is intended to permit you to alias commands to behave one way
390or the other, and then override that behavior on the command line.
391.Pp
392The
393.Xr ls 1
394and
395.Xr rm 1
396commands have exceptions to these rules.
397The
398.Nm rm
399command operates on the symbolic link, and not the file it references,
400and therefore never follows a symbolic link.
401The
402.Nm rm
403command does not support the
404.Fl H ,
405.Fl L
406or
407.Fl P
408options.
409.Pp
410To maintain compatibility with historic systems,
411the
412.Nm ls
413command acts a little differently.  If you do not specify the
414.Fl F ,
415.Fl d
416or
417.Fl l
418options,
419.Nm ls
420will follow symbolic links specified on the command line.  If the
421.Fl L
422flag is specified,
423.Nm ls
424follows all symbolic links,
425regardless of their type,
426whether specified on the command line or encountered in the tree walk.
427.Sh SEE ALSO
428.Xr chflags 1 ,
429.Xr chgrp 1 ,
430.Xr chmod 1 ,
431.Xr cp 1 ,
432.Xr du 1 ,
433.Xr find 1 ,
434.Xr ln 1 ,
435.Xr ls 1 ,
436.Xr mv 1 ,
437.Xr pax 1 ,
438.Xr rm 1 ,
439.Xr tar 1 ,
440.Xr lchown 2 ,
441.Xr lstat 2 ,
442.Xr readlink 2 ,
443.Xr rename 2 ,
444.Xr symlink 2 ,
445.Xr unlink 2 ,
446.Xr fts 3 ,
447.Xr remove 3 ,
448.Xr chown 8
449