symbolic link

{{Short description|Any file that contains a reference to another file or directory}}

{{for|the Microsoft data exchange format|Symbolic Link (SYLK)}}

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.[https://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11 Pathname resolution], POSIX.

Symbolic links are supported by POSIX and by most Unix-like operating systems, such as FreeBSD, Linux, and macOS. Support also exists in Windows 10 and 11.{{cite web|url=https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/mklink |title=Windows mklink Command}} CTSS on IBM 7090 had files linked by name in 1963.{{cite web |url=https://multicians.org/thvv/compatible-time-sharing-system.pdf |title=Compatible Time-Sharing System (1961-1973): Fiftieth Anniversary Commemorative Overview |editor-last1=Walden |editor-first1=David |editor-last2=Van Vleck |editor-first2=Tom |editor2-link=Tom Van Vleck |date=2011 |publisher=IEEE Computer Society |access-date=February 20, 2022 |quote=As CTSS developed, we provided ways for users to share their files on disk, through “common files” and “linking,”}}{{cite web |url=http://www.bitsavers.org/pdf/mit/ctss/CTSS_ProgrammersGuide_Dec69.pdf |title=The Compatible Time-Sharing System, A Programmer's Guide |editor-last=Crisman |editor-first=Patricia A. |date=December 31, 1969 |publisher=The M.I.T Computation Center |access-date=March 10, 2022 |quote=U.F.D. entries that point to other U.F.D. entries instead of to the file itself}}{{cite web |url=https://www.ibiblio.org/apollo/Documents/CTSS_ProgrammersGuide.pdf |title=The Compatible Time-Sharing System A Programmer's Guide |first1=F. J. |last1=Corbato |authorlink1=Fernando J. Corbató |first2=M. M. |last2=Daggett |first3=R. C. |last3=Daley |first4=R. J. |last4=Creasy |first5=J. D. |last5=Hellwig |first6=R. H. |last6=Orenstein |first7=L. K. |last7=Korn |date=1963 |publisher=MIT |access-date=November 29, 2022 |quote=Link: The format is similar to Copy. The specified file is not copied}} By 1978 minicomputer operating systems from DEC, and in Data General's RDOS included symbolic links.

Overview

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.{{cite conference |first=Rob |last=Pike |author-link=Rob Pike |title=Lexical file names in Plan 9 or getting dot-dot right |conference=Proc. USENIX Annual Tech. Conf. |year=2000 |url=https://static.usenix.org/events/usenix2000/general/full_papers/pikelex/pikelex.pdf}}

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

In POSIX-compliant operating systems, symbolic links are created with the symlink[https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html symlink, symlinkat]. IEEE Std 1003.1, 2013 Edition. system call. The ln shell command normally uses the link[https://pubs.opengroup.org/onlinepubs/9699919799/functions/link.html link, linkat]. IEEE Std 1003.1, 2013 Edition. 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.{{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.}}

The following command creates a symbolic link at the command-line interface (shell):

ln -s target_path link_path

{{mono|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. {{mono|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,[https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatat.html fstatat, lstat, stat - get file status] IEEE Std 1003.1, 2013 Edition. lchown[https://pubs.opengroup.org/onlinepubs/9699919799/functions/lchown.html lchown - change the owner and group of a symbolic link] IEEE Std 1003.1, 2013 Edition. and readlink[https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html readlink, readlinkat - read the contents of a symbolic link] IEEE Std 1003.1, 2013 Edition. 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

=Error handling=

A traditional Unix filesystem has a tree structure,{{cite journal |last1=Ritchie |first1=D.M. |author-link1=Dennis Ritchie |last2=Thompson |first2=K. |author-link2=Ken Thompson |title=The UNIX Time-Sharing System |journal=Bell System Tech. J. |volume=57 |issue=6 |pages=1905–1929 |date= July 1978 |doi=10.1002/j.1538-7305.1978.tb02136.x |citeseerx=10.1.1.112.595}} however symbolic links allow it to contain loops.

Microsoft Windows

== 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.

:

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=

{{Main|NTFS junction point}}

The Windows 2000 introduced NTFS reparse points, which enabled the use of NTFS volume mount points and junction points. Junction points are soft links to machine-local directories (junction points to remote shares are unsupported). The Windows 2000 and XP Resource Kits include a program called linkd.exe to create junction points. A more powerful one named Junction.exe is distributed as a part of Microsoft Sysinternals Suite.{{Cite web |url=https://learn.microsoft.com/en-us/sysinternals/downloads/junction#junction-v107 |title=Junction v1.07 |work=Microsoft Sysinternals |via=Microsoft Learn |publisher=Microsoft |author-first=Mark |author-last=Russinovich |author-link=Mark Russinovich |date=4 July 2016}} The tools introduced above (mklink and New-Item) also support creating junction points.

Not all standard applications support reparse points. Most noticeably, Windows Backup suffers from this problem and will issue an error message 0x80070003 when the folders to be backed up contain a reparse point.{{Cite web |url=https://support.microsoft.com/kb/973455 |title=Windows backup or restore errors 0x80070001, 0x81000037, or 0x80070003 |work=Microsoft Support |publisher=Microsoft |date=17 April 2018}}

=Shortcuts=

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{{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 }} 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.{{cite web

|title=Distributed Link Tracking and Object Identifiers

|url=https://msdn.microsoft.com/en-us/library/aa363997%28v=VS.85%29.aspx

|work=Microsoft Developers Network

|publisher=Microsoft Corporation

|access-date=30 June 2011

|date=20 March 2011

}} Additionally, in Windows 9x and later, Windows shell tries to find the target of a broken shortcut before proposing to delete it.

==Folder shortcuts==

Almost like shortcuts, but transparent to the Windows shell.{{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}} They are implemented as ordinary folders (which need to have the read only and/or system attribute{{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}}) 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=

The shell objects[https://msdn.microsoft.com/library/ms997573.aspx Creating Shell Extensions with Shell Instance Objects]. msdn.microsoft.com 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.

Other implementations

Implementations of features similar to symbolic links.

=Early MIT=

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.

=Data General RDOS=

Data General's RDOS for its Nova computers supports "link entries", which are directory entries that contain both the name of the entry and the name of another file, so that a reference to a file using the name of the entry refers to the other file.{{cite book |url=http://www.bitsavers.org/pdf/dg/software/rdos/093-000075-08_RDOS_Reference_Manual_Mar79.pdf |title=Real Time Disk Operating System (RDOS) Reference Manual |pages=2{{hyp}}11-2{{hyp}}12 |publisher=Data General |date=March 1979}}

=Amiga=

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=

{{Main|Alias (Mac OS)}}

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=

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.{{cite news |last1=Rojas |first1=Miguel |title=Cómo ejecutar versiones de Python diferentes a las predeterminadas |url=https://manualestutor.com/desarrollador-de-ios/como-ejecutar-versiones-de-python-diferentes-a-las-predeterminadas/ |newspaper=Manualestutor |date=16 December 2020 |access-date=20 December 2020}}

See also

References

{{Reflist|2}}