skip to content
Skesov.com

Symbolic and Hard Links in Linux and macOS: A Comprehensive Guide

/ 2 min read

Table of Contents

In UNIX-like systems (Linux, macOS), links are a fundamental part of the file system. They allow you to manage data efficiently, create aliases for libraries, or organize configurations.

There are two main types of links: Symbolic (Soft) and Hard. To understand the difference, we must first understand how the system stores files.

What is an Inode?

Imagine your hard drive is a massive warehouse filled with boxes of data. Each box has a unique identification number — this is the Inode.

What you see in your folders (files) are just entries in the system’s “index book”: File Name -> Box Number.

  • A Hard Link is when you create two different entries in the index book with different names but pointing to the same box number.
  • A Symbolic Link is when you create an entry that points not to a box, but to another entry in the index book.

A symbolic link is a standalone file that contains the path to the target object.

Characteristics:

  • Can point to both files and folders.
  • Work across different drives and file systems.
  • If the original is deleted, the link “breaks” (becomes orphaned).

Creation:

Terminal window
ln -s /path/to/original /path/to/link

If you need to point an existing link (especially one pointing to a folder) to a new path, use the -sfn combination:

Terminal window
ln -sfn /new/path/target /path/to/link
  • -f (force) — removes the old link before creating the new one.
  • -n (no-dereference) — ensures that if the link points to a folder, the new link isn’t created inside that folder.

A hard link is essentially a second name for physical data on the disk.

Characteristics:

  • Only work within the same partition.
  • Cannot be created for folders.
  • If the original is deleted, the data remains accessible via the hard link.

Creation:

Terminal window
ln /path/to/original /path/to/link

Comparison Table

PropertySymbolic Link (-s)Hard Link
For FoldersYesNo
Across PartitionsYesNo
Delete OriginalLink breaksFile remains accessible
Disk SizeA few bytes0 (points to the same Inode)

Useful Commands

Terminal window
ls -l /path/to/link
# Output: lrwxr-xr-x ... link -> /path/to/original

Use the standard rm command. This is safe — the original data will not be affected.

Terminal window
rm /path/to/link

Summary

Use symbolic links (ln -s) in 99% of cases — it is the standard for configurations and aliases. Hard links are useful for backup mechanisms or when you need to ensure data persistence even if one of the filenames is deleted.