Skip to main content

Command Palette

Search for a command to run...

How to Create Symbolic Link in Linux

Updated
6 min read

Creating symbolic links in Linux is a handy skill that can save you time and make managing files easier. If you've ever wanted to link one file or folder to another without copying it, symbolic links are the way to go. They act like shortcuts, pointing to the original file or directory, so you can access it from different places without duplication.

In this article, I’ll guide you through the process of creating symbolic links in Linux. You’ll learn what symbolic links are, how to create them using simple commands, and some practical tips to use them effectively. By the end, you’ll feel confident managing your files with symbolic links.

A symbolic link, often called a symlink or soft link, is a special type of file that points to another file or directory. Think of it as a shortcut on your desktop but for the Linux file system.

  • It contains a reference to the original file or folder’s path.
  • When you access the symlink, the system redirects you to the target file.
  • Unlike a hard link, a symlink can link to directories and files on different file systems.
  • If the original file is deleted, the symlink becomes broken and won’t work.

Symbolic links are useful for organizing files, simplifying access, and managing software configurations without moving or copying files.

The main command to create symbolic links in Linux is ln with the -s option. Here’s the basic syntax:

ln -s [target_file_or_directory] [link_name]
  • -s tells ln to create a symbolic link (soft link).
  • target_file_or_directory is the path to the original file or folder.
  • link_name is the name of the symbolic link you want to create.

Suppose you have a file called report.txt in your home directory, and you want a shortcut to it on your desktop.

ln -s ~/report.txt ~/Desktop/report_link.txt

This command creates a symbolic link named report_link.txt on your desktop that points to report.txt.

If you want to link a directory, say /var/logs, to a folder in your home directory:

ln -s /var/logs ~/logs_link

Now, ~/logs_link acts as a shortcut to /var/logs.

When creating symbolic links, you can use absolute or relative paths for the target.

  • Absolute Path: The full path from the root directory, e.g., /home/user/report.txt.
  • Relative Path: The path relative to the location of the symlink, e.g., ../report.txt.

Why Use Relative Paths?

Relative paths make your symlinks more portable. If you move the directory containing the symlink and the target together, the link still works.

If you are inside the directory /home/user/projects and want to link to a file in /home/user/docs:

ln -s ../docs/report.txt report_link.txt

This creates a symlink in projects pointing to docs/report.txt using a relative path.

After creating a symlink, you might want to verify it or manage it.

Use the ls -l command to see where a symlink points:

ls -l ~/Desktop/report_link.txt

Output example:

report_link.txt -> /home/user/report.txt

This shows the link name and its target.

To delete a symbolic link, use the rm command:

rm ~/Desktop/report_link.txt

This removes the link without affecting the original file.

If you want to change where a symlink points, delete the old link and create a new one with the updated target.

Symbolic links are powerful tools for many tasks. Here are some common uses:

  • Simplify Access: Create shortcuts to deeply nested files or folders.
  • Manage Configurations: Link configuration files from a central location to multiple places.
  • Organize Projects: Link shared resources across different project directories.
  • Save Disk Space: Avoid duplicating large files by linking instead of copying.
  • Software Management: Link different versions of software or libraries for easy switching.

Sometimes symbolic links don’t work as expected. Here are common problems and solutions:

  • Broken Symlink: If the target file is moved or deleted, the symlink breaks. Use ls -l to check and recreate the link if needed.
  • Permission Denied: Ensure you have the right permissions to access the target file or directory.
  • Relative Path Confusion: Double-check relative paths to make sure they correctly point to the target from the symlink’s location.
  • Linking Across File Systems: Symlinks can point across file systems, but hard links cannot.

Once you’re comfortable with basic symlinks, try these tips:

  • Use readlink to display the target of a symlink:

    readlink ~/Desktop/report_link.txt
    
  • Combine symlinks with scripts to automate linking tasks.

  • Use symlinks in combination with environment variables for flexible configurations.
  • Create symlinks in system directories (with sudo) to manage system-wide resources.
CommandDescription
ln -s /path/to/file link_nameCreate a symbolic link to a file
ln -s /path/to/directory link_nameCreate a symbolic link to a directory
ls -l link_nameCheck where a symlink points
rm link_nameRemove a symbolic link
readlink link_nameDisplay the target of a symbolic link

Conclusion

Creating symbolic links in Linux is a simple yet powerful way to manage your files and directories. By using the ln -s command, you can create shortcuts that save space and improve organization. Whether you’re linking files, directories, or managing configurations, symlinks make your workflow smoother.

Remember to choose between absolute and relative paths based on your needs, and always verify your links with ls -l. With these tips, you’ll be able to use symbolic links confidently in your daily Linux tasks.

FAQs

A symbolic link points to the file path and can link directories or files across file systems. A hard link points directly to the file’s inode and cannot link directories or cross file systems.

Yes, symbolic links can point to files or directories on different drives or partitions, unlike hard links.

Use ls -l to check the link. If the target file doesn’t exist, the symlink will be shown in a different color or with a warning.

Yes, you can create symbolic links in directories where you have write permissions. Root access is only needed for system directories.

Delete the old symlink with rm and create a new one with ln -s pointing to the new target.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts