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