How to Use the ln Command to Create Symbolic Links in Linux

In Linux, you often need a file to be accessible from multiple directories simultaneously. For example, a web server might expect a configuration file to be located in /etc/nginx/, but your development team keeps all configuration files in a centralized /var/git-repo/ folder. Instead of copying the file (which creates two separate versions that can fall out of sync), you should create a symbolic link using the ln command.

What is a Symbolic Link?

A symbolic link (often called a symlink or soft link) is essentially an advanced shortcut. It is a tiny file that contains nothing but a text string pointing to the absolute path of another file or directory. When an application attempts to read the symlink, the Linux kernel seamlessly redirects the application to the actual target file.

The Difference Between Hard Links and Soft Links

The ln command can create two types of links:

  • Hard Links (ln target link_name): A hard link points directly to the physical data on the hard drive (the inode). If you delete the original file, the data remains accessible through the hard link. Hard links cannot span across different hard drives or file systems, and they cannot link to directories.
  • Symbolic Links (ln -s target link_name): A soft link points to the file path of the original file. If you delete the original file, the symlink breaks and becomes a “dangling link” pointing to nothing. Symlinks can span across different drives and can link to directories. Because of this flexibility, symlinks are used 99% of the time in modern Linux administration.

How to Create a Symbolic Link

To create a symbolic link, use the ln command with the -s (symbolic) flag. The syntax is always Target first, followed by the Link Name.

ln -s /path/to/original_file.txt /path/to/shortcut_link.txt

For example, if you want to link your centralized Nginx configuration file to the directory where the Nginx server expects to find it, you would run:

sudo ln -s /var/git-repo/site-config.conf /etc/nginx/sites-enabled/site-config.conf

Now, if you edit the file in the git repository, the Nginx server will instantly see the updated changes, because it is reading the exact same file through the symlink.

How to View Symbolic Links

If you list the contents of a directory using ls -l, you can easily identify symbolic links. The permissions string will start with an l (for link), and the filename will show an arrow pointing to its target.

lrwxrwxrwx 1 user user 30 Aug 12 10:00 site-config.conf -> /var/git-repo/site-config.conf

How to Delete a Symbolic Link

Deleting a symbolic link is completely safe; it does not delete the original target file.

You can remove a symlink using the standard rm (remove) command or the unlink command.

rm /path/to/shortcut_link.txt

Critical Warning for Directories: If you created a symlink to a directory, be extremely careful when deleting it. Do not include a trailing slash. Running rm my_symlink_dir will safely delete the shortcut. Running rm -rf my_symlink_dir/ (with the slash) instructs Linux to follow the link into the target directory and delete all the files inside it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.