Links let you reference one file by more than one name. Linux gives you two kinds: hard links and symbolic links (symlinks, also called soft links). Both are created with the ln command, but they work very differently. This guide focuses on the ln command syntax, real use cases, linking directories, and how to find and fix broken links on Ubuntu.
Table of Contents
The ln command syntax
The ln command creates links. Its basic form is:
ln [OPTIONS] TARGET LINK_NAME
ln target linkcreates a hard link namedlinkpointing totarget.ln -s target linkcreates a symbolic link namedlinkpointing totarget.
TARGET is the existing file; LINK_NAME is the new name you’re creating.
Hard links
A hard link is an additional name for an existing file’s inode—the on-disk structure that holds the file’s data and metadata. Two hard links to the same file are indistinguishable equals: they share the same inode number and the same data. (For background on inodes, see how to get a file’s inode number in Linux.)
Create a hard link
echo "Ubuntu Linux" > file1.txt
ln file1.txt file2.txt
ls -li file1.txt file2.txt
2621501 -rw-r--r-- 2 user user 13 Sep 6 11:00 file1.txt
2621501 -rw-r--r-- 2 user user 13 Sep 6 11:00 file2.txt
Both names share inode 2621501, and the link count (the number after permissions) is 2. Delete either name and the data remains until the count reaches zero.
Find all hard links to a file
Because hard links share an inode, you can find every name that points to it. Use find with -samefile (or -inum):
find . -samefile file1.txt
./file1.txt
./file2.txt
Limits of hard links
- They cannot cross filesystems (an inode number is only meaningful within one filesystem).
- You normally cannot hard-link a directory—this is disallowed to prevent loops in the filesystem tree.
Symbolic links
A symbolic link is a small, separate file that stores the path to another file. It has its own inode and works like a shortcut. If the target moves or is deleted, the symlink is left dangling.
Create a symbolic link
ln -s file1.txt link1.txt
ls -li file1.txt link1.txt
2621501 -rw-r--r-- 2 user user 13 Sep 6 11:00 file1.txt
2621502 lrwxrwxrwx 1 user user 9 Sep 6 11:05 link1.txt -> file1.txt
Here the symlink has a different inode (2621502), a file type of l, and an arrow (->) showing its target. Confirm where it points with readlink:
readlink link1.txt
file1.txt
Symbolic links can point to directories
Unlike hard links, symlinks can reference directories—one of their most common uses:
mkdir -p /opt/app/releases/v2.0
ln -s /opt/app/releases/v2.0 /opt/app/current
ls -l /opt/app/current
lrwxrwxrwx 1 root root 24 Sep 6 11:10 /opt/app/current -> /opt/app/releases/v2.0
This “current” pattern lets you switch versions by repointing a single symlink.
See also: Mastering the Linux Command Line — Your Complete Free Training Guide
Absolute vs relative symlinks
A symlink can store either an absolute path or a relative one. Relative links stay valid if you move the whole directory tree together:
ln -s ../shared/config.yml config.yml # relative
ln -s /etc/app/config.yml config.yml # absolute
Use readlink -f to resolve a symlink to its final absolute path:
readlink -f config.yml
Detecting and fixing broken symbolic links
If a target is removed or moved, the symlink becomes “broken” (dangling). Accessing it fails:
rm file1.txt
cat link1.txt
cat: link1.txt: No such file or directory
Find broken symlinks under a directory with find -xtype l:
find . -xtype l
./link1.txt
To fix one, repoint it at a valid target with ln -sf (force overwrite):
ln -sf /path/to/new_target link1.txt
To remove a broken link, delete it like any file (see how to delete files in Linux):
rm link1.txt
Hard link vs symbolic link
| Feature | Hard link | Symbolic link |
|---|---|---|
| What it references | The file’s inode/data | The file’s path |
| Inode | Same as target | Its own inode |
| Across filesystems | No | Yes |
| Link to a directory | No | Yes |
| If target is deleted | Still works | Breaks (dangling) |
| Created with | ln target link | ln -s target link |
ls -l type | - | l |
When to use each
- Hard link — when you want two equivalent names for the same data on the same filesystem, and you want the data to survive removal of either name.
- Symbolic link — when you need to cross filesystems, link a directory, or create a clearly labeled shortcut (like
/opt/app/current) that you can repoint later.
Summary
Both hard links and symbolic links are made with ln, but a hard link is another name for the same inode, while a symbolic link is a separate file pointing to a path. Use ls -li to compare inodes and link counts, readlink to see a symlink’s target, find -samefile to list hard links, and find -xtype l to hunt down broken symlinks. To review the ls output fields, see understanding the ls command in Linux, and for a step-by-step comparison, see hard links and soft links with detailed examples.


