SYMLINK(7)

NAME
symlink - symbolic link handling

DESCRIPTION
Symbolic links are files that act as pointers to other files. To understand their behavior, you must first understand how hard links work.

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 file. 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 file systems.

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, are outlined here. 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. In the latter case, an application or system call is said to "follow" the link.

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, or a loop is detected. 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 exceeded.

There are three separate areas that need to be discussed. They are as follows:

1.  Symbolic links used as file name arguments for system calls.
2.  Symbolic links specified as command line arguments to utilities that are not traversing a file tree.
3.  Symbolic links encountered by utilities that are traversing a file tree (either specified on the command line or encountered as part of the file hierarchy walk).

  System calls
The first area is symbolic links used as file name arguments for system calls.

Except as noted below, all system calls follow symbolic links. For example, if there were a symbolic link "slink" which pointed to a file named "afile", the system call "open("slink" ...)" would return a file descriptor to the file "afile".

There are eight system calls that do not follow links, and which operate on the symbolic link itself. They are: lchflags(2), lchmod(2), lchown(2), lstat(2), lutimes(2), readlink(2), rename(2), and unlink(2). Because remove(3) is an alias for unlink(2), it also does not follow symbolic links.

The 4.4BSD system differs from historical 4BSD systems in that the system call chown(2) has been changed to follow symbolic links.

If the filesystem is mounted with the symperm mount(8) option, the symbolic link file permission bits have the following effects:

The readlink(2) system call requires read permissions on the symbolic link.

System calls that follow symbolic links will fail without execute/search permissions on all the symbolic links followed.

The write, sticky, set-user-ID-on-execution and set-group-ID-on-execution symbolic link mode bits have no effect on any system calls ( including execve(2)).

  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 line arguments. For example, if there were a symbolic link "slink" which pointed to a file named "afile", the command "cat slink" would display the contents of the file "afile".

It is important to realize that this rule includes commands which may optionally traverse file trees, e.g. the command "chown file" is included in this rule, while the command "chown -R file" is not (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 "file slink" display the type of file that "slink" is, whether it is a symbolic link or not, the -h option should be used. In the above example, "file slink" would report the type of the file referenced by "slink", while "file -h slink" would report that "slink" was a symbolic link.

There are three exceptions to this rule. The mv(1) and rm(1) 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).

The ls(1) command is also an exception to this rule. For compatibility with historic systems (when ls is not doing a tree walk, i.e. the -R option is not specified), the ls command follows symbolic links named as arguments if the -L option is specified, or if the -F, -d or -l options are not specified. (If the -L option is specified, ls always follows symbolic links. ls is the only command where the -L option affects its behavior even though it is not doing a walk of a file tree).

The 4.4BSD system differs from historical 4BSD systems in that the chown, chgrp and file commands follow symbolic links specified on the command line.

  Commands traversing a file tree
The following commands either optionally or always traverse file trees: chflags(1), chgrp(1), chmod(1), cp(1), du(1), find(1), ls(1), pax(1), rm(1), tar(1) and chown(8).

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 not of type directory. Operations that apply to symbolic links are performed on the links themselves, but otherwise the links are ignored.

For example, the command "chown -R user slink directory" will ignore "slink", because the -h flag must be used to change owners of symbolic links. Any symbolic links encountered during the tree traversal will also be ignored. The command "rm -r slink directory" will remove "slink", as well as any symbolic links encountered in the tree traversal of "directory", because symbolic links may be removed. In no case will either chown or rm affect the file which "slink" references in any way.

The second rule applies to symbolic links that reference files of type directory. Symbolic links which reference files of type directory are never "followed" by default. This is often referred to as a "physical" walk, as opposed to a "logical" 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 -H (for "half-logical") flag. 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 -H flag will be ignored if the -R flag is not also specified).

For example, the command "chown -HR user slink" will traverse the file hierarchy rooted in the file pointed to by "slink". Note, the -H is not the same as the previously discussed -h flag. The -H 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 -L (for "logical") flag. This flag is intended to make the entire name space look like the logical name space. (Note, for commands that do not always do file tree traversals, the -L flag will be ignored if the -R flag is not also specified).

For example, the command "chown -LR user slink" will change the owner of the file referenced by "slink". If "slink" references a directory, chown will traverse the file hierarchy rooted in the directory that it references. In addition, if any symbolic links are encountered in any file tree that chown traverses, they will be treated in the same fashion as "slink".

As consistently as possible, you can specify the default behavior by specifying the -P (for "physical") flag. This flag is intended to make the entire name space look like the physical name space.

For commands that do not by default do file tree traversals, the -H, -L and -P flags are ignored if the -R flag is not also specified. In addition, you may specify the -H, -L and -P options more than once; the last one specified determines the command's behavior. 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.

The ls(1) and rm(1) commands have exceptions to these rules. The rm command operates on the symbolic link, and not the file it references, and therefore never follows a symbolic link. The rm command does not support the -H, -L or -P options.

To maintain compatibility with historic systems, the ls command never follows symbolic links unless the -L flag is specified. If the -L flag is specified, ls follows all symbolic links, regardless of their type, whether specified on the command line or encountered in the tree walk. The ls command does not support the -H or -P options.

MAGIC SYMLINKS
Magic symlinks can be enabled by setting “vfs.generic.magiclinks” with sysctl(8). When magic symlinks are enabled “magic” patterns in symlinks are expanded. Those patterns begin with “@” (an at-sign), and end at the end of the pathname component ( i.e. at the next “/”, or at the end of the symbolic link if there are no more slashes).

To illustrate the pattern matching rules, assume that “@foo” is a valid magic string:

@foo
    would be matched
@foo/bar
    would be matched
bar@foo
    would be matched
@foobar
    would not be matched

Magic strings may also be delimited with ‘{’ and ‘}’ characters, allowing for more complex patterns in symbolic links such as:

    @{var1}-@{var2}.@{var3}

The following patterns are supported:

@domainname
    Expands to the machine's domain name, as set by setdomainname(3).

@hostname
    Expands to the machine's host name, as set by sethostname(3).

@emul
    Expands to the name of the current process's emulation.

@kernel_ident
    Expands to the name of the config(1) file used to generate the running kernel.

@machine
    Expands to the value of MACHINE for the system ( equivalent to the output of “uname -m”).

@machine_arch
    Expands to the value of MACHINE_ARCH for the system ( equivalent to the output of “uname -p”).

@osrelease
    Expands to the operating system release of the running kernel ( equivalent to the output of “uname -r”).

@ostype
    Expands to the operating system type of the running kernel ( equivalent to the output of “uname -s”). This will always be “NetBSD” on NetBSD systems.

@ruid
    Expands to the real user-id of the process.

@uid
    Expands to the effective user-id of the process.

SEE ALSO
chflags(1), chgrp(1), chmod(1), cp(1), du(1), find(1), ln(1), ls(1), mv(1), pax(1), rm(1), tar(1), uname(1), chown(2), execve(2), lchflags(2), lchmod(2), lchown(2), lstat(2), lutimes(2), mount(2), readlink(2), rename(2), symlink(2), unlink(2), fts(3), remove(3), chown(8), mount(8)

HISTORY
Magic symlinks appeared in NetBSD 4.0.