Symbolic link: Difference between revisions
Citation bot (talk | contribs) Alter: title. | Use this bot. Report bugs. | Suggested by Whoop whoop pull up | #UCB_webform 1679/3660 |
Citation bot (talk | contribs) Add: title, date, website. Changed bare reference to CS1/2. | Use this bot. Report bugs. | Suggested by Mako001 | Linked from User:Mako001/Missingtitle2 | #UCB_webform_linked 2088/3418 |
||
(19 intermediate revisions by 17 users not shown) | |||
Line 18: | Line 18: | ||
==POSIX and Unix-like operating systems== |
==POSIX and Unix-like operating systems== |
||
In [[POSIX]]-compliant operating systems, symbolic links are created with the <code>symlink</code><ref>[https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html symlink, symlinkat]. IEEE Std 1003.1, 2013 Edition.</ref> system call. The <code>[[ln (Unix)|ln]]</code> shell command normally uses the <code>link</code><ref>[https://pubs.opengroup.org/onlinepubs/9699919799/functions/link.html link, linkat]. IEEE Std 1003.1, 2013 Edition.</ref> system call, which creates a [[hard link]]. When the <code>ln ''-s''</code> flag is specified, the symlink() system call is used instead, creating a symbolic link. Symlinks were introduced in [[Berkeley Software Distribution|4. |
In [[POSIX]]-compliant operating systems, symbolic links are created with the <code>symlink</code><ref>[https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html symlink, symlinkat]. IEEE Std 1003.1, 2013 Edition.</ref> system call. The <code>[[ln (Unix)|ln]]</code> shell command normally uses the <code>link</code><ref>[https://pubs.opengroup.org/onlinepubs/9699919799/functions/link.html link, linkat]. IEEE Std 1003.1, 2013 Edition.</ref> system call, which creates a [[hard link]]. When the <code>ln ''-s''</code> flag is specified, the symlink() system call is used instead, creating a symbolic link. Symlinks were introduced in 1982 in [[Berkeley Software Distribution|4.1a BSD Unix]] from [[Computer Systems Research Group|U.C. Berkeley]].<ref>{{cite web |author1=Bill Joy |author2=Sam Leffler |author1-link=Bill Joy |author2-link=Samuel J. Leffler |title=Surviving with 4.1a bsd |website=[[GitHub]] |url=https://github.com/dspinellis/unix-history-repo/blob/BSD-4_1c_2/usr/man/man0/changes.4-82#L28 |access-date=8 September 2023 |quote=It also includes a few other features which you may find useful, such as ‘‘symbolic links’’ and an improved group scheme.}}</ref> |
||
The following command creates a symbolic link at the [[command-line interface]] (shell): |
The following command creates a symbolic link at the [[command-line interface]] (shell): |
||
Line 83: | Line 83: | ||
===NTFS symbolic link=== |
===NTFS symbolic link=== |
||
{{Main|NTFS symbolic link}} |
{{Main|NTFS symbolic link}} |
||
[[NTFS]] 3.1 introduced support for symbolic links for any type of file. It was included with [[Windows XP]], but was only enabled by default for kernel |
[[NTFS]] 3.1 introduced support for symbolic links for any type of file. It was included with [[Windows XP]], but was only enabled by default for kernel-mode apps. [[Windows Vista]] and later versions of Windows enabled support for symbolic links to user-mode applications. The <code>mklink</code> internal command of [[Windows command prompt|Windows Command Prompt]] can create symbolic links. Third-party drivers are required to enable support for NTFS symbolic links in Windows XP.<ref>{{cite web |url=https://schinagl.priv.at/nt/hardlinkshellext/hardlinkshellext.html#symboliclinksforwindowsxp |title=Link Shell Extension website |website=Link Shell Extension website}}</ref> Unlike [[NTFS junction point|junction point]]s, a symbolic link can also point to a file or remote [[Server Message Block]] (SMB) network path. Additionally, the NTFS symbolic link implementation provides full support for cross-filesystem links. However, the functionality enabling cross-host symbolic links requires that the remote system also support them. |
||
Symbolic links are designed to aid in migration and application compatibility with [[POSIX]] operating systems. Microsoft aimed for Windows Vista's symbolic links to "function just like UNIX links".<ref>[https://msdn.microsoft.com/en-us/library/aa365680.aspx Symbolic Links], MSDN Library, Win32 and COM Development, 2008-01-18</ref> However, the implementation differs from Unix symbolic links in several ways. For example, Windows Vista users must manually indicate when creating a symbolic link whether it is a file or a directory.<ref>{{Cite web|title=CreateSymbolicLinkA function (winbase.h)|url=https://msdn.microsoft.com/en-us/library/aa363866.aspx |website=[[MSDN]]}}</ref> Windows 7 and Vista support a maximum of 31 [[reparse point]]s (and therefore symbolic links) for a given path (i.e. any given path can have at most 31 indirections before Windows gives up).<ref>[https://msdn.microsoft.com/en-us/library/aa365460(VS.85).aspx Symbolic Link Programming Considerations], MSDN</ref> Only users with the new ''Create Symbolic Link'' privilege, which only administrators have by default, can create symbolic links.<ref>Mark Russinovich: [https://www.microsoft.com/technet/technetmag/issues/2007/02/VistaKernel/default.aspx Inside the Windows Vista Kernel: Part 1] – File-based symbolic links, Microsoft Technet, February 2007.</ref> If this is not the desired behavior, it must be changed in the Local Security Policy management console. Additionally, NTFS symbolic links to files are distinct from NTFS symbolic links to directories and therefore cannot be used interchangeably, unlike on POSIX where the same symbolic link can refer to either files or directories. |
Symbolic links are designed to aid in migration and application compatibility with [[POSIX]] operating systems. Microsoft aimed for Windows Vista's symbolic links to "function just like UNIX links".<ref>[https://msdn.microsoft.com/en-us/library/aa365680.aspx Symbolic Links], MSDN Library, Win32 and COM Development, 2008-01-18</ref> However, the implementation differs from Unix symbolic links in several ways. For example, Windows Vista users must manually indicate when creating a symbolic link whether it is a file or a directory.<ref>{{Cite web|title=CreateSymbolicLinkA function (winbase.h)|url=https://msdn.microsoft.com/en-us/library/aa363866.aspx |website=[[MSDN]]|date=June 2023 }}</ref> Windows 7 and Vista support a maximum of 31 [[reparse point]]s (and therefore symbolic links) for a given path (i.e. any given path can have at most 31 indirections before Windows gives up).<ref>[https://msdn.microsoft.com/en-us/library/aa365460(VS.85).aspx Symbolic Link Programming Considerations], MSDN</ref> Only users with the new ''Create Symbolic Link'' privilege, which only administrators have by default, can create symbolic links.<ref>Mark Russinovich: [https://www.microsoft.com/technet/technetmag/issues/2007/02/VistaKernel/default.aspx Inside the Windows Vista Kernel: Part 1] – File-based symbolic links, Microsoft Technet, February 2007.</ref> If this is not the desired behavior, it must be changed in the Local Security Policy management console. Additionally, NTFS symbolic links to files are distinct from NTFS symbolic links to directories and therefore cannot be used interchangeably, unlike on POSIX where the same symbolic link can refer to either files or directories. |
||
In Windows Vista and later, when the working directory path ends with a symbolic link, the current parent path reference, {{code|..}}, will refer to the parent directory of the symbolic link rather than that of its target. This |
In Windows Vista and later, when the working directory path ends with a symbolic link, the current parent path reference, {{code|..}}, will refer to the parent directory of the symbolic link rather than that of its target. This behavior is also found at the shell level in at least some POSIX systems, including [[Linux]], but never in accessing files and directories through operating system calls. For instance, bash builtin commands {{code|pwd}} and {{code|cd}} operate on the current logical directory. {{code|pwd}} is often used in scripts to determine the actual current working directory. When any path is used with a system call, any use of {{code|..}} will use the actual filesystem parent of the directory containing the {{code|..}} pseudo-directory entry. So, {{code|cd ..; cat something}} and {{code|cat ../something}} may return completely different results. |
||
==== Examples ==== |
==== Examples ==== |
||
The following examples both create a symbolic link called "Downloads" at "E:\" that points to the Downloads folder in the current user's profile. |
|||
⚫ | |||
PS > mklink /D "$env:LOCALAPPDATA\Plex Media Server" "G:\Plex Media Server" |
|||
* The first example works in [[Windows command prompt|Windows Command Prompt]] only because <code>mklink</code> is an internal command. |
|||
⚫ | |||
mklink /D E:\Downloads %UserProfile%\Downloads |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
⚫ | |||
* The second example works in [[PowerShell]] only because New-Item is an internal cmdlet. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 108: | Line 113: | ||
* Shortcuts are treated like ordinary files by the file system and by software programs that are not aware of them. Only software programs that understand shortcuts (such as the Windows shell and file browsers) treat them as references to other files. |
* Shortcuts are treated like ordinary files by the file system and by software programs that are not aware of them. Only software programs that understand shortcuts (such as the Windows shell and file browsers) treat them as references to other files. |
||
The mechanisms also have different capabilities: |
|||
* [[Microsoft Windows]] shortcuts normally refer to a destination by an [[absolute path]] (starting from the [[root directory]]), whereas POSIX symbolic links can refer to destinations via either an absolute or a [[relative path]]. The latter is useful if both the symlink and its target share some common ancestor path which is not known at creation (e.g., in an [[archive file]] that can be unpacked anywhere). |
* [[Microsoft Windows]] shortcuts normally refer to a destination by an [[absolute path]] (starting from the [[root directory]]), whereas POSIX symbolic links can refer to destinations via either an absolute or a [[relative path]]. The latter is useful if both the symlink and its target share some common ancestor path which is not known at creation (e.g., in an [[archive file]] that can be unpacked anywhere). |
||
* Microsoft Windows application shortcuts contain additional metadata that can be associated with the destination, whereas POSIX symbolic links are just strings that will be interpreted as absolute or relative pathnames. |
* Microsoft Windows application shortcuts contain additional metadata that can be associated with the destination, whereas POSIX symbolic links are just strings that will be interpreted as absolute or relative pathnames. |
||
* Unlike symbolic links, Windows shortcuts maintain their references to their targets even when the target is moved or renamed. Windows domain clients may subscribe to a [[Windows service]] called |
* Unlike symbolic links, Windows shortcuts maintain their references to their targets even when the target is moved or renamed. Windows domain clients may subscribe to a [[Windows service]] called Distributed Link Tracking<ref>{{cite web | url=https://learn.microsoft.com/en-us/troubleshoot/windows-server/backup-and-storage/distributed-link-tracking-on-domain-controller | title=Distributed Link Tracking on domain controllers - Windows Server | date=23 February 2023 }}</ref> to track the changes in files and folders to which they are interested. The service maintains the integrity of shortcuts, even when files and folders are moved across the network.<ref>{{cite web |
||
|title=Distributed Link Tracking and Object Identifiers |
|title=Distributed Link Tracking and Object Identifiers |
||
|url=https://msdn.microsoft.com/en-us/library/aa363997%28v=VS.85%29.aspx |
|url=https://msdn.microsoft.com/en-us/library/aa363997%28v=VS.85%29.aspx |
||
Line 121: | Line 126: | ||
====Folder shortcuts==== |
====Folder shortcuts==== |
||
Almost like shortcuts, but transparent to the Windows shell.<ref>{{cite web|url=https://msdn.microsoft.com/en-us/library/bb776817.aspx|title=Specifying a Namespace Extension's Location|website=msdn.microsoft.com|access-date=23 March 2018}}</ref> They are implemented as ordinary folders (which need to have the ''read only'' and/or ''system'' attribute<ref>{{Cite web|title=You cannot view or change the Read-only or the System attributes of folders in Windows Server 2003, in Windows XP, in Windows Vista or in Windows 7|url=https://support.microsoft.com/kb/256614/en-us |access-date=2021-07-08|website=support.microsoft.com}}</ref>) containing a shortcut named ''target.lnk'' which refers to the target and a (hidden) ''desktop.ini'' with (at least) the following contents: |
Almost like shortcuts, but transparent to the Windows shell.<ref>{{cite web|url=https://msdn.microsoft.com/en-us/library/bb776817.aspx|title=Specifying a Namespace Extension's Location|website=msdn.microsoft.com|date=11 January 2008 |access-date=23 March 2018}}</ref> They are implemented as ordinary folders (which need to have the ''read only'' and/or ''system'' attribute<ref>{{Cite web|title=You cannot view or change the Read-only or the System attributes of folders in Windows Server 2003, in Windows XP, in Windows Vista or in Windows 7|url=https://support.microsoft.com/kb/256614/en-us |access-date=2021-07-08|website=support.microsoft.com}}</ref>) containing a shortcut named ''target.lnk'' which refers to the target and a (hidden) ''desktop.ini'' with (at least) the following contents: |
||
<syntaxhighlight lang="ini"> |
<syntaxhighlight lang="ini"> |
||
[.ShellClassInfo] |
[.ShellClassInfo] |
||
Line 133: | Line 138: | ||
A minimal implementation is (the CLSID ''{00000000-0000-0000-0000-000000000000}'' is used as a placeholder): |
A minimal implementation is (the CLSID ''{00000000-0000-0000-0000-000000000000}'' is used as a placeholder): |
||
<syntaxhighlight lang=" |
<syntaxhighlight lang="registry"> |
||
[HKEY_CLASSES_ROOT\CLSID\{00000000-0000-0000-0000-000000000000}] |
[HKEY_CLASSES_ROOT\CLSID\{00000000-0000-0000-0000-000000000000}] |
||
@="display name" |
@="display name" |
||
Line 166: | Line 171: | ||
==Comparison of POSIX and Windows symbolic links== |
==Comparison of POSIX and Windows symbolic links== |
||
{| class="wikitable plainrowheaders" |
{| class="wikitable plainrowheaders" style="text-align:center" |
||
! |
|||
! colspan="2" | Property/action |
|||
! Symbolic link |
! Symbolic link |
||
! [[NTFS junction point|Junction]] |
! [[NTFS junction point|Junction]] |
||
! [[Hard link]] |
! [[Hard link]] |
||
|- |
|- |
||
! |
! scope="row" | When the link is deleted... |
||
| |
| The target remains unchanged |
||
| |
| The target is deleted{{efn|except when using special tools}} |
||
| |
| A reference counter is decremented. When it reaches 0, the target is deleted. |
||
|- |
|- |
||
! |
! scope="row" | When target is moved... |
||
| |
| The link becomes invalid |
||
| |
| The link becomes invalid |
||
| |
| The link remains valid |
||
|- |
|- |
||
! |
! scope="row" | Relative path |
||
| {{yes|Allowed}} |
| {{yes|Allowed}} |
||
| {{No|Not allowed |
| {{No|Not allowed}}{{efn|On saving, becomes an absolute path}} |
||
| {{N/A}} |
| {{N/A}} |
||
|- |
|- |
||
! |
! scope="row" | Can be on a different volume? |
||
| {{yes |
| {{yes}} |
||
| {{yes |
| {{yes}} |
||
| {{No}} |
|||
| {{No|Not supported <small>(target must be on same filesystem)</small>}} |
|||
|- |
|- |
||
! |
! scope="row" | Link to files on [[Windows]] |
||
| rowspan="2" {{yes}}{{Efn|Supported on Windows Vista and later. The Windows implementation is not POSIX-compliant. Creating them requires the "create symbolic link" privilege (SeCreateSymbolicLinkPrivilege). By default a user account holds this privilege when it is either an administrator or has Developer Mode enabled (Windows 10 v1703 and later).<ref>{{cite web |title=Create symbolic links |url=https://learn.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links |website=Windows client documentation for IT Pros |publisher=[[Microsoft]] |via=Microsoft Learn |date=18 January 2023}}</ref>}} |
|||
! scope="row" | For files |
|||
| rowspan="2" {{partial|Windows Vista and later{{efn|The Windows implementation is not POSIX-compliant.}}<br /><small>(administrator rights required)</small>}} |
|||
| {{No}} |
| {{No}} |
||
| {{yes}} |
| {{yes}} |
||
|- |
|- |
||
! scope="row" | |
! scope="row" | Link to folders on [[Windows]] |
||
| {{yes}} |
| {{yes}} |
||
| {{No}} |
| {{No}} |
||
|- |
|- |
||
! |
! scope="row" | Link to files on [[Unix]] |
||
! scope="row" | For files |
|||
| {{yes}} |
| {{yes}} |
||
| {{N/A}} |
| {{N/A}} |
||
| {{yes}} |
| {{yes}} |
||
|- |
|- |
||
! scope="row" | |
! scope="row" | Link to folders on [[Unix]] |
||
| {{yes}} |
| {{yes}} |
||
| {{N/A}} |
| {{N/A}} |
||
| {{partial}}{{efn|POSIX permits hard links on |
| {{partial}}{{efn|POSIX permits hard links on folders but does not require them. Modern file systems tend to not support it.}} |
||
|} |
|} |
||
{{notelist}} |
{{notelist}} |
||
==Other implementations== |
==Other implementations== |
||
Implementations of features similar to symbolic links. |
Implementations of features similar to symbolic links. |
||
Line 219: | Line 223: | ||
===Early MIT=== |
===Early MIT=== |
||
[[MIT]] [[Compatible Time-Sharing System]] |
[[MIT]] [[Compatible Time-Sharing System]] {{circa|1963}} and [[Incompatible Timesharing System]] both have linked files where the name of the target file is specified in a directory entry.<ref name="50th"/><ref name="ctsspg69"/><ref name="ctsspg63"/> |
||
===Amiga=== |
===Amiga=== |
||
Line 244: | Line 248: | ||
* [[Symlink race]] — a security-vulnerability caused by symbolic links |
* [[Symlink race]] — a security-vulnerability caused by symbolic links |
||
* [[freedup]] — generates links between identical data automatically |
* [[freedup]] — generates links between identical data automatically |
||
* [[Pointer (computer programming)]] |
|||
==References== |
==References== |
||
Line 256: | Line 261: | ||
{{Computer files}} |
{{Computer files}} |
||
{{File systems}} |
|||
[[Category:Computer file systems]] |
[[Category:Computer file systems]] |
Latest revision as of 19:32, 19 February 2024
In computing, a symbolic link (also symlink or soft link) is a file whose purpose is to point to a file or directory (called the "target") by specifying a path thereto.[1]
Symbolic links are supported by POSIX and by most Unix-like operating systems, such as FreeBSD, Linux, and macOS. Limited support also exists in Windows 7 and Windows Vista, and to some degree in Windows 2000 and Windows XP in the form of shortcut files. CTSS on IBM 7090 had files linked by name in 1963.[2][3][4] By 1978 minicomputer operating systems from DEC, and in Data General's RDOS included symbolic links.
Overview
[edit]A symbolic link contains a text string that is automatically interpreted and followed by the operating system as a path to another file or directory. This other file or directory is called the "target". The symbolic link is a second file that exists independently of its target. If a symbolic link is deleted, its target remains unaffected. If a symbolic link points to a target, and sometime later that target is moved, renamed or deleted, the symbolic link is not automatically updated or deleted, but continues to exist and still points to the old target, now a non-existing location or file. Symbolic links pointing to moved or non-existing targets are sometimes called broken, orphaned, dead, or dangling.
Symbolic links are different from hard links. Hard links do not link paths on different volumes or file systems, whereas symbolic links may point to any file or directory irrespective of the volumes on which the link and target reside. Hard links always refer to an existing file, whereas symbolic links may contain an arbitrary path that does not point to anything.
Symbolic links operate transparently for many operations: programs that read or write to files named by a symbolic link will behave as if operating directly on the target file. However, they have the effect of changing an otherwise hierarchic filesystem from a tree into a directed graph, which can have consequences for such simple operations as determining the current directory of a process. Even the Unix standard for navigating to a directory's parent directory no longer works reliably in the face of symlinks. Some shells heuristically try to uphold the illusion of a tree-shaped hierarchy, but when they do, this causes them to produce different results from other programs that manipulate pathnames without such heuristic, relying on the operating system instead.[5] Programs that need to handle symbolic links specially (e.g., shells and backup utilities) thus need to identify and manipulate them directly.
Some Unix as well as Linux distributions use symbolic links extensively in an effort to reorder the file system hierarchy. This is accomplished with several mechanisms, such as variant, context-dependent symbolic links. This offers the opportunity to create a more intuitive or application-specific directory tree and to reorganize the system without having to redesign the core set of system functions and utilities.
POSIX and Unix-like operating systems
[edit]In POSIX-compliant operating systems, symbolic links are created with the symlink
[6] system call. The ln
shell command normally uses the link
[7] system call, which creates a hard link. When the ln -s
flag is specified, the symlink() system call is used instead, creating a symbolic link. Symlinks were introduced in 1982 in 4.1a BSD Unix from U.C. Berkeley.[8]
The following command creates a symbolic link at the command-line interface (shell):
ln -s target_path link_path
target_path is the relative or absolute path to which the symbolic link should point. Usually the target will exist, although symbolic links may be created to non-existent targets. link_path is the path of the symbolic link.
After creating the symbolic link, some operations can be used to treat it as an alias for the target. However, the lstat
,[9] lchown
[10] and readlink
[11] operations are unique to symbolic links and do not apply to the target; by using those system calls, programs that examine the file system (e.g., ls
, find
) can report on symbolic links (instead of their targets, if any). Because the rename
and unlink
system calls are coded to operate directly on symbolic links, file system management commands (e.g., rm
, mv
) affect the symbolic link itself (instead of being applied to the symbolic link target, if any). The rm
(delete file) command removes the link itself, not the target file. Likewise, the mv
command moves or renames the link, not the target. The cp
command has options that allow either the symbolic link or the target to be copied. Commands which read or write file contents will access the contents of the target file.
The POSIX directory listing application, ls
, denotes symbolic links with an arrow after the name, pointing to the name of the target file (see following example), when the long directory list is requested (-l
option). When a directory listing of a symbolic link that points to a directory is requested, only the link itself will be displayed. In order to obtain a listing of the linked directory, the path must include a trailing directory separator character ('/', slash).
Note: In the example below do not create "three" directory before creation of link in /tmp directory.
$ mkdir -p /tmp/one/two
$ echo "test_a" >/tmp/one/two/a
$ echo "test_b" >/tmp/one/two/b
$ cd /tmp/one/two
$ ls -l
-rw-r--r-- 1 user group 7 Jan 01 10:01 a
-rw-r--r-- 1 user group 7 Jan 01 10:01 b
$ cd /tmp
$ ln -s /tmp/one/two three
$ ls -l three
lrwxrwxrwx 1 user group 12 Jul 22 10:02 /tmp/three -> /tmp/one/two
$ ls -l three/
-rw-r--r-- 1 user group 7 Jan 01 10:01 a
-rw-r--r-- 1 user group 7 Jan 01 10:01 b
$ cd three
$ ls -l
-rw-r--r-- 1 user group 7 Jan 01 10:01 a
-rw-r--r-- 1 user group 7 Jan 01 10:01 b
$ cat a
test_a
$ cat /tmp/one/two/a
test_a
$ echo "test_c" >/tmp/one/two/a
$ cat /tmp/one/two/a
test_c
$ cat a
test_c
Storage of symbolic links
[edit]Early implementations of symbolic links stored the symbolic link information as data in regular files. The file contained the textual reference to the link's target, and the file mode bits indicated that the type of the file is a symbolic link.
This method was slow and an inefficient use of disk-space on small systems. An improvement, called fast symlinks, allowed storage of the target path within the data structures used for storing file information on disk (inodes). This space normally stores a list of disk block addresses allocated to a file. Thus, symlinks with short target paths are accessed quickly. Systems with fast symlinks often fall back to using the original method if the target path exceeds the available inode space. The original style is retroactively termed a slow symlink. It is also used for disk compatibility with other or older versions of operating systems.
Although storing the link value inside the inode saves a disk block and a disk read, the operating system still needs to parse the path name in the link, which always requires reading additional inodes and generally requires reading other, and potentially many, directories, processing both the list of files and the inodes of each of them until it finds a match with the link's path components. Only when a link points to a file in the same directory do "fast symlinks" provide significantly better performance than other symlinks.
The vast majority of POSIX-compliant implementations use fast symlinks. However, the POSIX standard does not require the entire set of file status information common to regular files to be implemented for symlinks. This allows implementations to use other solutions, such as storing symlink data in directory entries.
The file system permissions of a symbolic link are not used; the access modes of the target file are controlled by the target file's own permissions. Some operating systems, such as FreeBSD, offer the ability to modify file permissions and filesystem attributes of a symbolic link, through lchmod
[12] and lchflags
[13] system calls respectively.
The reported size of a symlink is the number of characters in the path it points to.
Error handling
[edit]A traditional Unix filesystem has a tree structure,[14] however symbolic links allow it to contain loops.[5]
Microsoft Windows
[edit]NTFS symbolic link
[edit]NTFS 3.1 introduced support for symbolic links for any type of file. It was included with Windows XP, but was only enabled by default for kernel-mode apps. Windows Vista and later versions of Windows enabled support for symbolic links to user-mode applications. The mklink
internal command of Windows Command Prompt can create symbolic links. Third-party drivers are required to enable support for NTFS symbolic links in Windows XP.[15] Unlike junction points, a symbolic link can also point to a file or remote Server Message Block (SMB) network path. Additionally, the NTFS symbolic link implementation provides full support for cross-filesystem links. However, the functionality enabling cross-host symbolic links requires that the remote system also support them.
Symbolic links are designed to aid in migration and application compatibility with POSIX operating systems. Microsoft aimed for Windows Vista's symbolic links to "function just like UNIX links".[16] However, the implementation differs from Unix symbolic links in several ways. For example, Windows Vista users must manually indicate when creating a symbolic link whether it is a file or a directory.[17] Windows 7 and Vista support a maximum of 31 reparse points (and therefore symbolic links) for a given path (i.e. any given path can have at most 31 indirections before Windows gives up).[18] Only users with the new Create Symbolic Link privilege, which only administrators have by default, can create symbolic links.[19] If this is not the desired behavior, it must be changed in the Local Security Policy management console. Additionally, NTFS symbolic links to files are distinct from NTFS symbolic links to directories and therefore cannot be used interchangeably, unlike on POSIX where the same symbolic link can refer to either files or directories.
In Windows Vista and later, when the working directory path ends with a symbolic link, the current parent path reference, ..
, will refer to the parent directory of the symbolic link rather than that of its target. This behavior is also found at the shell level in at least some POSIX systems, including Linux, but never in accessing files and directories through operating system calls. For instance, bash builtin commands pwd
and cd
operate on the current logical directory. pwd
is often used in scripts to determine the actual current working directory. When any path is used with a system call, any use of ..
will use the actual filesystem parent of the directory containing the ..
pseudo-directory entry. So, cd ..; cat something
and cat ../something
may return completely different results.
Examples
[edit]The following examples both create a symbolic link called "Downloads" at "E:\" that points to the Downloads folder in the current user's profile.
- The first example works in Windows Command Prompt only because
mklink
is an internal command.
mklink /D E:\Downloads %UserProfile%\Downloads
- The second example works in PowerShell only because New-Item is an internal cmdlet.
New-Item -Path 'E:\Downloads' -ItemType 'SymbolicLink' -Value "$Env:UserProfile\Downloads"
NTFS junction points
[edit]The Windows 2000 version of NTFS introduced reparse points, which enabled, among other things, the use of Volume Mount Points and junction points. Junction points are for directories only, and moreover, local directories only; junction points to remote shares are unsupported.[20] The Windows 2000 and XP Resource Kits include a program called linkd to create junction points; a more powerful one named Junction was distributed by Sysinternals' Mark Russinovich.
Not all standard applications support reparse points. Most noticeably, Backup suffers from this problem and will issue an error message 0x80070003[21] when the folders to be backed up contain a reparse point.
Shortcuts
[edit]Shortcuts, which are supported by the graphical file browsers of some operating systems, may resemble symbolic links but differ in a number of important ways. One difference is what type of software is able to follow them:
- Symbolic links are automatically resolved by the file system. Any software program, upon accessing a symbolic link, will see the target instead, whether the program is aware of symbolic links or not.
- Shortcuts are treated like ordinary files by the file system and by software programs that are not aware of them. Only software programs that understand shortcuts (such as the Windows shell and file browsers) treat them as references to other files.
The mechanisms also have different capabilities:
- Microsoft Windows shortcuts normally refer to a destination by an absolute path (starting from the root directory), whereas POSIX symbolic links can refer to destinations via either an absolute or a relative path. The latter is useful if both the symlink and its target share some common ancestor path which is not known at creation (e.g., in an archive file that can be unpacked anywhere).
- Microsoft Windows application shortcuts contain additional metadata that can be associated with the destination, whereas POSIX symbolic links are just strings that will be interpreted as absolute or relative pathnames.
- Unlike symbolic links, Windows shortcuts maintain their references to their targets even when the target is moved or renamed. Windows domain clients may subscribe to a Windows service called Distributed Link Tracking[22] to track the changes in files and folders to which they are interested. The service maintains the integrity of shortcuts, even when files and folders are moved across the network.[23] Additionally, in Windows 9x and later, Windows shell tries to find the target of a broken shortcut before proposing to delete it.
Folder shortcuts
[edit]Almost like shortcuts, but transparent to the Windows shell.[24] They are implemented as ordinary folders (which need to have the read only and/or system attribute[25]) containing a shortcut named target.lnk which refers to the target and a (hidden) desktop.ini with (at least) the following contents:
[.ShellClassInfo]
CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}
Folder shortcuts are created and used from the Windows shell in the network neighborhood for example.
Shell objects
[edit]The shell objects[26] or shell folders are defined in the Windows registry and can be used to implement a sort of symbolic link too. Like folder shortcuts, they are transparent to the Windows shell.
A minimal implementation is (the CLSID {00000000-0000-0000-0000-000000000000} is used as a placeholder):
[HKEY_CLASSES_ROOT\CLSID\{00000000-0000-0000-0000-000000000000}]
@="display name"
[HKEY_CLASSES_ROOT\CLSID\{00000000-0000-0000-0000-000000000000}\DefaultIcon]
@="..." ; path to icon
[HKEY_CLASSES_ROOT\CLSID\{00000000-0000-0000-0000-000000000000}\InProcServer32]
@="%SystemRoot%\\System32\\ShDocVw.Dll"
"ThreadingModel"="Apartment"
[HKEY_CLASSES_ROOT\CLSID\{00000000-0000-0000-0000-000000000000}\Instance]
"CLSID"="{0AFACED1-E828-11D1-9187-B532F1E9575D}"
[HKEY_CLASSES_ROOT\CLSID\{00000000-0000-0000-0000-000000000000}\Instance\InitPropertyBag]
"Attributes"=hex:15,00,00,00
"Target"="..." ; absolute (WITHOUT "TargetKnownFolder" or "TargetSpecialFolder" only)
; or relative path to target
"TargetKnownFolder"="{guidguid-guid-guid-guid-guidguidguid}" ; GUID of target folder, Windows Vista and later
"TargetSpecialFolder"="0x00xy" ; CSIDL of target
[HKEY_CLASSES_ROOT\CLSID\{00000000-0000-0000-0000-000000000000}\ShellFolder]
"Attributes"=hex:00,00,00,00
The My Documents folder on the Desktop as well as the Fonts and the Administrative Tools folders in the Control Panel are examples of shell objects redirected to file-system folders.
Cygwin symbolic links
[edit]Cygwin simulates POSIX-compliant symbolic links in the Microsoft Windows file system. It uses identical programming and user utility interfaces as Unix (see above), but creates Windows shortcuts (.lnk files) with additional information used by Cygwin at the time of symlink resolution. Cygwin symlinks are compliant with the POSIX standard in terms of how they are resolved, and with Windows standards in terms of their on-disk representation.
Additionally, Cygwin can be set up to support native Windows symbolic links which can be used out of Cygwin without restrictions.[27] This requires:
- Changing the CYGWIN environment variable to contain winsymlinks:native;
- Running the Cygwin with elevated rights because Windows restricts the creation of symbolic links to privileged users
Some differences exist, however. Cygwin has no way to specify shortcut-related information – such as working directory or icon – as there is no place for such parameters in ln -s
command. To create standard Microsoft .lnk files Cygwin provides the mkshortcut
and readshortcut
utilities.[28]
The Cygwin User's Guide has more information on this topic.[27] MSYS2, which is based on Cygwin, has a similar set of winsymlinks settings but defaults to copying the files.[29]
Comparison of POSIX and Windows symbolic links
[edit]Symbolic link | Junction | Hard link | |
---|---|---|---|
When the link is deleted... | The target remains unchanged | The target is deleted[a] | A reference counter is decremented. When it reaches 0, the target is deleted. |
When target is moved... | The link becomes invalid | The link becomes invalid | The link remains valid |
Relative path | Allowed | Not allowed[b] | — |
Can be on a different volume? | Yes | Yes | No |
Link to files on Windows | Yes[c] | No | Yes |
Link to folders on Windows | Yes | No | |
Link to files on Unix | Yes | — | Yes |
Link to folders on Unix | Yes | — | Partial[d] |
- ^ except when using special tools
- ^ On saving, becomes an absolute path
- ^ Supported on Windows Vista and later. The Windows implementation is not POSIX-compliant. Creating them requires the "create symbolic link" privilege (SeCreateSymbolicLinkPrivilege). By default a user account holds this privilege when it is either an administrator or has Developer Mode enabled (Windows 10 v1703 and later).[30]
- ^ POSIX permits hard links on folders but does not require them. Modern file systems tend to not support it.
Other implementations
[edit]Implementations of features similar to symbolic links.
Early MIT
[edit]MIT Compatible Time-Sharing System c. 1963 and Incompatible Timesharing System both have linked files where the name of the target file is specified in a directory entry.[2][3][4]
Amiga
[edit]The command creating symbolic links is makelink
, which is also used for hard links. Internally the dos.library returns an error code indicating that a target is a soft link if you try to perform actions on it that are only legal for a file, and applications that wish to follow the symbolic link then needs to explicitly make a call to follow the link and retry the operation. The AmigaDOS shell will follow links automatically.
Mac OS
[edit]In Mac OS, applications or users can also employ aliases, which have the added feature of following the target, even if it is moved to another location on the same volume. This is not to be confused with the shell command alias.
OS/2
[edit]In the OS/2 operating system, symbolic links somewhat resemble shadows in the graphical Workplace Shell. However, shadows, due to the fully object-oriented System Object Model, are considerably more powerful and robust than a simple link. For example, shadows do not lose their capabilities when renamed or when either the object or subject of the link is relocated.[31]
Variable symbolic links
[edit]Symbolic links may be implemented in a context-dependent or variable fashion, such that the link points to varying targets depending on a configuration parameter, run-time parameter, or other instantaneous condition.
A variable or variant symbolic link is a symbolic link that has a variable name embedded in it. This allows some flexibility in filesystem order that is not possible with a standard symbolic link. Variables embedded in a symbolic link may include user and environment specific information.
Operating systems that make use of variant symbolic links include NetBSD, DragonFly BSD, Domain/OS.[32][33][5] Tru64 uses a context dependent symbolic link where the context is the cluster member number.
Pyramid Technology's OSx Operating System implemented conditional symbolic links which pointed to different locations depending on which universe a program was running in. The universes supported were AT&Ts's SysV.3 and the Berkeley Software Distribution (BSD 4.3). For example: if the ps command was run in the att universe, then the symbolic link for the directory /bin would point to /.attbin and the program /.attbin/ps would be executed. Whereas if the ps command was run in the ucb universe, then /bin would point to /.ucbbin and /.ucbbin/ps would be executed. Similar Conditional Symbolic Links were also created for other directories such as /lib, /usr/lib, /usr/include.[34]
See also
[edit]- Symlink race — a security-vulnerability caused by symbolic links
- freedup — generates links between identical data automatically
- Pointer (computer programming)
References
[edit]- ^ Pathname resolution, POSIX.
- ^ a b Walden, David; Van Vleck, Tom, eds. (2011). "Compatible Time-Sharing System (1961-1973): Fiftieth Anniversary Commemorative Overview" (PDF). IEEE Computer Society. Retrieved February 20, 2022.
As CTSS developed, we provided ways for users to share their files on disk, through "common files" and "linking,"
- ^ a b Crisman, Patricia A., ed. (December 31, 1969). "The Compatible Time-Sharing System, A Programmer's Guide" (PDF). The M.I.T Computation Center. Retrieved March 10, 2022.
U.F.D. entries that point to other U.F.D. entries instead of to the file itself
- ^ a b Corbato, F. J.; Daggett, M. M.; Daley, R. C.; Creasy, R. J.; Hellwig, J. D.; Orenstein, R. H.; Korn, L. K. (1963). "The Compatible Time-Sharing System A Programmer's Guide" (PDF). MIT. Retrieved November 29, 2022.
Link: The format is similar to Copy. The specified file is not copied
- ^ a b c Pike, Rob (2000). Lexical file names in Plan 9 or getting dot-dot right (PDF). Proc. USENIX Annual Tech. Conf.
- ^ symlink, symlinkat. IEEE Std 1003.1, 2013 Edition.
- ^ link, linkat. IEEE Std 1003.1, 2013 Edition.
- ^ Bill Joy; Sam Leffler. "Surviving with 4.1a bsd". GitHub. Retrieved 8 September 2023.
It also includes a few other features which you may find useful, such as symbolic links and an improved group scheme.
- ^ fstatat, lstat, stat - get file status IEEE Std 1003.1, 2013 Edition.
- ^ lchown - change the owner and group of a symbolic link IEEE Std 1003.1, 2013 Edition.
- ^ readlink, readlinkat - read the contents of a symbolic link IEEE Std 1003.1, 2013 Edition.
- ^ "lchmod(2)". Manual pages for FreeBSD 11.
- ^ "lchflags(2)". Manual pages for FreeBSD 11.
- ^ Ritchie, D.M.; Thompson, K. (July 1978). "The UNIX Time-Sharing System". Bell System Tech. J. 57 (6): 1905–1929. CiteSeerX 10.1.1.112.595. doi:10.1002/j.1538-7305.1978.tb02136.x.
- ^ "Link Shell Extension website". Link Shell Extension website.
- ^ Symbolic Links, MSDN Library, Win32 and COM Development, 2008-01-18
- ^ "CreateSymbolicLinkA function (winbase.h)". MSDN. June 2023.
- ^ Symbolic Link Programming Considerations, MSDN
- ^ Mark Russinovich: Inside the Windows Vista Kernel: Part 1 – File-based symbolic links, Microsoft Technet, February 2007.
- ^ "Sysinternals Junction documentation". microsoft.com. Retrieved 23 March 2018.
- ^ "Windows backup or restore errors 0x80070001, 0x81000037, or 0x80070003". support.microsoft.com.
- ^ "Distributed Link Tracking on domain controllers - Windows Server". 23 February 2023.
- ^ "Distributed Link Tracking and Object Identifiers". Microsoft Developers Network. Microsoft Corporation. 20 March 2011. Retrieved 30 June 2011.
- ^ "Specifying a Namespace Extension's Location". msdn.microsoft.com. 11 January 2008. Retrieved 23 March 2018.
- ^ "You cannot view or change the Read-only or the System attributes of folders in Windows Server 2003, in Windows XP, in Windows Vista or in Windows 7". support.microsoft.com. Retrieved 2021-07-08.
- ^ Creating Shell Extensions with Shell Instance Objects. msdn.microsoft.com
- ^ a b "Chapter 3. Using Cygwin". www.cygwin.com. Retrieved 2021-07-08.
- ^ "Using Cygwin effectively with Windows".
- ^ "Coreutils: ln --symbolic creates hard links (MSYS2-packages #249)". GitHub.
- ^ "Create symbolic links". Windows client documentation for IT Pros. Microsoft. 18 January 2023 – via Microsoft Learn.
- ^ Rojas, Miguel (16 December 2020). "Cómo ejecutar versiones de Python diferentes a las predeterminadas". manualestutor.com/. Retrieved 20 December 2020.
- ^ NetBSD Miscellaneous Information Manual: magic symlinks. –
- ^ Brooks Davis (2008). "Variant symbolic links for FreeBSD" (PDF).
- ^ Neil Brown (2016). "A case for variant symlinks". LWN.
External links
[edit]- Q & A: The difference between hard and soft links as applied to Linux
- Junction: maintain NTFS junction points (for Windows 2000 and above)
- FSUtil Hardlink: Microsoft Technet page on using the command-line tool FSUtil to create hardlinks (for Windows 2000 and above)
- Symbolic Drivers for Windows XP (in Japanese): file system drivers to enable Symbolic Links for Windows XP (also mirrored on Link Shell Extension site). Sources available.