Configuration files scattered across /etc, project directories buried three levels deep, binaries installed outside $PATH. Instead of copying files or typing long paths every time, you can create symbolic links in Ubuntu to point exactly where you need. A single ln -s command builds a lightweight pointer that behaves like the original file or directory, without duplicating a single byte. Paired with basics like moving files and directories, symlinks keep even complex project trees manageable.
What Are Symbolic Links in Linux?
Symbolic links act as pointers to files or directories rather than storing the actual data. Unlike hard links, which directly reference file data at the inode level (an inode is the filesystem record that tracks a file’s metadata), symlinks only reference the file path. If the original file is moved or deleted, the symlink becomes broken.
Symbolic Links vs Hard Links
Here is how they compare:
- Symbolic links store a path to the target. They work across filesystems and can point to directories, but break if the target moves or is deleted.
- Hard links share the same inode as the original file. They only work within the same filesystem and cannot link to directories, but remain valid even if the original filename is deleted (since both names point to the same data).
Unless you specifically need a hard link (rare outside backup tools), symbolic links are what you want.
In practice, symlinks appear everywhere: linking dotfiles from a Git repository into your home directory, exposing a custom binary in /usr/local/bin so it runs from any terminal, or pointing /var/www/html at a project folder (or folder on another partition) without moving the data. Because they store a path rather than data, they work across filesystems and mount points where hard links cannot.
Create Symbolic Links on Ubuntu
The ln command handles all link creation on Ubuntu. By default it creates hard links, so always pass -s to get a symbolic link instead.
Basic ln Symlink Syntax
ln -s [target] [symlink_name]
[target]refers to the original file or directory the symlink will point to[symlink_name]is the name of the symbolic link being created
Common ln Options Explained
The ln utility ships with the coreutils package on Ubuntu and supports several flags beyond -s:
| Option | What it does | When to use it |
|---|---|---|
-s | Creates a symbolic link instead of a hard link. | Always include this flag when you need a symlink. |
-f | Removes an existing destination before creating the new link. | Safe way to update links without manual cleanup. |
-n | Treats the destination as a normal file, even if it is a directory symlink. | Prevents ln from following an existing directory symlink. |
-r | Creates relative symbolic links based on the link location. | Keeps symlinks portable when moving directory trees together. |
-T | Forces ln to treat the link name as a file, not a directory. | Helpful when the destination path might already exist as a directory. |
-v | Prints verbose output as the link is created. | Use during troubleshooting to confirm results. |
Handling Paths with Spaces
If your file or directory names contain spaces, enclose the paths in straight quotes to avoid errors. Use $HOME (which expands safely inside quotes) instead of ~ when quoting:
ln -s "/path/with spaces/original file.txt" "$HOME/link name.txt"
Alternatively, escape each space with a backslash:
ln -s /path/with\ spaces/original\ file.txt ~/link\ name.txt
Quick Reference: Common ln Symlink Patterns
| Task | Command |
|---|---|
| Create a symlink to a file | ln -s /path/to/target ~/link_name |
| Create a symlink to a directory | ln -s /path/to/dir ~/link_name |
| Create a relative symlink | ln -sr target link_name |
| Overwrite an existing symlink | ln -sf /new/target /path/to/link |
| Remove a symlink | rm link_name or unlink link_name |
| Check where a symlink points | readlink -f /path/to/link |
| Find broken symlinks | find /path -xtype l |
Example: Creating a Symlink to a File
Create a symlink in your home directory pointing to the Nginx configuration file:
ln -s /etc/nginx/nginx.conf ~/nginx.conf
Now ~/nginx.conf opens the real file at /etc/nginx/nginx.conf without you ever leaving your home directory.
Verify a Newly Created Symlink
Confirm the link worked as expected before relying on it in scripts. A quick ls -l reveals the arrow (->) that shows the target, while readlink resolves the final destination:
ls -l ~/nginx.conf
Expected output showing the symlink and its target:
lrwxrwxrwx 1 user user 21 Jan 27 10:00 /home/user/nginx.conf -> /etc/nginx/nginx.conf
The l at the beginning indicates a symbolic link, and the arrow (->) shows the target path. Use readlink -f to resolve the full absolute path:
readlink -f ~/nginx.conf
/etc/nginx/nginx.conf
If the output does not point where you expect, recreate the link with the correct path or use the -f flag to replace it safely.
Create Directory Symbolic Links on Ubuntu
Symlinks work on directories too, not just files. This is especially handy when a project folder lives deep in a path like /var/www/html and you want quick access from your home directory.
Example: Creating a Symlink to a Directory
ln -s /var/www/html ~/website
Now cd ~/website drops you straight into /var/www/html. Verify the link:
ls -ld ~/website
lrwxrwxrwx 1 user user 13 Jan 27 10:05 /home/user/website -> /var/www/html
The -d flag tells ls to show the symlink itself instead of listing the directory contents.
Remove Symbolic Links on Ubuntu
Since symbolic links do not contain actual data, deleting them does not affect the original file or directory. The target remains intact.
Deleting a Symlink with rm
The standard rm command removes symlinks just like regular files:
rm ~/nginx.conf
When removing a symlink to a directory, do not include a trailing slash. Running
rm ~/website/(with the slash) may attempt to delete the contents of the target directory instead of the link itself. Always userm ~/website(without the trailing slash) to remove just the symlink.
Deleting a Symlink with unlink
The unlink command is an alternative that explicitly removes a single file or symlink:
unlink ~/nginx.conf
This method is useful when you want to ensure only the link is removed. Unlike rm, unlink does not accept multiple arguments or recursive flags, making it a safer choice for removing individual symlinks.
Update or Replace Symbolic Links on Ubuntu
Symlinks do not support in-place edits; you replace the link with a new one. The most direct method combines -s (symbolic) and -f (force), which removes any existing link path before creating the new one:
ln -sf /new/path/file.txt /path/to/symlink
If you prefer an explicit two-step process, delete the old link and create a new one:
rm /path/to/symlink
ln -s /new/path/file.txt /path/to/symlink
Verify the updated symlink points to the new target:
readlink -f /path/to/symlink
Advanced Symbolic Link Techniques on Ubuntu
Linking Executables for Global Access
If you manually install software outside system directories, creating a symlink in /usr/local/bin/ allows you to run the program from any location. Because that directory is writable only by root on Ubuntu, prepend sudo when creating the link:
sudo ln -s /opt/customapp/custombinary /usr/local/bin/customapp
After this, typing customapp in any terminal session runs the binary. Verify the symlink:
ls -l /usr/local/bin/customapp
lrwxrwxrwx 1 root root 27 Jan 27 10:15 /usr/local/bin/customapp -> /opt/customapp/custombinary
Ensure the target binary is executable; otherwise, the symlink will not run. You can set the executable bit with chmod:
chmod +x /opt/customapp/custombinary
You may notice symlinks always show lrwxrwxrwx (777) in ls -l output. That is cosmetic. The kernel ignores a symlink’s own permission bits and checks the target’s permissions instead, so the binary’s own mode is what matters.
Organizing Dotfiles and Shared Configurations
Many developers track dotfiles in a Git repository and link them into place. Symlinks make it easy to keep version-controlled copies under ~/dotfiles while exposing them in the locations applications expect:
ln -sfn ~/dotfiles/.bashrc ~/.bashrc
ln -sfn ~/dotfiles/.config/nvim ~/.config/nvim
The -sfn combination forces an overwrite (-f), creates a symbolic link (-s), and prevents ln from traversing existing directory symlinks (-n). This protects nested configuration trees from being merged unexpectedly.
Linking to Network Shares
Symlinks provide quick access to mounted network directories, saving time navigating long paths:
ln -s /mnt/server/share ~/network_drive
Now ~/network_drive reaches the share directly. If the share is not mounted, the symlink shows as broken until the mount comes back.
Relative vs. Absolute Symbolic Links
Every symlink stores a target path, and that path can be relative or absolute. The choice matters when you move things around later.
Relative symlinks stay valid as long as the relative structure between the link and its target stays the same. This makes them ideal inside version-controlled repositories or project trees you might relocate:
# Current directory: /home/user/project
# Target: /home/user/shared/config.txt
ln -s ../shared/config.txt project_config
Here project_config points to ../shared/config.txt. Move project/ and shared/ together to /new/location/ and the link still resolves. You can also let ln compute the relative path automatically with the -r flag:
ln -sr /home/user/shared/config.txt /home/user/project/project_config
Absolute symlinks always resolve to the same filesystem path regardless of where the link lives. Use them when the target is a fixed system file that will not move:
ln -s /etc/nginx/nginx.conf ~/nginx.conf
~/nginx.conf always reaches /etc/nginx/nginx.conf, no matter where you are. If you ever rename or relocate the target, the absolute link breaks.
Replicating a Directory Structure with Symbolic Links for Files
While the ln command does not create a single “recursive” symlink that dynamically reflects all contents of a target directory, you can use find in combination with mkdir and ln to create a mirrored directory structure where symlinks represent all original files in the new structure. This is handy when you need a linked copy of a complex directory tree.
To replicate the structure of /original/directory under /symlink/directory, with all files in the replicated structure being symlinks to their originals:
cd /original/directory
# First, create the same directory structure in the target location
find . -type d -exec sh -c 'mkdir -p "/symlink/directory/$1"' _ {} \;
# Then, create symlinks for all files into the new structure
find . -type f -exec sh -c 'ln -s "$(pwd)/$1" "/symlink/directory/$1"' _ {} \;
The first find pass recreates every subdirectory under /symlink/directory. The second pass fills that tree with symlinks pointing back to the original files. The sh -c wrapper handles paths containing spaces or special characters safely.
Because the symlinks use absolute paths, moving /symlink/directory somewhere else still works. Moving or deleting /original/directory breaks them. Make sure /symlink/directory exists (or that you have permission to create it) before running these commands.
This mirrors only regular files (existing symlinks inside the source tree are skipped). If a destination path already exists,
lnwill fail unless you deliberately use-f. Absolute targets make the mirror portable when moving/symlink/directorybut will break if you later move/original/directory. Creating correct relative symlinks is possible but requires computing a per-file relative path (for example, withrealpath --relative-to). For background on usingfind -exec, see the find -exec command guide.
Troubleshoot Symbolic Links on Ubuntu
Why Is My Symlink Broken?
A broken symlink occurs when someone moves, renames, or deletes the target file or directory it points to. Broken symlinks still exist in the filesystem but point to nothing. To find all broken symlinks within a specific directory and its subdirectories:
find /path/to/check -xtype l
The -xtype l test matches symlinks whose target does not exist. Example output showing broken links:
/path/to/check/old_config /path/to/check/projects/dead_link
To audit broken links and see which targets they reference, list them with their stored paths before cleanup:
find /path/to/check -xtype l -printf '%p -> %l\n'
/path/to/check/old_config -> /etc/app/config.old /path/to/check/projects/dead_link -> /home/user/deleted_project
Once you have confirmed which links are safe to remove, delete all broken symlinks recursively:
find /path/to/check -xtype l -delete
Always run the
-printfaudit command first to review which links will be removed. The-deleteaction is irreversible and silently removes every match without confirmation.
“File exists” Error When Creating a Symlink
If a file, directory, or another symlink already occupies the destination path, ln refuses to overwrite it. On GNU coreutils (Ubuntu 24.04 and 22.04) the error looks like:
ln: failed to create symbolic link '/home/user/link': File exists
On Ubuntu 26.04 (which ships uutils coreutils), the message is shorter:
ln: Already exists
Either way, the fix is the same. Force-replace the existing path with -f:
ln -sf /new/target /home/user/link
If the destination is an existing directory symlink, add -n so ln replaces the link rather than placing a new symlink inside the directory:
ln -sfn /new/target /home/user/link
Verify the result with readlink -f /home/user/link to confirm it now points to the intended target.
Permission Denied When Creating a Symlink
Attempting to create a symlink in a root-owned directory without elevated privileges produces:
ln: failed to create symbolic link '/usr/local/bin/foo': Permission denied
Prefix the command with sudo and double-check both paths before pressing Enter:
sudo ln -s /opt/myapp/bin/foo /usr/local/bin/foo
How Can I List All Symbolic Links in a Directory?
Use find with a shallow depth limit to list symlinks in the current directory, including hidden ones:
find . -maxdepth 1 -type l -exec ls -l {} +
Example output showing symlinks in the current directory:
lrwxrwxrwx 1 user user 21 Jan 27 10:00 ./nginx.conf -> /etc/nginx/nginx.conf lrwxrwxrwx 1 user user 13 Jan 27 10:05 ./website -> /var/www/html
Unlike ls alone, this catches hidden symlinks and avoids locale or color-code quirks.
To find all symbolic links recursively within a directory and its subdirectories:
find /path/to/your_directory -type l
The -type l filter matches only symlinks within that path. Pipe the output to grep to narrow results further, for example find /path -type l | grep config.
For the full ln reference, see the official ln man page.
Best Practices for Symbolic Links on Ubuntu
- Prefer relative paths inside project directories so clones or backups stay portable.
- Keep naming predictable: mirror the original filename to make broken links easier to diagnose.
- Document custom links in provisioning scripts or README files to avoid surprises for collaborators.
- Audit regularly with
find -xtype lon shared servers (or print mappings with-printf '%p -> %l') so stale links do not accumulate. - Test with non-root accounts before relying on symlinks in production automation, especially when sudo is required.
- Avoid trailing slashes when removing directory symlinks to prevent accidentally deleting target contents.
Frequently Asked Questions About Symbolic Links on Ubuntu
This error means a file or symlink already exists at the destination path. Use ln -sf to force-replace the existing link, or remove it first with rm and then create the new symlink. When the destination is a directory symlink, add the -n flag (ln -sfn) to prevent ln from following it.
Yes. Symbolic links are stored in the filesystem like regular files, so they survive reboots and power cycles. The only situation where a symlink breaks after a reboot is when the target file or directory was on a temporary filesystem (like /tmp on some configurations) or on a network share that has not been remounted yet.
Yes. Unlike hard links, symbolic links work across different filesystems, partitions, and mounted drives. This makes them ideal for linking to directories on external storage, secondary partitions, or network mounts. The symlink stores a path string rather than an inode reference, so it is not limited to a single filesystem.
Conclusion
Once you get comfortable with ln -s, symlinks become second nature for managing configs, binaries, and project trees on Ubuntu. Use ln -sf to update links safely, readlink -f to confirm where they point, and find -xtype l to catch broken ones before they bite you. A periodic audit and consistent naming go a long way.
It looks like you’ve covered all the bases here – a BIG leap past everything I’ve seen elsewhere. Thanks for your impressive perspicuity!
Now, as a lifelong GUI addict, I’m left with an embarrassingly basic question: Once I’ve planted (link) into (this drive) over to (that drive), do I then save to & open from (this), or (that)?
Symbolic Links creation under Dolphin is very easy. A little add-on to this fact would have been nice.
Useful guide, thank you.