1SYMLINK(7)                 Linux Programmer's Manual                SYMLINK(7)
2
3
4

NAME

6       symlink - symbolic link handling
7
9       Symbolic  links  are  files  that  act  as pointers to other files.  To
10       understand their behavior, you must first  understand  how  hard  links
11       work.
12
13       A  hard  link  to  a  file  is indistinguishable from the original file
14       because it is a reference to the object underlying the  original  file‐
15       name.   (To be precise: each of the hard links to a file is a reference
16       to the same i-node number, where an i-node number is an index into  the
17       i-node table, which contains metadata about all files on a file system.
18       See stat(2).)  Changes to a file are independent of the  name  used  to
19       reference  the  file.  Hard links may not refer to directories (to pre‐
20       vent the possibility of loops within the file system tree, which  would
21       confuse  many  programs)  and  may not refer to files on different file
22       systems (because i-node numbers are not unique across file systems).
23
24       A symbolic link is a special type of file whose contents are  a  string
25       that  is  the pathname another file, the file to which the link refers.
26       In other words, a symbolic link is a pointer to another name,  and  not
27       to  an underlying object.  For this reason, symbolic links may refer to
28       directories and may cross file system boundaries.
29
30       There is no requirement that the pathname referred  to  by  a  symbolic
31       link should exist.  A symbolic link that refers to a pathname that does
32       not exist is said to be a dangling link.
33
34       Because a symbolic link and its referenced object coexist in  the  file
35       system  name  space,  confusion can arise in distinguishing between the
36       link itself and the referenced object.  On historical systems, commands
37       and  system  calls  adopted  their  own link-following conventions in a
38       somewhat ad-hoc fashion.  Rules for a more uniform  approach,  as  they
39       are  implemented  on Linux and other systems, are outlined here.  It is
40       important that site-local applications also conform to these rules,  so
41       that the user interface can be as consistent as possible.
42
43   Symbolic link ownership, permissions, and timestamps
44       The  owner  and group of an existing symbolic link can be changed using
45       lchown(2).  The only time that the ownership of a symbolic link matters
46       is  when  the  link is being removed or renamed in a directory that has
47       the sticky bit set (see stat(2)).
48
49       The last access and last modification timestamps of a symbolic link can
50       be changed using utimensat(2) or lutimes(3).
51
52       On Linux, the permissions of a symbolic link are not used in any opera‐
53       tions; the permissions are always 0777 (read, write,  and  execute  for
54       all user categories), and can't be changed.
55
56   Handling of symbolic links by system calls and commands
57       Symbolic  links  are handled either by operating on the link itself, or
58       by operating on the object referred to by  the  link.   In  the  latter
59       case,  an  application or system call is said to follow the link.  Sym‐
60       bolic links may refer to other symbolic links, in which case the  links
61       are  dereferenced until an object that is not a symbolic link is found,
62       a symbolic link that refers to a file which does not exist is found, or
63       a  loop is detected.  (Loop detection is done by placing an upper limit
64       on the number of links that may be followed, and an  error  results  if
65       this limit is exceeded.)
66
67       There  are three separate areas that need to be discussed.  They are as
68       follows:
69
70       1. Symbolic links used as filename arguments for system calls.
71
72       2. Symbolic links specified as command-line arguments to utilities that
73          are not traversing a file tree.
74
75       3. Symbolic  links  encountered by utilities that are traversing a file
76          tree (either specified on the command line or encountered as part of
77          the file hierarchy walk).
78
79   System calls
80       The  first area is symbolic links used as filename arguments for system
81       calls.
82
83       Except as noted below, all system calls  follow  symbolic  links.   For
84       example,  if  there  were a symbolic link slink which pointed to a file
85       named afile, the system call open("slink"  ...)  would  return  a  file
86       descriptor referring to the file afile.
87
88       Various  system  calls do not follow links, and operate on the symbolic
89       link itself.  They are: lchown(2),  lgetxattr(2),  llistxattr(2),  lre‐
90       movexattr(2), lsetxattr(2), lstat(2), readlink(2), rename(2), rmdir(2),
91       and unlink(2).  Certain other system calls optionally  follow  symbolic
92       links.   They  are:  faccessat(2),  fchownat(2), fstatat(2), linkat(2),
93       open(2), openat(2),  and  utimensat(2);  see  their  manual  pages  for
94       details.   Because  remove(3)  is  an alias for unlink(2), that library
95       function also does not follow symbolic links.  When rmdir(2) is applied
96       to  a symbolic link, it fails with the error ENOTDIR.  The link(2) war‐
97       rants special discussion.  POSIX.1-2001 specifies that  link(2)  should
98       dereference  oldpath if it is a symbolic link.  However, Linux does not
99       do this.  (By default Solaris is the same, but the POSIX.1-2001  speci‐
100       fied  behavior  can  be  obtained with suitable compiler options.)  The
101       upcoming POSIX.1 revision changes the  specification  to  allow  either
102       behavior in an implementation.
103
104   Commands not traversing a file tree
105       The  second  area is symbolic links, specified as command-line filename
106       arguments, to commands which are not traversing a file tree.
107
108       Except as noted below, commands follow symbolic links named as command-
109       line arguments.  For example, if there were a symbolic link slink which
110       pointed to a file named afile, the command cat slink would display  the
111       contents of the file afile.
112
113       It  is  important to realize that this rule includes commands which may
114       optionally traverse  file  trees,  e.g.,  the  command  chown  file  is
115       included  in this rule, while the command chown -R file, which performs
116       a tree traversal, is not.  (The latter is described in the third  area,
117       below.)
118
119       If  it  is explicitly intended that the command operate on the symbolic
120       link instead of following the symbolic link, e.g., it is  desired  that
121       chown  slink change the ownership of the file that slink is, whether it
122       is a symbolic link or not, the -h option should be used.  In the  above
123       example,  chown  root  slink  would  change  the  ownership of the file
124       referred to by slink, while chown -h root slink would change the owner‐
125       ship of slink itself.
126
127       There are some exceptions to this rule:
128
129       * The  mv(1)  and  rm(1) commands do not follow symbolic links named as
130         arguments, but  respectively  attempt  to  rename  and  delete  them.
131         (Note,  if  the  symbolic link references a file via a relative path,
132         moving it to another directory may very well cause it to  stop  work‐
133         ing, since the path may no longer be correct.)
134
135       * The ls(1) command is also an exception to this rule.  For compatibil‐
136         ity with historic systems (when ls(1) is not doing a tree walk, i.e.,
137         the  -R  option is not specified), the ls(1) command follows symbolic
138         links named as arguments if the -H or -L option is specified,  or  if
139         the  -F,  -d, or -l options are not specified.  (The ls(1) command is
140         the only command where the -H and -L options affect its behavior even
141         though it is not doing a walk of a file tree.)
142
143       * The  file(1)  command is also an exception to this rule.  The file(1)
144         command does not follow symbolic links named as argument by  default.
145         The  file(1)  command does follow symbolic links named as argument if
146         the -L option is specified.
147
148   Commands traversing a file tree
149       The following commands either optionally or always traverse file trees:
150       chgrp(1),  chmod(1),  chown(1),  cp(1),  du(1), find(1), ls(1), pax(1),
151       rm(1), and tar(1).
152
153       It is important to realize that the following rules  apply  equally  to
154       symbolic  links encountered during the file tree traversal and symbolic
155       links listed as command-line arguments.
156
157       The first rule applies to symbolic links  that  reference  files  other
158       than  directories.   Operations  that  apply to symbolic links are per‐
159       formed on the links themselves, but otherwise the links are ignored.
160
161       The command rm -r slink directory will remove slink,  as  well  as  any
162       symbolic  links encountered in the tree traversal of directory, because
163       symbolic links may be removed.  In no case will rm(1) affect  the  file
164       referred to by slink.
165
166       The  second  rule  applies to symbolic links that refer to directories.
167       Symbolic links that refer to directories are never followed by default.
168       This  is often referred to as a "physical" walk, as opposed to a "logi‐
169       cal" walk (where symbolic links the refer to directories are followed).
170
171       Certain conventions are (should be) followed as consistently as  possi‐
172       ble by commands that perform file tree walks:
173
174       * A  command can be made to follow any symbolic links named on the com‐
175         mand line, regardless of the type of file they reference, by specify‐
176         ing  the -H (for "half-logical") flag.  This flag is intended to make
177         the command-line name space look like the logical name space.  (Note,
178         for  commands that do not always do file tree traversals, the -H flag
179         will be ignored if the -R flag is not also specified.)
180
181         For example, the command chown -HR user slink will traverse the  file
182         hierarchy  rooted  in  the file pointed to by slink.  Note, the -H is
183         not the same as the previously discussed -h flag.  The -H flag causes
184         symbolic  links  specified on the command line to be dereferenced for
185         the purposes of both the action to be performed and  the  tree  walk,
186         and  it is as if the user had specified the name of the file to which
187         the symbolic link pointed.
188
189       * A command can be made to follow any symbolic links named on the  com‐
190         mand  line, as well as any symbolic links encountered during the tra‐
191         versal, regardless of the type of file they reference, by  specifying
192         the  -L  (for  "logical")  flag.   This  flag is intended to make the
193         entire name space look like the logical name space.  (Note, for  com‐
194         mands that do not always do file tree traversals, the -L flag will be
195         ignored if the -R flag is not also specified.)
196
197         For example, the command chown -LR user slink will change  the  owner
198         of  the  file  referred to by slink.  If slink refers to a directory,
199         chown will traverse the file hierarchy rooted in the  directory  that
200         it references.  In addition, if any symbolic links are encountered in
201         any file tree that chown traverses, they will be treated in the  same
202         fashion as slink.
203
204       * A  command  can be made to provide the default behavior by specifying
205         the -P (for "physical") flag.  This flag  is  intended  to  make  the
206         entire name space look like the physical name space.
207
208       For  commands  that  do not by default do file tree traversals, the -H,
209       -L, and -P flags are ignored if the -R flag is not also specified.   In
210       addition,  you  may  specify the -H, -L, and -P options more than once;
211       the last one specified determines  the  command's  behavior.   This  is
212       intended  to  permit  you  to  alias  commands to behave one way or the
213       other, and then override that behavior on the command line.
214
215       The ls(1) and rm(1) commands have exceptions to these rules:
216
217       * The rm(1) command operates on the symbolic link, and not the file  it
218         references,  and  therefore never follows a symbolic link.  The rm(1)
219         command does not support the -H, -L, or -P options.
220
221       * To maintain compatibility with historic systems,  the  ls(1)  command
222         acts  a  little  differently.  If you do not specify the -F, -d or -l
223         options, ls(1) will follow symbolic links specified  on  the  command
224         line.  If the -L flag is specified, ls(1) follows all symbolic links,
225         regardless of their type, whether specified on the  command  line  or
226         encountered in the tree walk.
227

SEE ALSO

229       chgrp(1),  chmod(1),  find(1),  ln(1),  ls(1), mv(1), rm(1), lchown(2),
230       link(2), lstat(2), readlink(2), rename(2), symlink(2), unlink(2),  uti‐
231       mensat(2), lutimes(3), path_resolution(7)
232

COLOPHON

234       This  page  is  part of release 3.22 of the Linux man-pages project.  A
235       description of the project, and information about reporting  bugs,  can
236       be found at http://www.kernel.org/doc/man-pages/.
237
238
239
240Linux                             2008-06-18                        SYMLINK(7)
Impressum