symlink(7)
๐ symlink(7) ๐
okay so bestie, this is the symlink(7) manual page, and it's literally all about symbolic link handling. Buckle up, because links are about to get main character energy. ๐
๐ NAME ๐
symlink โ symbolic link handling
โจ DESCRIPTION โจ
okay so symbolic links are files that act like pointers to other files. But to really get their behavior, you first gotta understand how hard links work. no cap.
A hard link to a file is indistinguishable from the original file, because it's a reference to the object underlying the original filename. To be precise: each of the hard links to a file is a reference to the same inode number, where an inode number is an index into the inode table, which contains metadata about all files on a filesystem. See stat(2). Changes to a file are independent of the name used to reference the file. Hard links may not refer to directories (this prevents the possibility of loops within the filesystem tree, which would confuse many programs) and may not refer to files on different filesystems (because inode numbers are not unique across filesystems).
A symbolic link is a special type of file whose contents are a string that is the pathname of another file โ the file to which the link refers. (You can read the contents of a symbolic link using readlink(2).) In other words, a symbolic link is a pointer to another name, and not to an underlying object. That's why symbolic links may refer to directories and may cross filesystem boundaries. slay.
There is no requirement that the pathname referred to by a symbolic link should exist. A symbolic link that refers to a pathname that does not exist is said to be a dangling link.
Because a symbolic link and its referenced object coexist in the filesystem name space, confusion can arise when you're distinguishing between the link itself and the referenced object. On historical systems, commands and system calls adopted their own link-following conventions in a somewhat ad-hoc fashion. The rules for a more uniform approach, as they are implemented on Linux and other systems, are outlined here. It is important that site-local applications also conform to these rules, so that the user interface can be as consistent as possible.
๐ช Magic links ๐ช
There is a special class of symbolic-link-like objects known as "magic links", which can be found in certain pseudofilesystems such as proc(5) (examples include /proc/pid/exe and /proc/pid/fd/*). Unlike normal symbolic links, magic links are not resolved through pathname-expansion, but instead act as direct references to the kernel's own representation of a file handle. As such, these magic links allow users to access files which cannot be referenced with normal paths (such as unlinked files still referenced by a running program).
Because they can bypass ordinary mount_namespaces(7)-based restrictions, magic links have been used as attack vectors in various exploits. ๐ญ
๐ Symbolic link ownership, permissions, and timestamps ๐
The owner and group of an existing symbolic link can be changed using lchown(2). The ownership of a symbolic link matters when the link is being removed or renamed in a directory that has the sticky bit set (see inode(7)), and when the fs.protected_symlinks sysctl is set (see proc(5)).
The last access and last modification timestamps of a symbolic link can be changed using utimensat(2) or lutimes(3).
On Linux, the permissions of an ordinary symbolic link are not used in any operations; the permissions are always 0777 (read, write, and execute for all user categories), and can't be changed.
However, magic links do not follow this rule. They can have a non-0777 mode, though this mode is not currently used in any permission checks.
๐ซถ Obtaining a file descriptor that refers to a symbolic link ๐ซถ
Using the combination of the O_PATH and O_NOFOLLOW flags to open(2) yields a file descriptor that can be passed as the dirfd argument in system calls such as fstatat(2), fchownat(2), fchmodat(2), linkat(2), and readlinkat(2), in order to operate on the symbolic link itself (rather than the file to which it refers).
By default (i.e., if the AT_SYMLINK_FOLLOW flag is not specified), if name_to_handle_at(2) is applied to a symbolic link, it yields a handle for the symbolic link (rather than the file to which it refers). One can then obtain a file descriptor for the symbolic link (rather than the file to which it refers) by specifying the O_PATH flag in a subsequent call to open_by_handle_at(2). Again, that file descriptor can be used in the aforementioned system calls to operate on the symbolic link itself. iconic.
๐ง Handling of symbolic links by system calls and commands ๐ง
Symbolic links are handled either by operating on the link itself, or by operating on the object referred to by the link. In the latter case, an application or system call is said to follow the link. Symbolic links may refer to other symbolic links, in which case the links are dereferenced until an object that is not a symbolic link is found, a symbolic link that refers to a file which does not 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:
- Symbolic links used as filename 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 file hierarchy walk).
Before describing the treatment of symbolic links by system calls and commands, we require some terminology. Given a pathname of the form a/b/c, the part preceding the final slash (i.e., a/b) is called the dirname component, and the part following the final slash (i.e., c) is called the basename component.
โ๏ธ Treatment of symbolic links in system calls โ๏ธ
The first area is symbolic links used as filename arguments for system calls.
The treatment of symbolic links within a pathname passed to a system call is as follows:
- Within the dirname component of a pathname, symbolic links are always followed in nearly every system call. (This is also true for commands.) The one exception is
openat2(2), which provides flags that can be used to explicitly prevent following of symbolic links in the dirname component. - Except as noted below, all system calls follow symbolic links in the basename component of a pathname. For example, if there were a symbolic link
slinkwhich pointed to a file namedafile, the system callopen("slink" ...)would return a file descriptor referring to the fileafile.
Various system calls do not follow links in the basename component of a pathname, and operate on the symbolic link itself. They are: lchown(2), lgetxattr(2), llistxattr(2), lremovexattr(2), lsetxattr(2), lstat(2), readlink(2), rename(2), rmdir(2), and unlink(2).
Certain other system calls optionally follow symbolic links in the basename component of a pathname. They are: faccessat(2), fchownat(2), fstatat(2), linkat(2), name_to_handle_at(2), open(2), openat(2), open_by_handle_at(2), and utimensat(2); see their manual pages for details. Because remove(3) is an alias for unlink(2), that library function also does not follow symbolic links. When rmdir(2) is applied to a symbolic link, it fails with the error ENOTDIR.
link(2) warrants special discussion. POSIX.1-2001 specifies that link(2) should dereference oldpath if it is a symbolic link. However, Linux does not do this. (By default, Solaris is the same, but the POSIX.1-2001 specified behavior can be obtained with suitable compiler options.) POSIX.1-2008 changed the specification to allow either behavior in an implementation.
๐ Commands not traversing a file tree ๐
The second area is symbolic links, specified as command-line filename 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; for example, the command chown file is included in this rule, while the command chown -R file, which performs a tree traversal, 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 โ for example, it is desired that chown slink change the ownership of the file that slink is, whether it is a symbolic link or not โ then the -h option should be used. In the above example, chown root slink would change the ownership of the file referred to by slink, while chown -h root slink would change the ownership of slink itself.
There are some exceptions to this rule:
- The
mv(1)andrm(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 (whenls(1)is not doing a tree walk โ that is,-Roption is not specified), thels(1)command follows symbolic links named as arguments if the-Hor-Loption is specified, or if the-F,-d, or-loptions are not specified. (Thels(1)command is the only command where the-Hand-Loptions affect its behavior even though it is not doing a walk of a file tree.) - The
file(1)command is also an exception to this rule. Thefile(1)command does not follow symbolic links named as argument by default. Thefile(1)command does follow symbolic links named as argument if the-Loption is specified.
๐ณ Commands traversing a file tree ๐ณ
The following commands either optionally or always traverse file trees: chgrp(1), chmod(1), chown(1), cp(1), du(1), find(1), ls(1), pax(1), rm(1), and tar(1).
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 other than directories. Operations that apply to symbolic links are performed on the links themselves, but otherwise the links are ignored.
So like, rm -r slink directory is gonna yeet slink and every symbolic link it trips over while walking the tree of directory โ because symbolic links are removable, bestie, they're just little name-arrows ๐
. But in no case will rm(1) ever touch the actual file that slink points at. Never. Not once. Periodt. ๐
โโ๏ธ
๐ฆ The Second Rule, But Make It Directories ๐ฆ
Rule number two is about symbolic links that point at directories, and here's the tea: those babies are never followed by default. That's what the girlies call a "physical" walk โ as opposed to a "logical" walk, where symlinks-to-directories do get followed. Two completely different vibes, fr fr. ๐ธ
And certain conventions are (should be ๐) followed as consistently as possible by commands that do file tree walks:
- A command can be made to follow any symbolic links named right there on the command line โ no matter what type of file they reference โ by specifying the
-Hflag (the "H" is for "half-logical", iykyk). This flag is meant to make the command-line name space look like the logical name space. (Note, bestie: for commands that don't always do file tree traversals, the-Hflag just gets ignored if the-Rflag isn't also specified. ๐ซ )
For example, the command chown -HR user slink will traverse the file hierarchy rooted in the file pointed to by slink. And no, -H is not the same as that -h flag we talked about earlier โ do not confuse them, it's giving different. ๐ The -H flag causes symbolic links specified on the command line to be dereferenced for the purposes of both the action to be performed and the tree walk, and it's as if the user had just typed the name of the file that the symbolic link pointed to. Same energy, different spelling. โจ
- A command can be made to follow any symbolic links named on the command line, plus any symbolic links it encounters during the traversal โ regardless of the type of file they reference โ by specifying the
-Lflag (that's "logical", queen ๐). This one is intended to make the entire name space look like the logical name space. (Note again, for commands that don't always do file tree traversals, the-Lflag will be ignored if the-Rflag isn't also specified. Yes, same rule. Consistency is the moment. ๐)
For example, the command chown -LR user slink will change the owner of the file referred to by slink. And if slink refers to a directory? Then chown will traverse the file hierarchy rooted in the directory that it references. Plus, if any symbolic links are encountered in any file tree that chown traverses, they get treated in the same fashion as slink. It's giving full commitment. ๐ฅ
- A command can be made to give you the default behavior by specifying the
-Pflag (you guessed it: "physical"). This flag is intended to make the entire name space look like the physical name space. Chill, classic, no notes. ๐
For commands that don't do file tree traversals by default, the -H, -L, and -P flags are ignored if the -R flag isn't also specified. And plot twist: you can specify the -H, -L, and -P options more than once, and the last one specified wins โ that's what determines the command's behavior. This whole thing exists so you can alias commands to behave one way or the other, and then override that behavior right on the command line. Iconic, honestly. ๐
๐ The Exceptions (aka the ones who don't play by the rules) ๐
The ls(1) and rm(1) commands have exceptions to these rules:
- The
rm(1)command operates on the symbolic link itself, and not the file it references, and therefore never follows a symbolic link. Also โrm(1)does not support the-H,-L, or-Poptions at all. So don't even try it, bestie. ๐ - To maintain compatibility with historic systems, the
ls(1)command acts a little differently. If you don't specify the-F,-d, or-loptions,ls(1)will follow symbolic links specified on the command line. And if the-Lflag is specified,ls(1)follows all symbolic links, regardless of their type โ whether they're specified on the command line or encountered in the tree walk. She's an old soul with her own rules. ๐
๐ SEE ALSO ๐
chgrp(1), chmod(1), find(1), ln(1), ls(1), mv(1), namei(1), rm(1), lchown(2), link(2), lstat(2), readlink(2), rename(2), symlink(2), unlink(2), utimensat(2), lutimes(3), path_resolution(7)
And that's the whole page, bestie!! ๐โจ This lil masterpiece is from Linux man-pages 6.20, dated 2026-02-08, and it's the last page of symlink(7) โ go forth and dereference responsibly, mwah ๐
๐ซถ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-3-Clause
- Copyright 1992-1994, The Regents of the University of California
- Copyright 2008-2014, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms