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