Symlink
1. Goal
Understand what ln -s does on Linux, when to use it, and how to verify it safely from the terminal.
2. Why this matters in real jobs
Symlinks are everywhere in real environments:
-
Pointing
/var/www/currentto the active release in a deploy (blue/green style) -
Making config paths stable (e.g., linking a service to a versioned config file)
-
Avoiding duplication while keeping predictable paths (scripts, systemd units, CI runners)
If you don’t understand symlinks, you’ll break deploys, copy files unnecessarily, or debug the wrong path.
3. Minimal theory
-
lncreates a link. -
-smeans symbolic link (a “symlink”) — basically a pointer to another path. -
A symlink is like a shortcut: it stores the target path, and the OS resolves it when you access the link.
-
If the target moves or is deleted, the symlink becomes broken (it still exists, but points nowhere).
Also: there are hard links (without -s), but for now: symlinks are what you’ll use most day-to-day.
4. Plan (step-by-step)
-
Create a small test directory and file.
-
Create a symlink with
ln -s. -
Inspect the symlink using
ls -landreadlink. -
Prove behaviour by reading through the link and then breaking the target.
-
Learn the safe overwrite patterns you’ll use at work.
5. Practice (commands + exercises with full breakdown)
A) Create a test area (host-safe)
mkdir -p ~/lab-links && cd ~/lab-links
echo “hello” > original.txt
Breakdown
-
mkdir= make directory-p= “parents”; don’t error if it exists, create missing parents too
-
~/lab-links= path under your home directory (safe) -
&&= only run the next command if the previous succeeded -
cd= change directory -
echo "hello" > original.txt>redirects stdout into a file (overwrites)
Keyboard shortcuts (terminal fluency)
-
Ctrl+Lclears the screen (NOTE: frequently used) -
Ctrl+Ajump to start of line;Ctrl+Eend of line -
↑/Ctrl+Pprevious command;Ctrl+Rreverse search history (NOTE)
Self-service lookup
-
man ln -
ln --help -
man bash(for&&and redirection), orhelp cd
B) Create a symlink
ln -s original.txt link.txt
What this does
Creates link.txt as a symbolic link pointing to original.txt.
Breakdown (every word)
-
ln= link command -
-s= “symbolic”; make a symlink instead of a hard link -
original.txt= target (the thing you want to point to) -
link.txt= link name (the new shortcut path)
Keyboard shortcuts
-
Tabcompletion: typeorigthen pressTabto complete filenames -
Alt+.(orEscthen.) inserts last argument of previous command — handy when reusing paths
Verify
ls -l
cat link.txt
Breakdown
-
lslists directory contents-l= long listing (shows link arrows likelink.txt -> original.txt) (NOTE)
-
catprints file contents — it follows the symlink transparently
Self-service lookup
-
man ls/ls --help -
man cat/cat --help
C) Inspect the symlink target directly
readlink link.txt
readlink -f link.txt
Breakdown
-
readlinkprints what a symlink points to -
-f= follow recursively and canonicalise (resolve to an absolute “real” path where possible)
Keyboard shortcuts
-
Ctrl+Wdeletes the word behind the cursor (fast edits) -
Ctrl+Uclears to start of line;Ctrl+Kclears to end of line
Self-service lookup
-
man readlink -
readlink --help
D) Show what happens when the target disappears
rm original.txt
cat link.txt
ls -l link.txt
What you’ll see
-
cat link.txtshould fail: “No such file or directory” because the link points to a missing target. -
ls -l link.txtwill still show the arrow, but it’s now broken.
Breakdown
-
rmremoves a file (dangerous outside a lab) -
Here it’s safe because we’re in
~/lab-links.
Keyboard shortcuts
-
Ctrl+Ccancels a running/blocked command (NOTE) -
Ctrl+Dsends EOF (useful to exit shells or end input)
Self-service lookup
man rm/rm --help
E) Real-job safe update pattern (overwrite symlink)
When updating a symlink to point to a new target, you’ll often use:
ln -sfn newtarget link.txt
Breakdown
-
-ssymlink -
-fforce (remove existing destination if needed) -
-ntreat destination that is a symlink as a normal file (prevents weird behaviour when link points to a dir)
Production note
This is a common release-switch trick: update a single stable path to a new version atomically-ish (often combined with mv patterns). Always verify after.
Self-service lookup
man lnthen search for-nand-f(inman, press/then type-n)
6. Mini review + checklist
-
✅
ln -s TARGET LINKNAMEcreates a symbolic link -
✅
ls -lshowsLINK -> TARGET -
✅
cat LINKreads the target content transparently -
✅ If TARGET is removed, LINK becomes broken
-
✅ Use
readlink/readlink -fto inspect resolution
7. Control questions with answers (job-like scenarios)
- Scenario: Your deploy script expects
/opt/app/currentto always point at the active release folder (/opt/app/releases/2026-02-26-1200). You need to switch to a new release with minimal risk.
Answer: Use a symlink:
ln -sfn /opt/app/releases/NEW /opt/app/current
Then verify:readlink -f /opt/app/currentand a health check.
-
Scenario: A service reads
/etc/myapp/config.yaml, but you want to keep versioned configs in/etc/myapp/configs/config-2026-02.yamland switch between them.
Answer: Make/etc/myapp/config.yamla symlink to the chosen version, then restart/reload the service and confirm it reads the new config (logs + behaviour). -
Scenario: Someone deleted the target file but the symlink still exists. A build now fails with “No such file or directory” even though the path exists.
Answer: Check withls -landreadlink. Fix by recreating the target or repointing the symlink.
8. Small homework task with answers
Task (realistic lab):
Create a “current config” symlink and rotate it.
-
In
~/lab-links, createconfig-v1.yamlandconfig-v2.yamlwith different contents. -
Make
config.yamlsymlink toconfig-v1.yaml. -
Switch
config.yamlto point toconfig-v2.yamlusing a safe overwrite pattern. -
Verify which version is active using both
catandreadlink -f.
Answer (commands)
cd ~/lab-links
echo “version: v1” > config-v1.yaml
echo “version: v2” > config-v2.yaml
ln -s config-v1.yaml config.yaml
cat config.yaml
readlink config.yaml
ln -sfn config-v2.yaml config.yaml
cat config.yaml
readlink config.yaml
readlink -f config.yaml
Next best action (1–3)
-
Practise the difference between symlinks vs hard links: run
ln original hardlinkand comparestatoutput. -
Learn safe file/path inspection:
stat,file,realpath,readlink -f. -
Tie it to container or lab workflows: use a symlink in
~/.local/binto switch between tool versions while keeping a stable path for scripts or automation.
Discovery question
When you update a symlink used by a running service, how do you prove the service has picked up the new target — logs (journalctl), process open files (lsof), or a reload (systemctl reload) — and what’s your rollback plan?
Related
- Back to Linux Concepts
- File management — mkdir, cp, mv, rm basics
- Navigation — cd, pwd, ls
- Filesystem, Paths, and FHS — paths, locations, and Linux layout