.\" Copyright (c) 1992, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\" %sccs.include.redist.roff%
.\" @(#)symlink.7 8.3 (Berkeley) %G%
.Nd symbolic link handling
.Sh SYMBOLIC LINK HANDLING
Symbolic links are files that act as pointers to other files.
To understand their behavior, you must first understand how hard links
A hard link to a file is indistinguishable from the original file because
it is a reference to the object underlying the original file name.
Changes to a file are independent of the name used to reference the
Hard links may not refer to directories and may not reference files
on different file systems.
A symbolic link contains the name of the file to which it is linked,
i.e. it is a pointer to another name, and not to an underlying object.
For this reason, symbolic links may reference directories and may span
Because a symbolic link and its referenced object coexist in the filesystem
name space, confusion can arise in distinguishing between the link itself
and the referenced object.
Historically, commands and system calls have adopted their own link
following conventions in a somewhat ad-hoc fashion.
Rules for more a uniform approach, as they are implemented in this system,
It is important that local applications conform to these rules, too,
so that the user interface can be as consistent as possible.
Symbolic links are handled either by operating on the link itself,
or by operating on the object referenced by the link.
an application or system call is said to
Symbolic links may reference other symbolic links,
in which case the links are dereferenced until an object that is
not a symbolic link is found,
a symbolic link which references a file which doesn't exist is found,
(Loop detection is done by placing an upper limit on the number of
links that may be followed, and an error results if this limit is
There are three separate areas that need to be discussed.
.Bl -enum -compact -offset indent
Symbolic links used as file name arguments for system calls.
Symbolic links specified as command line arguments to utilities that
are not traversing a file tree.
Symbolic links encountered by utilities that are traversing a file tree
(either specified on the command line or encountered as part of the
The first area is symbolic links used as file name arguments for
Except as noted below, all system calls follow symbolic links.
For example, if there were a symbolic link
which pointed to a file named
would return a file descriptor to the file
There are four system calls that do not follow links, and which operate
on the symbolic link itself.
it also does not follow symbolic links.
Unlike other filesystem objects, symbolic links do not have an owner,
group, permissions, access and modification times, etc.
The only attributes returned from an
that refer to the symbolic link itself are the file type (S_IFLNK),
size, blocks, and link count (always 1).
The other attributes are filled in from the directory that contains
For portability reasons, you should be aware that other implementations
(including historic implementations of 4BSD), implement symbolic links
such that they have the same attributes as any other file.
system differs from historical 4BSD systems in that the system call
has been changed to follow symbolic links.
.Ss Commands not traversing a file tree.
The second area is symbolic links, specified as command line file
name arguments, to commands which are not traversing a file tree.
Except as noted below, commands follow symbolic links named as command
For example, if there were a symbolic link
which pointed to a file named
would display the contents of the file
It is important to realize that this rule includes commands which may
optionally traverse file trees, e.g. the command
is included in this rule, while the command
(The latter is described in the third area, below.)
If it is explicitly intended that the command operate on the symbolic
link instead of following the symbolic link, e.g., it is desired that
display the type of file that
is, whether it is a symbolic link or not, the
would report the type of the file referenced by
There are three exceptions to this rule.
commands do not follow symbolic links named as arguments,
but respectively attempt to rename and delete them.
(Note, if the symbolic link references a file via a relative path,
moving it to another directory may very well cause it to stop working,
since the path may no longer be correct.)
command is also an exception to this rule.
For compatibility with historic systems (when
is not doing a tree walk, i.e. the
option is not specified),
command follows symbolic links named as arguments if the
options are not specified.
always follows symbolic links.
is the only command where the
option affects its behavior even though it is not doing a walk of
system differs from historical 4BSD systems in that the
commands follow symbolic links specified on the command line.
.Ss Commands traversing a file tree.
The following commands either optionally or always traverse file trees:
It is important to realize that the following rules apply equally to
symbolic links encountered during the file tree traversal and symbolic
links listed as command line arguments.
The first rule applies to symbolic links that reference files that are
Operations that apply to symbolic links are performed on the links
themselves, but otherwise the links are ignored.
.Dq Li "chown -R user slink directory"
because symbolic links in this system do not have owners.
Any symbolic links encountered during the tree traversal will also be
.Dq Li "rm -r slink directory"
as well as any symbolic links encountered in the tree traversal of
because symbolic links may be removed.
The second rule applies to symbolic links that reference files of type
Symbolic links which reference files of type directory are never
This is often referred to as a
walk (where symbolic links referencing directories are followed).
As consistently as possible, you can make commands doing a file tree
walk follow any symbolic links named on the command line, regardless
of the type of file they reference, by specifying the
This flag is intended to make the command line name space look
like the logical name space.
(Note, for commands that do not always do file tree traversals, the
flag will be ignored if the
flag is not also specified.)
.Dq Li "chown -HR user slink"
will traverse the file hierarchy rooted in the file pointed to by
is not the same as the previously discussed
flag causes symbolic links specified on the command line to be
dereferenced both for the purposes of the action to be performed
and the tree walk, and it is as if the user had specified the
name of the file to which the symbolic link pointed.
As consistently as possible, you can make commands doing a file tree
walk follow any symbolic links named on the command line, as well as
any symbolic links encountered during the traversal, regardless of
the type of file they reference, by specifying the
This flag is intended to make the entire name space look like
(Note, for commands that do not always do file tree traversals, the
flag will be ignored if the
flag is not also specified.)
.Dq Li "chown -LR user slink"
will change the owner of the file referenced by
will traverse the file hierarchy rooted in the directory that it
In addition, if any symbolic links are encountered in any file tree that
traverses, they will be treated in the same fashion as
As consistently as possible, you can specify the default behavior by
This flag is intended to make the entire name space look like the
For commands that do not by default do file tree traversals, the
flag is not also specified.
In addition, you may specify the
options more than once; the last one specified determines the
This is intended to permit you to alias commands to behave one way
or the other, and then override that behavior on the command line.
commands have exceptions to these rules.
command operates on the symbolic link, and not the file it references,
and therefore never follows a symbolic link.
command does not support the
To maintain compatibility with historic systems,
command never follows symbolic links unless the
follows all symbolic links,
regardless of their type,
whether specified on the command line or encountered in the tree walk.
command does not support the