Symbolic Links in Linux: How to Use Them
In this article
Symbolic links (symlinks) are special files that point to another file or directory. They let you access a target from a different path without copying it. You should use them to save space, keep configurations in sync, or make files appear in multiple places. To create one, use the ln -s command. This guide shows you how to create, view, and remove symlinks, and explains when they beat hard links.
What You'll Need
- A Linux system with a terminal (any distribution works).
- Basic familiarity with the command line (cd, ls, pwd).
- Write permission on the directory where you'll place the symlink.
Step-by-Step
- Open a terminal. On most desktops, press
Ctrl+Alt+Tor find “Terminal” in the apps menu. - Understand the syntax. The basic command is:
ln -s [target] [link_name]. The target is the file or directory you want to point to; the link name is the new symlink you create. If you omit the link name, it uses the target's basename in the current directory. - Create a symlink to a file. For example, to make
shortcut.txtpoint to/home/user/documents/report.txt, run:ln -s /home/user/documents/report.txt shortcut.txt. This creates a symlink namedshortcut.txtin the current directory. - Create a symlink to a directory. Use the same command:
ln -s /var/www/html mysite. Nowmysiteacts like a shortcut to that directory. You cancd mysiteand it will take you inside the target. - Use relative paths when possible. Instead of absolute paths, you can use relative ones. For example, from
/home/user,ln -s documents/report.txt report.txtcreates a symlink that points relative to its own location. This makes the symlink portable if you move both the link and the target together. - Verify the symlink. Run
ls -lto see the symlink and its target. It will look like:shortcut.txt -> /home/user/documents/report.txt. The arrow shows where it points. - Use the symlink. You can read, write, or execute the symlink just like the original file. For directories, you can list contents, create files inside, and navigate through it. Changes made via the symlink affect the original target.
- Remove a symlink. Use
rm linkname(notrm -runless it points to a directory and you want to delete the link, not the target). For example,rm shortcut.txtremoves the symlink only, leaving the original file untouched. - Find broken symlinks. If the target is moved or deleted, the symlink becomes broken. Use
find . -type l -xtype lto list broken links in the current directory. Replace.with the path you want to search. - Update a symlink. To change where a symlink points, you can remove it and recreate it, or use
ln -sfn new_target linkname. The-fforces overwrite, and-ntreats the link as a file (useful if it points to a directory).
Common Mistakes
- Using absolute paths when you need portability. If you move the symlink to another system or location, absolute paths break. Use relative paths when the target and link will move together.
- Deleting the target via the symlink. Running
rm symlinkremoves the link, butrm symlink/(with a trailing slash) can delete the contents of the target directory if you're not careful. Always double-check what you're removing. - Forgetting the
-sflag. Without-s,lncreates a hard link, which behaves differently (see FAQ). Hard links cannot cross filesystems and cannot link to directories. - Creating a symlink to a relative path from the wrong directory. The relative path is interpreted from the directory where the symlink is created, not from your current working directory. If you're in
/home/userand createln -s documents/report.txt report.txt, the symlink points todocuments/report.txtrelative to/home/user, which is correct. But if you create it from/tmp, it will point to/tmp/documents/report.txt, which likely doesn't exist.
Tips
- Use symlinks for dotfiles. Keep your configuration files (like
.bashrc) in a Git repository and symlink them into your home directory. This way you can sync settings across machines. - Symlink a directory to a faster disk. If you have a large folder on a slow drive, move it to a fast SSD and symlink the original path to the new location. Applications that expect the old path will still work.
- Check the target with
readlink. To see where a symlink points without usingls -l, runreadlink linkname. - Use
pwd -Pto see the physical path. If you're inside a symlinked directory,pwdshows the logical path (the symlink), whilepwd -Pshows the real target path. - Be careful with scripts that follow symlinks. Some commands (like
cp -r) may follow symlinks and copy the target contents instead of the link itself. Usecp -Pto preserve symlinks.
Troubleshooting
- “No such file or directory” when using the symlink. The target likely doesn't exist or the relative path is wrong. Run
ls -lto see the target, then verify that path exists. Usereadlink -f linknameto resolve the absolute path. - Permission denied when accessing the symlink. Check permissions on both the symlink and the target. The symlink itself usually doesn't matter, but the target must be readable/executable by your user. Use
ls -lon the target. - Symlink loops. If you create a symlink that points to itself (e.g.,
ln -s a a), commands may hang or fail. Remove the broken link withrm linknameand recreate it correctly. - “Too many levels of symbolic links” error. This usually means a loop or a chain that's too deep. Check each link in the chain with
readlinkand fix the broken one. - The symlink appears but doesn't work after moving the target. Update the symlink with
ln -sfn new_target linknameor recreate it.
Frequently Asked Questions
What is the difference between a symbolic link and a hard link?
A symbolic link is a separate file that points to a path. If the target is moved or deleted, the symlink breaks. A hard link is another name for the same inode; it points directly to the data. Hard links cannot cross filesystems and cannot link to directories. Symlinks can do both.
Can I symlink to a file on another filesystem?
Yes, symlinks work across filesystems because they store a path, not an inode. That's one of their main advantages over hard links.
How do I see all symlinks in a directory?
Use ls -l and look for entries with an arrow (->). Or use find . -type l to list all symlinks recursively.
Does deleting a symlink delete the original file?
No. Deleting a symlink only removes the link itself. The original file or directory remains untouched. However, if you delete the target, the symlink becomes broken.
Can I symlink a directory?
Yes, symlinks can point to directories. You can cd into them, list their contents, and create files inside. Just be aware that some commands (like rm -r) may behave differently if you include a trailing slash.
Why would I use a symlink instead of copying the file?
Symlinks save disk space and keep a single source of truth. If the original changes, all symlinks reflect that change automatically. Copying creates independent duplicates that can drift out of sync.