Learn by reading through in order

Symbolic and Hard Links — ln

Create a symbolic link that stores a path with ln -s target.txt link.txt and confirm the -> in ls -l, then make a hard link to the same data body with ln base.txt hard.txt; see the difference with diagrams and a browser terminal.

Symbolic Links — ln -s

A link lets you reach the same file under a different name. You create a symbolic link with ln -s target name, and it is a special file that stores the path (location) of the target. When you open name, it automatically follows the stored path to the target. In ls -l, the link's row shows where it points, as name -> target.

SyntaxEffect
ln -s target name-s (symbolic) creates a symbolic link that is a path-based alias
ls -lCheck where it points via the name -> target display
ln target aliasCreates a hard link pointing to the same data body
Symbolic vs hardSymbolic = a special file storing the target's path / Hard = an alias to the same data body
When the target is deletedSymbolic = loses its target and breaks / Hard = the contents remain
How a Symbolic Link Works — ln -s
target.txtcontent: "data"link.txt (special file)stores path "target.txt"outputlink.txt -> target.txtoutput: "data"follows path to real fileln -s target.txt link.txtls -lcat link.txt
ln -s creates link.txt, a special file that stores the path target.txt. ls -l shows it as link.txt -> target.txt, and cat link.txt follows that path to read the contents of the real file target.txt.
printf 'data\n' > target.txt   # Create the target file
ln -s target.txt link.txt     # Create a symbolic link
ls -l                         # Check link.txt -> target.txt
cat link.txt                  # Shows the contents of target.txt

① Create the target file with printf 'data\n' > target.txt.

② Add the option that makes ln create a symbolic link, then pass the target and the new name in that order to create link.txt.

③ Run ls -l and check that the link target appears as link.txt -> target.txt. (When you run it correctly, an explanation appears.)

Linux console
0 / 3 completed
Loading Linux Terminal...

Hard Links — ln

Without -s, ln target alias creates a hard link. While a symbolic link is a special file that stores a path to the target, a hard link is another name pointing to the same data body. Opening either name gives identical contents, and in ls -l a hard link shows no arrow — it looks like an ordinary file. Keep the distinction in mind: symbolic = a special file storing the target's path, hard = an alias to the same data body.

How Symbolic and Hard Links Differ
base.txtdata body "shared"symcontent = path "base.txt"hardalias to same data bodyoutput: "shared"read via the pathoutput: "shared"read data body directlyln -s base.txt symln base.txt hardcat symcat hardboth read "shared"
ln -s creates sym, which stores a path to the real file, while ln creates hard, an alias to the same data body. Both print shared with cat, but sym reads via the path and hard reads the data body directly.
printf 'shared\n' > base.txt   # Create the target file
ln base.txt hard.txt          # Create a hard link
ls -l                         # hard.txt is listed without an arrow
cat hard.txt                  # Shows the same contents as base.txt

① Create the target file with printf 'shared\n' > base.txt.

② Use ln with no option, passing the target and the alias in that order, to create a hard link called hard.txt.

③ Run ls -l and check that hard.txt is listed like an ordinary file, with no arrow (->).

Linux console
0 / 3 completed
Loading Linux Terminal...

What Happens When You Delete the Target

Because a symbolic link is a special file that stores a path to the target, it loses what it points to once the target is gone. Because a hard link is another name pointing to the same data body, the contents remain even after the original name is gone. Delete the target and see the difference for yourself.

What Happens to Each Link When the Target Is Deleted
orig.txtdata body "v"sl.txtcontent = path "orig.txt"hl.txtalias to same data bodyfails: dangling linkNo such fileprints "v"data body remainsln -s orig.txt sl.txtln orig.txt hl.txtcat sl.txtcat hl.txtafter rm orig.txt
After rm orig.txt removes the real file, sl.txt, which only stores a path, becomes a dangling link, while hl.txt, which shares the data body, still prints its contents with cat.
printf 'v\n' > orig.txt       # Create the target file
ln -s orig.txt sl.txt         # Symbolic link
ln orig.txt hl.txt            # Hard link
rm orig.txt                   # Delete the target
cat sl.txt                    # Loses its target and fails
cat hl.txt                    # Contents remain and can be shown

① Create the target with printf 'v\n' > orig.txt.

② Create a symbolic link with ln -s orig.txt sl.txt and a hard link with ln orig.txt hl.txt.

③ Delete the target with rm orig.txt.

④ Run cat sl.txt and cat hl.txt and compare the difference in results.

⑤ Run ls -l and check how the two links look in ls -l after the deletion.

Linux console
0 / 7 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1How is it displayed in ls -l after you run ln -s target.txt link.txt?

Q2Which of the following correctly describes the difference between symbolic and hard links?

Q3After you delete the target file with rm, for which one does cat still succeed?