Software & OS

Symbolic Link Explained: Create and Remove Symlinks on Any OS

A symbolic link (symlink) is a small file that contains a path to another file or folder, and programs that open it are sent on to that target. Create one with ln -s target linkname on Linux and macOS, or mklink linkname target on Windows, and note that the order of the two names is reversed between them.

How a symbolic link works

A symlink holds nothing but a pathname. When a program opens it, the operating system reads that path and opens whatever is there instead. The man page puts it plainly: symbolic links "are files that act as pointers to other files". Because the link only stores text, it can point to a folder, to a file on another disk, or to something that does not exist at all.

That last case has a name: a link whose target is missing is a dangling (broken) link. The link itself stays in place; anything that tries to open it gets "No such file or directory".

Create a symlink on Linux and macOS

ln -s target.txt link.txt
ln -s projects/app app

The first argument is the target, the second is the new link's name. Without -s, ln makes a hard link instead. Here is the result of the two commands above plus a broken link, run in a scratch folder on Ubuntu 22.04. Example output:

$ ln -s missing.txt broken.txt
$ ls -l
lrwxrwxrwx 1 alex alex   12 Oct 26 19:30 app -> projects/app
lrwxrwxrwx 1 alex alex   11 Oct 26 19:30 broken.txt -> missing.txt
lrwxrwxrwx 1 alex alex   10 Oct 26 19:30 link.txt -> target.txt
drwxr-xr-x 3 alex alex 4096 Oct 26 19:30 projects
-rw-r--r-- 1 alex alex    6 Oct 26 19:30 target.txt

The leading l marks a link and the arrow shows where it points. The size column is the length of the stored path: target.txt is 10 characters, so link.txt is 10 bytes.

A relative target is resolved from the folder the link lives in, not from where you ran the command. ln -s ../config/app.conf /etc/app.conf points to /config/app.conf, which is rarely what you meant. Use an absolute target, or GNU ln -sr, which works out the correct relative path for you.

Useful ln options

OptionWhat it does
-sMake a symbolic link instead of a hard link
-fRemove an existing file with the link's name first
-nIf the link name is already a symlink to a folder, replace the link instead of creating a new link inside that folder
-rWith -s, store the target relative to the link's location (GNU coreutils)

To repoint an existing link to a folder, combine them: ln -sfn /opt/app-2.4 /opt/app-current. Without -n, ln follows the old link and drops a new link inside the old target folder.

Read where a symlink points

readlink link.txt
readlink -f app

Example output:

target.txt
/home/alex/demo/projects/app

readlink prints the stored text exactly. readlink -f resolves every link in the chain and prints the final absolute path. stat link.txt also says "symbolic link" on its first lines.

Find broken symlinks

find . -xtype l

With GNU find, -xtype l matches links whose target cannot be reached. In the demo folder it printed ./broken.txt. Renaming or moving a target breaks every symlink that points to it: after mv target.txt moved.txt, cat link.txt returned No such file or directory.

Symbolic link vs hard link

A hard link is a second name for the same file data (the same inode), not a pointer. In the demo, ln target.txt hard.txt produced a file with the same inode number as target.txt and a link count of 2. When the original was renamed, cat hard.txt still printed hello, while the symlink broke.

Symbolic linkHard link
StoresA path (text)A reference to the same inode
Can point to a folderYesNo
Can cross file systems or drivesYesNo
Target renamed or deletedLink breaksData still reachable through the hard link
Own permissionsAlways 0777, ignoredShares the file's permissions

Create a symlink on Windows

In Command Prompt, the link name comes first and the target second, the reverse of ln:

mklink link.txt target.txt
mklink /d linkdir C:\work\realdir
mklink /j junc C:\work\realdir

/d makes a directory symlink (the default is a file symlink), /h makes a hard link, and /j makes a directory junction. mklink is built into cmd.exe, so in PowerShell use cmd /c mklink ... or the native cmdlet:

New-Item -ItemType SymbolicLink -Path link.txt -Target target.txt

"You do not have sufficient privilege to perform this operation"

This is the exact message mklink prints in a normal, non-elevated prompt. New-Item fails with Administrator privilege required for this operation. Both come from the same rule: the Create symbolic links user right (SeCreateSymbolicLinkPrivilege) belongs only to Administrators by default. You have three ways round it:

  • Run the terminal as administrator.
  • Turn on Developer Mode in Windows Settings. Since Windows 10 build 14972, symlinks can then be created without elevation.
  • For folders on the same machine, use mklink /j. A junction needs no special right; in the test above the junction succeeded in the same prompt where both symlink commands failed.

To check what a link points to in PowerShell:

Get-Item junc | Select-Object Name, LinkType, Target

Example output:

Name     : junc
LinkType : Junction
Target   : {C:\work\realdir}

How to remove a symbolic link safely

Deleting a link never deletes the target. unlink and rm act on the link itself. The trap is how you name it:

  • Linux and macOS: rm app or unlink app. Do not add a trailing slash. On Ubuntu 22.04, rm -r app/ deleted every file inside the real folder and then printed rm: cannot remove 'app/': Not a directory, leaving the link in place and the target empty.
  • Windows: remove a file symlink with del link.txt and a directory symlink or junction with rd linkdir. rd without /s removes only the link.

Common uses

  • Switching versions. Point /opt/app-current at a release folder and repoint it on upgrade with ln -sfn.
  • Dotfiles. Keep config files in one Git repository and symlink them into your home folder.
  • Moving big folders. Move a cache to another drive and leave a link at the old path so programs still find it.

Programs differ in whether they follow links. Backup and sync tools often have a setting for it, and copying a symlinked folder may copy the link or the whole target depending on the tool. If a copy is unexpectedly small or unexpectedly huge, check that first. File permissions on the target still apply in full, as covered in Linux file permissions explained.

Frequently asked questions

Does deleting a symbolic link delete the original file?
No. Removing a symbolic link with rm, unlink, del or rd removes only the link. The file or folder it points to is untouched. The one trap is a trailing slash on Linux: rm -r link/ follows the link and deletes the target folder's contents, so remove a directory link with rm link or unlink link, without the slash.
Why do symlinks show lrwxrwxrwx permissions?
On Linux, a symbolic link's own permissions are always 0777 and cannot be changed. They are ignored. What decides access is the permission on the target file, which the system checks when it follows the link.
Can a symbolic link point to a file on another drive?
Yes. A symbolic link stores a path as text, so it can point to another file system, another drive or a network path. A hard link cannot, because it refers to an inode number that is only unique within one file system.
Why does mklink say I do not have sufficient privilege?
On Windows, creating symbolic links needs the Create symbolic links user right, which only Administrators have by default. Run the command from an elevated prompt, turn on Developer Mode, or use mklink /j to make a directory junction, which does not need the right.

Sources