May 15

Linux: Understanding Symbolic Links

Understand the effects of a symbolic and hard link and how they differ.
What are they?

Symbolic links are like shortcuts or references to the actual file or directory. Most of the time these links are transparent when working with them through other programs. For example you could have a link to a directory to one hard drive inside a directory that lies on a different hard drive and an application will still treat it the same.

Symbolic links are used all the time to link libraries and make sure files are in consistent places without moving or copying the original. Links are often used to “store” multiple copies of the same file in different places but still reference to one file.
Creating a Symbolic Link

To create a symbolic link in Linux we use this syntax:
ln -s /path/to/original/ /path/to/linkName

What happens if I edit the link? Any modifications to the linked file will be changed on the original file.
What happens if I delete the link? If you delete the link the original file is unchanged. It will still exist.
What happens if I delete the original file but not the link? The link will remain but will point to a file that does not exist. This is called an orphaned or dangling link.
Creating a Hard Link

You can create a hard link like so:
ln /path/to/original.file /path/to/link.file
The Difference Between Soft and Hard Links
Hard links

Only link to a file not a directory
Can not reference a file on a different disk/volume
Links will reference a file even if it is moved
Links reference inode/physical locations on the disk

Symbolic (soft) links

Can link to directories
Can reference a file/folder on a different hard disk/volume
Links remain if the original file is deleted
Links will NOT reference the file anymore if it is moved
Links reference abstract filenames/directories and NOT physical locations. They are given their own inode

Practice Makes Perfect

To really grasp the concept of symbolic links lets give it a shot.

Go into the tmp directory:
cd /tmp

Make a directory
mkdir original

Copy over the host.conf file or any file to test with.
cd original
cp /etc/host.conf .

List the contents and take note of the inode (first column)
ls -ila

Create a symbolic link to host.conf called linkhost.conf
ln -s host.conf linkhost.conf

Now do list out the inodes
ln -ila

Notice how the inode for the link is different.

Now create a hard link to the same file
ln host.conf hardhost.conf

Now list the inoes one more time
ln -ila

Notice how the inode numbers are exactly the same for the hard link and the actual file.

Lets try some file operations now

Open up linkhost.conf and edit it and save it

Now look in host.conf and notice that the changes were made

Lets move host.conf now and see if it causes any problems
mv host.conf ../

Uh oh, now when we list the directory our link turned red lets see what is in side it
cat linkhost.conf
cat: linkhost.conf: No such file or directory

It looks like our symbolic link is now broken as it linked to a file name and not the inode. What about our hard link?
cat hardhost.conf

Looks like our hard link still works even though we moved the original file. This is because the hard link was linked to the inode the physical reference on the hard drive where the file resides. The soft link (symbolic link) on the other hand was linked to the abstract file name and was broken the moment we moved the file.

This leads to an interesting question. What happens if I delete a hard link? Even though the hard link is referenced to the physical location of the file on the hard drive though an inode, removing a hard link will not delete the original file.

Symbolic links will remain intact but will point to a non existent file.

By: Mark Sanborn


Copyright 2021. All rights reserved.

Posted May 15, 2013 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B