Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Working with Files  ›  Capstone Project

Soft Links and Hard Links

Soft Links / Hard Links 15 minA hard link is a second name for the same inode that keeps the data alive, while a soft link stores a path and breaks if that path disappears.

Soft Links and Hard Links

Asha's latest harvest file sits deep inside ~/garden/seasons/2026/, and she's tired of typing that long path. She wants a short name in her home folder that always leads to it. Linux has two kinds of link for this, and they behave very differently when the original file is moved or deleted.

The rule

A hard link is another name; a soft link is a signpost.

To see why, you need one fact: a file's data and its name are stored separately. The data and its details (size, owner, permissions) live in a record called an inode. A directory entry is just a name that points to an inode.

Hard link: a second name for the same inode.

$ cd ~/garden/seasons/2026
$ ln harvest.csv harvest-hard.csv
$ ls -li harvest.csv harvest-hard.csv
1048601 -rw-r--r-- 2 asha garden 181 Jun  4 harvest.csv
1048601 -rw-r--r-- 2 asha garden 181 Jun  4 harvest-hard.csv
  • Both names show the same inode number (1048601), and the link count is 2.
  • Neither name is the "original". Delete one and the data stays, reachable through the other. The data is only freed when the last name is gone.
  • Limits: a hard link can't point to a directory, and it can't cross to a different filesystem (another disk or partition).

Soft (symbolic) link: a tiny separate file that stores a path.

$ ln -s ~/garden/seasons/2026/harvest.csv ~/harvest
$ ls -l ~/harvest
lrwxrwxrwx 1 asha asha 42 Jun  4 harvest -> /home/asha/garden/seasons/2026/harvest.csv
  • The type letter is l, and the arrow shows where it points.
  • It can point to directories and to other filesystems.
  • If the target is moved or deleted, the link is left broken (dangling). It still points to a path that no longer exists.

The argument order is the same as cp: target first, then the new link name.

The shortcut

Ask "what should happen if the original disappears?" If the data should survive, use a hard link. If the link should follow a path, even across disks or to a folder, use a soft link. Most day-to-day links are soft.

Asha deletes the originalStep 1 of 4
  1. Set up

    In ~/garden/seasons/2026, harvest-hard.csv is a hard link to harvest.csv, and ~/harvest is a soft link to the same file.

A sneaky trap: a relative soft link path is worked out from the link's own folder, not from where you were when you made it. Run from ~/garden/seasons/2026, ln -s harvest.csv ~/h creates ~/h pointing to ~/harvest.csv, which probably doesn't exist. When in doubt, give an absolute target.

Drills

Answer fast — trust the rule.

quick drill

Which command makes a symbolic link called latest that points to harvest.csv?

quick drill

Two names show the same inode number in ls -li. What are they?

quick drill

Asha deletes a file that had a soft link pointing to it. What happens to the link?

quick drill

Which of these can a hard link NOT do?

Remember

  • A hard link is another name for the same inode; a soft link is a signpost that stores a path.
  • Ask what should happen if the original disappears. Hard links keep the data, soft links break.
  • Target first, then link name (ln -s target name), and use absolute targets when in doubt.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhich link?

Explain the difference between a hard link and a soft link using inodes, say what happens to each when the original file is deleted, and say which one Asha should use to create a shortcut ~/harvest to a file in another folder.