What a symlink is and why you'd use one
A symlink (short for symbolic link) is a file that points to another file or folder somewhere else on your system. When you open a symlink, Linux takes you to the actual file it references instead. Think of it as a shortcut that works at the file system level — you can use it in commands, scripts, and programs exactly as if it were the real file.
Symlinks solve real problems. You might create one to avoid typing a long path repeatedly, to keep multiple copies of the same file in sync without duplicating storage, or to organize files in a way that matches how you work without moving them from where programs expect them to be. For example, if a program looks for a configuration file at /etc/myapp/config but you want to keep it in your home folder, a symlink at that location points the program to the real file.
Linux also distinguishes symlinks from hard links, which are a different tool. A hard link is another name for the exact same file on disk; a symlink is a pointer to a file that can be on a different disk or even point to a file that doesn't exist yet. For most everyday use, symlinks are what you want.
Key Takeaways
- Create a symlink with the command ln -s /path/to/real/file /path/to/symlink, where the real file comes first and the symlink name comes second.
- Use relative paths (like ../folder/file) when the symlink and target are in the same area of your file system, and absolute paths (starting with /) when they're far apart.
- List symlinks with ls -l and they will show an arrow pointing to their target; remove them with rm just like any other file.
- A broken symlink (one pointing to a file that no longer exists) won't cause errors but won't work either; find them with find -L to clean them up.
The basic syntax: ln -s target symlink
The command to create a symlink is ln -s, followed by the path to the file or folder you want to point to, then the name and location of the symlink you're creating. The -s flag tells ln to make a symbolic link rather than a hard link.
Here's the pattern:
ln -s /path/to/real/file /path/to/symlink
The order matters: the target (the real file) comes first, and the symlink name comes second. If you reverse them, the command will fail or create the symlink in the wrong place. For example, to create a symlink called mylink in your home folder that points to a file at /var/log/app.log, you would type:
ln -s /var/log/app.log ~/mylink
After running this, ~/mylink exists and behaves like the real file. If you open it, read it, or pass it to a program, Linux follows the pointer to /var/log/app.log and uses that instead.
Absolute paths versus relative paths
You can write the target path as either absolute (starting with /) or relative (starting with ./, ../, or a folder name). The choice affects how the symlink behaves if you move it or the target.
An absolute path always points to the same place, no matter where the symlink lives. Use this when the target and symlink are in different parts of your file system or when you want the symlink to keep working even if you move it. For example:
ln -s /home/user/documents/report.pdf ~/Desktop/report
A relative path describes where the target is relative to the symlink's location. Use this when both are in the same folder or nearby folders, because the symlink stays valid if you move the entire folder structure together. For example, if both the symlink and target are in your home folder:
ln -s documents/report.pdf ~/Desktop/report
Or if the target is one level up from the symlink:
ln -s ../report.pdf ~/Desktop/report
Relative paths are safer for portable setups; absolute paths are clearer when you're working across distant parts of the file system.
Creating symlinks to folders
Symlinks work on folders exactly as they do on files. The syntax is identical — just point to a folder path instead of a file path. For example, to create a symlink to a folder:
ln -s /var/www/myproject ~/projects/myproject-link
Now ~/projects/myproject-link is a symlink that points to the /var/www/myproject folder. When you navigate into it, list its contents, or pass it to a program, you're working with the real folder.
One important difference: if you use rm to delete a symlink to a folder, it removes only the symlink, not the folder itself. This is safer than with hard links and is one reason symlinks are preferred for this kind of work.
Viewing and removing symlinks
To see which files are symlinks and what they point to, use ls -l:
ls -l ~/mylink
The output will show an arrow and the target path. For example:
lrwxrwxrwx 1 user user 20 Jan 15 10:30 mylink -> /var/log/app.log
The l at the start indicates it's a link, and the arrow shows where it points. To list all symlinks in a folder, use:
ls -l | grep "^l"
To remove a symlink, use rm just as you would for any file:
rm ~/mylink
This deletes only the symlink itself, leaving the target file untouched. You can also use unlink, which does the same thing:
unlink ~/mylink
Finding and fixing broken symlinks
A broken symlink is one that points to a file or folder that no longer exists. It won't cause errors, but it won't work either — trying to open it will fail with a "file not found" message. Over time, broken symlinks can clutter your system.
To find all broken symlinks in a folder and its subfolders, use:
find /path/to/search -L -type l
The -L flag tells find to follow symlinks and report those that are broken. For example, to search your home folder:
find ~ -L -type l
Once you've found broken symlinks, you can remove them with rm. If you want to delete all broken symlinks in a folder automatically, you can combine the commands:
find ~ -L -type l -delete
Be careful with -delete — test your find command first without it to make sure you're targeting the right symlinks.
Common mistakes and how to avoid them
The most frequent error is reversing the order of the target and symlink name. Remember: target first, symlink second. If you type ln -s ~/mylink /var/log/app.log, you're trying to create a symlink at the wrong location and will likely get a permission error.
Another mistake is forgetting the -s flag. Without it, ln creates a hard link instead, which behaves differently and can cause confusion. Always include -s unless you specifically need a hard link.
If the target path doesn't exist when you create the symlink, the command will still succeed — the symlink is created but becomes broken immediately. This is sometimes intentional (you might create a symlink before the target exists), but usually it's a typo. Double-check your target path before running the command.
Finally, be aware that some programs and tools don't follow symlinks by default. If a symlink isn't working as expected, check the program's documentation or try using the real path instead. Most command-line tools respect symlinks, but some older or specialized programs may not.
Frequently Asked Questions
Can I create a symlink to a file that doesn't exist yet?
Yes. The symlink will be created successfully, but it will be broken until the target file is created. This is sometimes useful for setting up directory structures in advance, but usually it's a sign of a typo in the target path.
What's the difference between a symlink and a hard link?
A symlink is a pointer to another file; a hard link is another name for the same file on disk. Symlinks can point across file systems and can point to folders; hard links cannot. For most everyday use, symlinks are the right choice.
If I edit a file through a symlink, does it change the real file?
Yes. A symlink is transparent — any changes you make through it affect the real file. This is the whole point of using symlinks to keep files in sync.
Can I create a symlink to a symlink?
Yes, and it works as expected. The second symlink will point to the first, which points to the real file. However, this can become confusing, so avoid it unless you have a specific reason.
How do I move a symlink without breaking it?
If the symlink uses an absolute path, you can move it anywhere and it will still work. If it uses a relative path, moving it to a different folder will break it unless you update the relative path. To move a symlink safely, use mv as you would any file, then verify it still works with ls -l.