ln — Create Links
💬 "ln command"
ln creates an alias (link) to a file. Without -s, it creates a hard link; with it, a symbolic link.
In practice, you create a symbolic link that points to the current version (for example, current → release-20261018) and use it for zero-downtime deployments.
- Concept: A link points to the same content under another name without copying the file.
- Left column: In
ls -l, a symbolic link starts withland showsname -> target. - Hard link: Shares the same inode. If you delete one name, the data remains as long as another name remains.
- Symbolic link: A special file that contains a path. If the target disappears, it becomes a broken link.
Common options
| Option | Description |
|---|---|
| -s | Creates a symbolic (soft) link — the default in practice |
| -f | Overwrites an existing link if present |
| -n | When the target is a directory link, replaces the link itself instead of the inside of the link |
| -v | Prints what was created |
3 essential command combinations
ln -sfn /opt/app/releases/20261018 /opt/app/current— atomically replaces the current version pointerln -s /var/www/html/index.php /etc/nginx/html/index.php— symlinks the actual file into the configuration pathls -l /opt/app/current— shows where the link points
💬 "Tell me the order to check when you receive a real customer inquiry."
Here are common situations you encounter on production servers, organized step by step.
Step 1 · Prepare the new version directory
Extract the new release into a separate directory.
tar -xzf /tmp/release.tgz -C /opt/app/releases/20261018
Step 2 · Replace the pointer
Use -sfn to change the current link to the new version. The replacement is atomic, so there is no momentary interruption.
ln -sfn /opt/app/releases/20261018 /opt/app/current
Step 3 · Verify
Verify the link target and the service response.
ls -l /opt/app/current && curl -sI https://example.com | head -1
How it is used in practice
1. Replacing the pointer for zero-downtime deployment
Situation — The web server points to /opt/app/current and you need to switch to a new version.
ln -sfn /opt/app/releases/20261018 /opt/app/current
Result — The current link points to the new release, and new requests are served with the new code.
Point — Compared to copying the entire directory, this causes less disruption and rollback is done with a single link change.
2. Sharing configuration files
Situation — Multiple apps need to reference the same configuration file.
ln -s /etc/app/common.env /srv/app1/.env
Result — Change it in one place, and it is reflected in all apps.
Point — Because unexpected synchronization issues can occur when apps write to the configuration, use this mainly for read-only sharing.
Things to watch out for
- Trying to overwrite an existing link without -f fails. The -sfn combination is the safest.
- If you omit -n for a directory link, you can end up creating a link inside the link.
- Relative-path symbolic links can break depending on the working directory, so absolute paths are recommended.
- When deleting, confirm whether you are deleting only the link or also the original.
Actual output
The result of verifying that the current link points to the new release.
!ln command execution result: verifying the symbolic link target
Live production server
Source (HTML): https://games.aidebatehub.com/programs/linux/filesystem/ln.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments