Create a Symbolic Link
Create a Symbolic Link
The school wants a convenient name, latest.txt, for the current mark report. A symbolic link can provide that name without storing a second independent copy of the marks. The link is a separate directory entry whose contents describe a target path.
When a program follows the link, it reaches the target. That sounds like a shortcut, but be precise: the link does not preserve the target's data as a backup. If the target moves or disappears, the link can remain while pointing nowhere useful.
What you’ll learn
- Create a symbolic link to a report.
- Explain relative target paths and recognize a dangling link.
Follow the link to the target
Link entry
latest.txt stores the path to another entry.
latest.txt → marks.txtTarget
The actual mark bytes live in marks.txt.
Diya: 72Read
Reading through the link reaches the target; removing the target can leave a dangling link.
cat latest.txt
Read the current marks through latest.txt
Prepare the example
Use a new practice directory. The first line creates marks.txt, so the target exists before the link is made. Keep the two files in that directory for this example; relative link targets are interpreted from the link's containing directory, which matters if you later move it.
Bash · Linux terminal
printf 'Diya: 72\n' > marks.txt
ln -s marks.txt latest.txt
cat latest.txtExpected result
Diya: 72In ln -s marks.txt latest.txt, -s requests a symbolic link. The target comes first, and the new link name comes second. cat latest.txt follows that link and reads the actual bytes from marks.txt, so you see Diya's mark.
Now separate this from the earlier copy example. Editing the target changes what a reader reaches through the link, because the bytes still live in the target file. Removing the target does not remove the separate link entry; it can become dangling. A link is useful for a stable access name, while a copy is useful for preserving separate content.
File targets, directory targets and relative paths
A symbolic link is a small filesystem object that points to another path. It gives you another convenient name or location for reaching the same target without copying the target's data.
Why Use Symbolic Links?
- To avoid duplicating large files or folders
- To organize files across multiple locations
- To make configurations cleaner and more maintainable
ln -s syntax for symbolic links
ln -s [target] [link_name]
Let’s break this down:
- [target] — This is the original file or directory you want to link to.
- [link_name] — This is the name of the symbolic link you’re creating.
Example 1: Linking a File
Let’s say we have a file called report.txt in our Documents folder, and we want to create a symbolic link to it on the Desktop.
cd ~/Desktop
ln -s ~/Documents/report.txt report-link.txt
Now we can check the link:
ls -l report-link.txt
lrwxr-xr-x 1 user group 25 Jul 2 10:00 report-link.txt -> /home/user/Documents/report.txt
The arrow (->) confirms that report-link.txt is a symlink pointing to report.txt.
Example 2: Linking a Directory
You can do the same for a directory. For example, link a project folder to your home directory:
ln -s ~/projects/myapp ~/myapp-link
ls -l ~/myapp-link
lrwxr-xr-x 1 user group 20 Jul 2 10:15 myapp-link -> /home/user/projects/myapp
To Remove a Symbolic Link
Just delete it like a regular file:
rm report-link.txt
Don’t worry — this won’t touch the original file.
Common Pitfall: Relative Paths
If you use a relative path instead of an absolute one, the link will break if you move it to a different location. Always prefer absolute paths when in doubt.
Create and verify symbolic links
- Use
ln -sto create symbolic (soft) links - They act like shortcuts, saving space and effort
- Work with both files and directories
- Use absolute paths for stability
Try it, then check the worked answer
Your task: Explain why a symlink can remain after its target is deleted.
Use a new practice directory. The first line creates marks.txt, so the target exists before the link is made. Keep the two files in that directory for this example; relative link targets are interpreted from the link's containing directory, which matters if you later move it.
Bash · complete example for this question
printf 'Diya: 72\n' > marks.txt
ln -s marks.txt latest.txt
cat latest.txtExpected result
Diya: 72Worked answer
It is a separate entry storing a target path.
The symlink can survive target deletion because it stores a pathname, not an automatic backup. Inspect the target path when a link stops working. Recreating a valid target can make the path useful again, but the link alone cannot recover deleted report contents.
Check your decision
A symlink stores a path, not a backup
Read a symbolic link as a path to follow. Check the operand order when creating it and the base directory of a relative target. Use copies for independent snapshots and links for another route to the same target; those are different needs.
Reaching the recap and completing the quizzes are separate milestones.