Symbolic Links, Hard Links, and Inodes: A Beginner Guide
Before you begin
- Applies to:Linux systems with ln, ls, cat, mv, and rm.
- Where to start:Open one terminal as your regular user. Run the exercise in your home directory.
- What you need:Use the practice file created in the guide. Keep $HOME as written. You do not need sudo.
Guide tools
A symbolic link is a small file that points to another file’s location. You can use it much like a shortcut. If the target moves, the link can stop working.
A hard link gives an existing file another name. Both names refer to the same file contents. Changing those contents through either name changes what you see through the other.
You will create one text file, try both types of link, and see what happens when a name changes or is removed. You only need a Linux terminal and basic familiarity with copying commands. Use a normal user account; none of these steps needs sudo. These examples use Linux versions of ln, ls, cat, mv, and rm.
A symbolic link follows a path. Hard-link names share one file.
Create Your Practice Folder
Start HereCreate a new folder called links-practice in your home directory and enter it. The && means “run the next command only if the first command succeeds.” Then use pwd to check where you are.
mkdir "$HOME/links-practice" && cd "$HOME/links-practice" && pwd❯ What you should see
/home/sam/links-practiceCreate and Read One Text File
Your First Fileprintf prints text, and > sends that text into a file. The \n starts a new line. Use cat to read the file back. Because this is a new practice folder, original.txt should not already exist.
printf 'Hello from my file.\n' > original.txtcat original.txt❯ Expected file contents
Hello from my file.Make a Symbolic Link
A Shortcut-Like Linkln creates a link. Adding -s requests a symbolic link. Put the existing file first and the new link name second. The final command lists the link so you can see where it points.
ln -s original.txt shortcut.txtls -l shortcut.txt❯ The important part of the listing
shortcut.txt -> original.txtRead the File Through Its Link
Follow the LinkReading shortcut.txt follows the link to the original file. Add a second line to the original, then read through the link again. The link does not hold a separate copy of the text.
cat shortcut.txtprintf 'A second line.\n' >> original.txtcat shortcut.txt❯ Expected result from the final cat command
Hello from my file.A second line.Rename the File and See the Link Break
An Expected Errormv renames the original file to renamed.txt. The link still points to the old name, original.txt. Reading the link now produces an expected error. The last command confirms that your text still exists under its new name.
mv original.txt renamed.txtcat shortcut.txtcat renamed.txt❯ Expected error, followed by the surviving file contents
cat: shortcut.txt: No such file or directoryHello from my file.A second line.Point the Link at the New Filename
Fix the LinkRemove the broken link by its exact name, then create it again pointing to renamed.txt. Here rm shortcut.txt removes the link itself. It does not remove the file that the link points to. Read through the repaired link to check the result.
rm shortcut.txtln -s renamed.txt shortcut.txtcat shortcut.txt❯ Expected file contents
Hello from my file.A second line.Give the Same File a Second Name
A Hard LinkThis time, run ln without -s. It creates a hard link named second-name.txt. Both names now refer to the same file. The -i option asks ls to show each file’s inode number: a number used to identify a file on a filesystem. Both names should show the same number in this practice folder.
ln renamed.txt second-name.txtls -i renamed.txt second-name.txt❯ Example inode numbers; your number will differ
3210 renamed.txt 3210 second-name.txtChange the Contents Through the Second Name
Shared ContentsAdd a line through second-name.txt, then read renamed.txt. The new line appears through both names because they share the same file contents. A hard link is useful as another name, but it is not an independent copy.
printf 'Added through the hard link.\n' >> second-name.txtcat renamed.txt❯ Expected result when reading renamed.txt
Hello from my file.A second line.Added through the hard link.Remove One Name and Compare the Links
See the DifferenceRemove the name renamed.txt. The file remains accessible through its other hard-link name, second-name.txt. The symbolic link breaks again because it points to the removed pathname. This is the main difference the exercise is designed to show.
rm renamed.txtcat second-name.txtcat shortcut.txt❯ Expected contents followed by a broken-link error
Hello from my file.A second line.Added through the hard link.cat: shortcut.txt: No such file or directory| What you learned | Symbolic link | Hard link |
|---|---|---|
| What it provides | A path to follow | Another name for the same file |
| Target filename is renamed or removed | Can become broken | Other hard-link names still work |
| Changes to the shared file contents | Visible when its target resolves | Visible through either name |
| Independent backup copy | No | No |
Optional: why a hard link is not a backup
A hard link shares the live file’s contents and permissions. An unwanted in-place change affects both names. Some editors instead save by replacing a filename with a newly created file; an existing hard link can then continue referring to the earlier file. That application-dependent behavior makes hard links unsuitable as a general backup strategy. Use a backup method that creates a separate, protected recovery copy.
Remove Your Practice Files and Folder
CleanupThe exercise should now have only second-name.txt and the broken link shortcut.txt left. Inspect the folder first. Remove those two exact paths, leave the folder, then use rmdir to remove it. rmdir only removes an empty directory; if anything unexpected remains, stop and inspect it.
ls -la "$HOME/links-practice"rm "$HOME/links-practice/second-name.txt" "$HOME/links-practice/shortcut.txt"cd "$HOME" && rmdir links-practice && printf 'Practice folder removed.\n'❯ Check that the folder has been removed
Practice folder removed.The success message confirms the empty practice folder was removed. You have now seen a symbolic link follow a path, a hard link share a file, and the different effects of removing a filename.
References: GNU coreutils ln and ls inode numbers. Continue with file permissions or directory layout.
Your feedback keeps these production procedures accurate and battle-tested.