Files, hard links and symbolic links
Video 104 of 140 in Virtualization Foundations, in the unit “The shell and the filesystem tree”. 3:04, with English and Hindi audio.
Two names can point at the same file, and one kind of name keeps working after the original is removed while the other does not. That difference matters when you manage servers, because a link is often the thing that lets an upgrade happen safely. This article explains what a file is made of on Linux, the two kinds of link Linux offers, and how to test both in a practice folder.
What you will learn
- A file is data stored on disk, plus a name in a folder that points to that data.
- A hard link is a second name for the same data, and it keeps working when the original name is removed.
- A symbolic link holds only a path, so it breaks when its target is removed.
- A hard link must stay on the same filesystem, and Linux does not allow a hard link to a folder.
- A link can let a team switch to a new version and keep the old folder until the new one works.
What a file is made of on Linux
A file has two parts. The data is the bytes stored on disk, and the contents of the file live there. The name is a label in a folder that points to that data. A folder is simply a list of names, and each name points to some data on the disk.
Because a name is only a label, one set of data can have more than one name. That is the idea behind both kinds of link.
Hard link against symbolic link
- A hard link is an extra name for the same data on disk. It keeps working when the original name goes. It works only on the same filesystem, and Linux does not allow a hard link to a folder, for any user.
- A symbolic link is a small file that holds a path, written as text. It can point to any path, on any disk, and folders are allowed. When its target goes, it breaks and points at nothing.
Note: A symbolic link never holds the data. Removing the target leaves the link in place, but it now leads nowhere.
Testing both links in a practice folder
The practice folder below shows the difference. First, a hard link is made to a file called report.txt. Then a symbolic link is made to the same file. The original name is removed last.
ln report.txt hard.txtln -s report.txt soft.txtAfter the original name is removed, reading the hard link still shows the text, because both names lead to the same data. Reading the symbolic link fails, because it held only a path.
Why links help during an upgrade
A monitoring stack on a Harbor Point Logistics server reads its settings through one fixed name. Harbor Point is a fictional logistics firm, used for training examples. That fixed name is a link to the folder of the version now in use.
To upgrade, the team points the link at the new folder. The old folder stays on disk, so the team can switch back if the new version fails.
Try it yourself
You need a Linux terminal and a practice folder where you can create and remove files. Run the lines in order, and compare what the last two commands print.
Make a practice folder, so the test files stay in one place.
mkdir demoMove into the practice folder, so the next files are created there.
cd demoWrite one line into a new file called report.txt.
echo report > report.txtMake a hard link, which is a second name for the same data.
ln report.txt hard.txtMake a symbolic link, which is a small file that holds the path to report.txt.
ln -s report.txt soft.txtWhat you should see: No output. Listing the folder would show soft.txt with an arrow to report.txt.
Remove the original name. The data is not lost while the hard link still names it.
rm report.txtRead the hard link. Both names led to the same data, so the text is still there.
cat hard.txtWhat you should see: The shell prints report.
Read the symbolic link. It held only a path, and the path now leads nowhere.
cat soft.txtWhat you should see: The shell prints: cat: soft.txt: No such file or directory
Terms
- Hard link
- A second name for the same data on disk, which keeps working when the original name is removed.
- Symbolic link
- A small file that holds a path to another name, and breaks when that target is removed.
Check yourself
What happens to a hard link when the original name is removed?
It keeps working, because it is a second name for the same data on disk. The data is still there.
Why does a symbolic link break when its target is removed?
It holds only a path, not the data. When the target name is gone, the path leads to nothing, so reading the link fails.
Can a hard link point to a folder?
No. Linux does not allow a hard link to a folder, for any user. A symbolic link can point to a folder.
Previous: The Linux directory tree. Next: Copying, moving and deleting safely.