symlinks
100%

The Filesystem · Lesson 12

symlinks

Learn how symbolic and hard links differ in pathname resolution, inode identity, and filesystem scope.

A directory entry gives an inode a name. A hard link creates another directory entry for the same inode, while a symbolic link creates a different inode whose content is a pathname to resolve. This difference controls identity, lifetime, and cross-filesystem behavior.

Creating and Inspecting a Symbolic Link

Create a symlink with ln -s TARGET LINK_NAME:

$ printf '%s\n' 'example' > myfile
$ ln -s -- myfile myfilelink
$ ls -li myfile myfilelink
151   -rw-r--r-- 1 user user 8 ... myfile
93403 lrwxrwxrwx 1 user user 6 ... myfilelink -> myfile

The symlink has its own inode and stores the text myfile. When a program follows myfilelink, pathname resolution continues to the target. Display the stored text without following it with:

$ readlink myfilelink

Which command creates symbolic link myfilelink with target text myfile?

Relative and Absolute Symlink Targets

An absolute target starts at /. A relative target is resolved relative to the directory containing the symlink—not relative to the shell's current directory at the moment someone later opens it.

$ mkdir -p tree/data tree/current
$ printf '%s\n' 'value' > tree/data/item
$ ln -s ../data/item tree/current/item

Moving the entire tree hierarchy preserves this relative relationship. Moving only the link or target can break it. A symlink is allowed to contain a nonexistent target and is then called dangling or broken.

From where is a relative symlink target resolved?

Creating a Hard Link

Create another name for an existing regular file without -s:

$ ln -- myfile myhardlink
$ ls -li myfile myhardlink
151 -rw-r--r-- 2 user user 8 ... myfile
151 -rw-r--r-- 2 user user 8 ... myhardlink

Both names map to the same filesystem and inode number. The link count becomes 2. Neither name is inherently the “original”; changing content through one name changes the shared object, and removing one name leaves the other.

Hard links cannot cross filesystem boundaries because an inode number is meaningful only within its filesystem. Linux also restricts ordinary users from hard-linking directories and can restrict links to files they do not own, preventing cycles and security problems.

What do two hard links to one regular file share?

Lifetime and Deletion

Removing a symlink removes that link object, not its target:

$ rm -- myfilelink

Removing a hard-link name decrements the shared inode's link count. The filesystem can reclaim the object only after the count reaches zero and no open file descriptions or other filesystem references keep it alive.

Avoid a trailing slash when removing a symlink to a directory, because trailing-slash path resolution can follow directory semantics depending on the command. Inspect with ls -ld -- LINK and remove the link name deliberately.

What normally happens when you remove a symlink itself?

Following Links Safely

Symlinks can redirect a privileged program outside an expected directory or change between validation and use. Secure programs should avoid check-then-open pathname races and use directory-relative, no-follow, or constrained-resolution interfaces appropriate to their language and operating system.

For routine inspection:

  • ls -ld LINK shows the link itself.
  • readlink LINK prints its stored target text.
  • stat LINK commonly reports link metadata, while stat -L LINK follows it in GNU coreutils.
  • find -L follows links and can encounter cycles; use it only intentionally.

Permissions displayed as lrwxrwxrwx are not a general access grant. Access is decided through directory traversal, link-following policy, and target permissions; symlink ownership also matters for some protected-directory rules.

What does readlink LINK print by default?

Lesson complete

You finished symlinks

You can now choose and inspect the correct kind of filesystem link.

  • Use ln -s TARGET LINK for a pathname-based symbolic link.

  • Resolve relative targets from the link's containing directory.

  • Use ln EXISTING LINK for another same-filesystem inode name.

  • Distinguish unlinking a symlink from unlinking a hard link.

  • Avoid unsafe link following in privileged or recursive operations.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Back to The Filesystem