xref: /dragonfly/bin/ln/symlink.7 (revision cfd1aba3)
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. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"	@(#)symlink.7	8.3 (Berkeley) 3/31/94
29.\" $FreeBSD: src/bin/ln/symlink.7,v 1.13.2.7 2003/03/03 19:04:46 trhodes Exp $
30.\"
31.Dd July 31, 2010
32.Dt SYMLINK 7
33.Os
34.Sh NAME
35.Nm symlink
36.Nd symbolic link handling
37.Sh SYMBOLIC LINK HANDLING
38Symbolic links are files that act as pointers to other files.
39To understand their behavior, you must first understand how hard links
40work.
41A hard link to a file is indistinguishable from the original file because
42it is a reference to the object underlying the original file name.
43Changes to a file are independent of the name used to reference the
44file.
45Hard links may not refer to directories and may not reference files
46on different file systems.
47A symbolic link contains the name of the file to which it is linked,
48i.e. it is a pointer to another name, and not to an underlying object.
49For this reason, symbolic links may reference directories and may span
50file systems.
51.Pp
52Because a symbolic link and its referenced object coexist in the file system
53name space, confusion can arise in distinguishing between the link itself
54and the referenced object.
55Historically, commands and system calls have adopted their own link
56following conventions in a somewhat ad-hoc fashion.
57Rules for more a uniform approach, as they are implemented in this system,
58are outlined here.
59It is important that local applications conform to these rules, too,
60so that the user interface can be as consistent as possible.
61.Pp
62Symbolic links are handled either by operating on the link itself,
63or by operating on the object referenced by the link.
64In the latter case,
65an application or system call is said to
66.Dq follow
67the link.
68Symbolic links may reference other symbolic links,
69in which case the links are dereferenced until an object that is
70not a symbolic link is found,
71a symbolic link which references a file which doesn't exist is found,
72or a loop is detected.
73(Loop detection is done by placing an upper limit on the number of
74links that may be followed, and an error results if this limit is
75exceeded.)
76.Pp
77There are three separate areas that need to be discussed.
78They are as follows:
79.Pp
80.Bl -enum -compact -offset indent
81.It
82Symbolic links used as file name arguments for system calls.
83.It
84Symbolic links specified as command line arguments to utilities that
85are not traversing a file tree.
86.It
87Symbolic links encountered by utilities that are traversing a file tree
88(either specified on the command line or encountered as part of the
89file hierarchy walk).
90.El
91.Ss System calls.
92The first area is symbolic links used as file name arguments for
93system calls.
94.Pp
95Except as noted below, all system calls follow symbolic links.
96For example, if there were a symbolic link
97.Dq Li slink
98which pointed to a file named
99.Dq Li afile ,
100the system call
101.Dq Li open("slink" ...\&)
102would return a file descriptor to the file
103.Dq afile .
104.Pp
105There are six system calls that do not follow links, and which operate
106on the symbolic link itself.
107They are:
108.Xr lchown 2 ,
109.Xr lstat 2 ,
110.Xr readlink 2 ,
111.Xr rename 2 ,
112.Xr rmdir 2 ,
113and
114.Xr unlink 2 .
115Because
116.Xr remove 3
117is an alias for
118.Xr unlink 2 ,
119it also does not follow symbolic links.
120When
121.Xr rmdir 2
122is applied to a symbolic link, it fails with the error
123.Er ENOTDIR .
124.Pp
125The owner and group of an existing symbolic link can be changed by
126means of the
127.Xr lchown 2
128system call.
129The other file attributes, such as the modification time and access
130permissions, are not used by the system and cannot be changed.
131.Pp
132The
133.Bx 4.4
134system differs from historical
135.Bx 4
136systems in that the system call
137.Xr chown 2
138has been changed to follow symbolic links.
139The
140.Xr lchown 2
141system call was added later when the limitations of the new
142.Xr chown 2
143became apparent.
144.Ss Commands not traversing a file tree.
145The second area is symbolic links, specified as command line file
146name arguments, to commands which are not traversing a file tree.
147.Pp
148Except as noted below, commands follow symbolic links named as command
149line arguments.
150For example, if there were a symbolic link
151.Dq Li slink
152which pointed to a file named
153.Dq Li afile ,
154the command
155.Dq Li cat slink
156would display the contents of the file
157.Dq Li afile .
158.Pp
159It is important to realize that this rule includes commands which may
160optionally traverse file trees, e.g. the command
161.Dq Li "chown file"
162is included in this rule, while the command
163.Dq Li "chown -R file"
164is not.
165(The latter is described in the third area, below.)
166.Pp
167If it is explicitly intended that the command operate on the symbolic
168link instead of following the symbolic link, e.g., it is desired that
169.Dq Li "chown slink"
170change the ownership of the file that
171.Dq Li slink
172is, whether it is a symbolic link or not, the
173.Fl h
174option should be used.
175In the above example,
176.Dq Li "chown root slink"
177would change the ownership of the file referenced by
178.Dq Li slink ,
179while
180.Dq Li "chown -h root slink"
181would change the ownership of
182.Dq Li slink
183itself.
184.Pp
185There are four exceptions to this rule.
186The
187.Xr mv 1
188and
189.Xr rm 1
190commands do not follow symbolic links named as arguments,
191but respectively attempt to rename and delete them.
192(Note, if the symbolic link references a file via a relative path,
193moving it to another directory may very well cause it to stop working,
194since the path may no longer be correct.)
195.Pp
196The
197.Xr ls 1
198command is also an exception to this rule.
199For compatibility with historic systems (when
200.Nm ls
201is not doing a tree walk, i.e. the
202.Fl R
203option is not specified),
204the
205.Nm ls
206command follows symbolic links named as arguments if the
207.Fl H
208or
209.Fl L
210option is specified,
211or if the
212.Fl F ,
213.Fl d
214or
215.Fl l
216options are not specified.  (The
217.Nm ls
218command is the only command where the
219.Fl H
220and
221.Fl L
222options affect its behavior even though it is not doing a walk of
223a file tree.)
224.Pp
225The
226.Xr file 1
227command is also an exception to this rule.
228The
229.Xr file 1
230command does not follow symbolic links named as argument by default.
231The
232.Xr file 1
233command does follow symbolic links named as argument if
234.Fl L
235option is specified.
236.Pp
237The
238.Bx 4.4
239system differs from historical
240.Bx 4
241systems 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 renameat 2 ,
445.Xr symlink 2 ,
446.Xr unlink 2 ,
447.Xr fts 3 ,
448.Xr remove 3 ,
449.Xr chown 8
450