Q1How is it displayed in ls -l after you run ln -s target.txt link.txt?
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.
| Syntax | Effect |
|---|---|
ln -s target name | -s (symbolic) creates a symbolic link that is a path-based alias |
ls -l | Check where it points via the name -> target display |
ln target alias | Creates a hard link pointing to the same data body |
| Symbolic vs hard | Symbolic = a special file storing the target's path / Hard = an alias to the same data body |
| When the target is deleted | Symbolic = loses its target and breaks / Hard = the contents remain |
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
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.
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
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.
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
Knowledge Check
Answer each question one by one.
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?