What a symlink is and why you might need one
A symlink (short for symbolic link) is a pointer to another file or folder on your computer. When you click or open a symlink, Linux takes you to the actual file it points to, rather than opening a copy. Think of it like a shortcut on Windows or macOS — the symlink itself is tiny, but it leads somewhere else.
You might create a symlink when you want the same file to appear in multiple places without duplicating it, or when a program expects a file in one location but you keep it somewhere else. For example, if you have a configuration file buried deep in your home directory but a program looks for it in /etc, a symlink can bridge that gap.
Symlinks save disk space because they don't copy data — they just point to it. If you edit the file through the symlink, you're editing the original. If you delete the symlink, the original file stays put.
Key Takeaways
- A symlink is created with the ln -s command, where the first path is what you're pointing to and the second is where the symlink goes.
- The target file must exist before you create the symlink, or the symlink will point to nothing and break.
- Use absolute paths (starting with /) when you're not sure, because relative paths can break if you move the symlink to a different folder.
- You can see which files are symlinks by running ls -l and looking for an arrow (→) in the output.
- Deleting a symlink does not delete the file it points to, only the pointer itself.
The basic command: ln -s
The command to create a symlink is ln -s, followed by the path to the file you want to point to, then the name and location of the symlink you're creating.
Here's the structure:
ln -s /path/to/original/file /path/to/symlink
For example, if you have a file called config.txt in your home directory and you want to create a symlink to it in /usr/local/bin, you would type:
ln -s ~/config.txt /usr/local/bin/config.txt
The -s flag tells Linux to create a symbolic link. Without it, ln creates a hard link, which is different — a hard link is another name for the same file on disk, not a pointer to it. For most purposes, you want -s.
Using absolute paths to avoid broken symlinks
A relative path describes where a file is compared to where you are right now. An absolute path starts with a forward slash and describes the exact location from the root of your system.
When you create a symlink with a relative path, Linux stores that relative path inside the symlink. If you later move the symlink to a different folder, the relative path no longer points to the right place, and the symlink breaks.
For example, if you're in your home directory and type ln -s config.txt ~/Desktop/config-link, the symlink stores the path "config.txt" (relative to where the symlink is). If you later move that symlink to a different folder, it will look for "config.txt" in that new location and fail.
To avoid this, use absolute paths:
ln -s /home/yourname/config.txt ~/Desktop/config-link
Now the symlink stores the full path and will work no matter where you move it. You can find your absolute path by typing pwd in the terminal.
Creating symlinks to folders
Symlinks work the same way for folders as they do for files. The command is identical:
ln -s /path/to/original/folder /path/to/symlink
For example, if you have a folder called Projects in your home directory and you want a symlink to it on your Desktop:
ln -s /home/yourname/Projects /home/yourname/Desktop/Projects-link
When you open the symlink, you'll see the contents of the original folder. Any files you add to the folder through the symlink are added to the original folder.
Checking which files are symlinks
To see which files in a folder are symlinks, use ls -l (the long list format):
ls -l
In the output, symlinks are marked with an arrow (→) pointing to the file they link to. For example:
config-link -> /home/yourname/config.txt
The first character of each line is also a clue: regular files start with a dash (-), folders start with d, and symlinks start with l.
If you want to see only symlinks in a folder, you can use:
ls -l | grep "^l"
This filters the output to show only lines that start with l.
Removing a symlink
To delete a symlink, use the rm command, the same as you would for a regular file:
rm /path/to/symlink
This deletes only the symlink itself, not the file or folder it points to. The original file stays where it is.
Be careful not to accidentally type rm /path/to/symlink/ (with a trailing slash) if the symlink points to a folder. Depending on your system, this might delete the contents of the original folder instead of just the symlink. Always use rm without a trailing slash for symlinks.
What happens when the target file is deleted
If you delete the original file that a symlink points to, the symlink becomes broken — it points to nothing. The symlink still exists, but it no longer works.
When you try to open a broken symlink, you'll get an error like "No such file or directory." You can still delete the broken symlink with rm, but it won't help you recover the original file.
To find broken symlinks in a folder, you can use:
find . -type l ! -exec test -e {} \;
This command lists all symlinks that point to files that no longer exist. The . means "search in the current folder and all subfolders."
Frequently Asked Questions
Can I create a symlink to a file that doesn't exist yet?
Yes, technically you can, but the symlink will be broken until the file is created. Linux allows you to create a symlink pointing to any path, even if nothing is there. This is sometimes useful if you're setting up a system before all files are in place, but usually you should create the target file first.
What's the difference between a symlink and a hard link?
A symlink is a pointer to a file path, while a hard link is another name for the same file on disk. If you delete the original file, a hard link still works because it points directly to the data. A symlink breaks because it points to a path that no longer exists. For most purposes, use symlinks with ln -s.
Can I create a symlink in a folder I don't have permission to write to?
No. You need write permission in the folder where you're creating the symlink. If you get a "Permission denied" error, try using sudo before the command, but be cautious — creating symlinks in system folders can cause problems if you're not sure what you're doing.
Will a symlink work if I move the original file to a different location?
Only if you created the symlink with an absolute path and the file is still at that absolute path. If you move the original file, the symlink breaks. If you created it with a relative path and move the symlink itself, it will also break. Always use absolute paths to be safe.
Can I create a symlink to a symlink?
Yes, you can, and Linux will follow the chain. If symlink A points to symlink B, which points to file C, opening symlink A will eventually open file C. However, this can get confusing, so it's usually better to point symlinks directly to the original file.