How to Use the ln Command to Create Hard and Symbolic Links in Linux

Organising file hierarchies, managing shared library versions, and referencing configuration files across different directory paths in Linux often requires pointing to a single file from multiple locations. Duplicating files by copying them wastes precious disk space and creates divergent, out-of-sync copies whenever one file is edited. The Linux filesystem addresses this with filesystem links managed through the ln (link) command. Understanding the fundamental architectural difference between Symbolic Links (Soft Links) and Hard Links allows administrators and developers to structure complex filesystems cleanly and reliably.

Hard Links vs. Symbolic Links: The Inode Architecture

In Linux filesystems (such as ext4, XFS, and Btrfs), a file’s metadata and data block pointers are stored in a data structure called an inode. A filename is merely an entry in a directory table pointing to that inode number.

The two linking types interact with inodes differently:

  • Hard Link: Creates an additional directory entry pointing directly to the identical inode number as the original file. A hard link is not a pointer to another filename; it is a direct equal peer to the data. If the original filename is deleted, the file data remains fully intact and accessible via the hard link until all linked names are unlinked. (Hard links cannot span across different filesystem partitions and cannot point to directories).
  • Symbolic Link (Soft Link / Symlink): Creates a brand-new, independent file with its own unique inode that contains the text path pointing to the target file or directory. Symlinks function like desktop shortcuts: they can span across different drive partitions and link to directories. However, if the target file is deleted or moved, the symlink breaks, becoming a dangling or orphaned link.

Creating Symbolic Links with the ln -s Command

Symbolic links are by far the most commonly used links in Linux systems administration (e.g., managing Nginx site configurations or creating CLI binary shortcuts).

The standard syntax structure is:

ln -s /path/to/original /path/to/symlink

For example, to link a configuration file located in your project directory into the system Nginx directory:

sudo ln -s /var/www/myproject/nginx.conf /etc/nginx/sites-enabled/myproject.conf

To create a symlink to an entire directory:

ln -s /mnt/storage/shared_media ~/media

Creating Hard Links

To create a hard link, invoke ln without the -s flag:

ln original_data.csv backup_data.csv

To verify that both filenames share the exact same underlying inode, run ls -li:

ls -li original_data.csv backup_data.csv

Output:

1442018 -rw-r--r-- 2 user user 1024 Apr 15 14:00 backup_data.csv
1442018 -rw-r--r-- 2 user user 1024 Apr 15 14:00 original_data.csv

Notice that both files share the identical inode number (1442018) and the link count column displays 2.

Relative vs. Absolute Symlinks: Avoiding Broken Links

When creating symbolic links, you can specify target paths as either absolute or relative:

  • Absolute Path: ln -s /var/log/app.log ~/app.log. The symlink will resolve correctly from any directory, but will break if the parent directory tree is relocated.
  • Relative Path: When using relative paths, the target path must be relative to the symlink’s destination directory, not your current terminal location. For example, if you are inside /etc/nginx/sites-enabled, run:
    ln -s ../sites-available/default default

Hard Links vs. Symbolic Links Comparison

Feature Hard Link (ln) Symbolic Link (ln -s)
Inode Assignment Shares identical inode with target Receives fresh unique inode
Target Deletion Behavior Data persists completely intact Link breaks (dangling symlink)
Spans Partitions / Disks No (same filesystem strictly) Yes (cross-partition and network mounts)
Can Link Directories No (prohibited to prevent loops) Yes (full directory linking)
Visual File Size Displays full size of data Displays character length of target path

Overwriting, Modifying, and Removing Links Safely

To manage links during deployments and refactoring:

  • Force Overwrite Existing Link: Use the -f flag to update an existing symlink to point to a new target without deleting it first:
    ln -sf /new/path/to/binary ~/bin/mytool
  • Removing a Symlink: Delete a symlink using standard rm or unlink. Crucially, do not include a trailing slash when removing a symlink to a directory:
    rm ~/media       # Correct: deletes symlink only
    rm -rf ~/media/  # DANGEROUS: can delete target directory contents!

Frequently Asked Questions

How do I find all broken symbolic links in a directory?

Run the find command with the -xtype l parameter: find . -xtype l. This scans the directory tree and outputs all broken or dangling symlinks whose target files no longer exist.

Does editing a symlink modify the original file?

Yes. Opening a symlink in a text editor like nano or writing to it via a shell redirect (>>) modifies the target file directly. The symlink is merely an address pointer.

Why are hard links forbidden on directories?

Allowing hard links to directories would break the hierarchical tree structure of the Unix filesystem, creating recursive infinite loops that could crash filesystem traversal utilities like find, du, and fsck.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.