How to Create Symbolic Link in Linux: Simple Guide
To create a symbolic link in Linux, use the
ln -s command followed by the target file or directory and the link name. For example, ln -s /path/to/original /path/to/link creates a symbolic link pointing to the original file or folder.Syntax
The basic syntax to create a symbolic link is:
ln: the command to create links-s: option to create a symbolic (soft) link instead of a hard linktarget: the original file or directory you want to link tolink_name: the name of the symbolic link you want to create
bash
ln -s target link_name
Example
This example creates a symbolic link named mylink that points to the file /usr/bin/python3. After running the command, mylink acts like a shortcut to /usr/bin/python3.
bash
ln -s /usr/bin/python3 mylink ls -l mylink
Output
lrwxrwxrwx 1 user user 15 Apr 27 12:00 mylink -> /usr/bin/python3
Common Pitfalls
Common mistakes when creating symbolic links include:
- Forgetting the
-soption, which creates a hard link instead of a symbolic link. - Using a relative path for the target without understanding where the link will be used, causing broken links.
- Trying to create a link where a file or directory with the same name already exists.
Always check if the link name already exists and use absolute paths for clarity.
bash
ln /usr/bin/python3 mylink # Wrong: creates hard link ln -s /usr/bin/python3 mylink # Correct: creates symbolic link
Quick Reference
| Command | Description |
|---|---|
| ln -s target link_name | Create a symbolic link named link_name to target |
| ls -l link_name | Show details of the symbolic link |
| rm link_name | Remove the symbolic link without affecting the target |
| readlink link_name | Display the target path of the symbolic link |
Key Takeaways
Use
ln -s target link_name to create symbolic links in Linux.Always use absolute paths for the target to avoid broken links.
Check if the link name already exists to prevent errors.
Symbolic links are shortcuts and do not duplicate the original file.
Remove symbolic links with
rm without affecting the original file.