facebook youtube pinterest twitter reddit whatsapp instagram

Understanding & Using Symbolic & Hard Links (Ubuntu)

Have you ever wondered how you can create shortcuts that link to other files in Linux? Well, let me introduce you to symbolic and hard links.

The two types of links in Linux are symbolic links and hard links, if you are familiar with a GUI, then you can think of them as shortcuts that point to another file or application. If you are using a Windows or macOS system, then you most likely would have seen application shortcuts.

On a Linux server, we use symbolic or hard links, which we would be practicalizing below.

Navigate into your home user directory, create a test directory, and a text file.

Note: Don't copy anything starting from < -to the right.

cd ~                     < - Changing directory into User home directory
sudo mkdir mynotes       < - Creating 'mynotes' folder
cd mynotes               < - Changing the working directory into the 'mynotes' folder
sudo touch notes.txt     < - Creating a note file
cd ~                     < - Change back to the home directory

To create a link to the notes.txt file, I'll use the following command:

sudo ln ~/mynotes/notes.txt notes

What I am doing here, is creating a hard link (notes) that is link to the file (notes.txt) in the mynotes directory of the user. Let's see something interesting, run the following command for each file:

ls -i ~/mynotes/notes.txt
ls -i notes

Output

user@server:~$ ls -i ~/mynotes/notes.txt
530384 /home/user/mynotes/notes.txt
user@server:~$ ls -i notes
530384 notes

If you look closely at the above number, you'll see they have the same inode number (would come back to the meaning of inode shortly).

This means they are both the same file, the good thing about a hard link is that, you can move the file around and it would still be linked together, the downsides of using a hard link are

  • You can't create a hard link to a directory, and
  • This link cannot be moved to a different filesystem as each system has a different inode.

To really grasp the idea of links fully, you might want to understand inodes:

An inode is a data object that contains metadata regarding files within your filesystem, think of them as a  type of database object, containing metadata for the files you're storing on your disk such as the owner of the file, size, permissions, last modified date, and type (whether it is a directory or a file).

It is represented by an integer number that is used to represent a file system object (e.g file or directory). It’s a unique number for files and directories under a disk block/partition.

Bored already, me too! Just remember that inodes contain metadata regarding files. Back to the link...

It's a bit annoying you can't create a link to a directory nor move them into a different filesystem, to overcome these limitations, we can consider using a symbolic link.

A symbolic link or soft links or symlinks is an entry that points to another directory or file, it is different to a hard link in the sense that it references a specific path, like a pointer to the actual file.

It can not only be moved around between filesystems (as these do not share the same inode number as the original file), you can also create a symbolic link to a directory as well.

Let's delete the original hard link we created, and instead create a symbolic link, use the following command:

sudo rm notes
sudo ln -s ~/mynotes/notes.txt notes

If we use the ls -l again, you'll see a different inode number:

user@server:~$ ls -i ~/mynotes/notes.txt
530384 /home/user/mynotes/notes.txt
user@server:~$ ls -i notes
262257 notes

In fact, if you run ls -l against the created symlink it would tell you where the file is pointing to:

ls -l notes
lrwxrwxrwx 1 root root 30 Jul  2 01:02 notes -> /home/user/mynotes/notes.txt

Creating a Symlink to a Directory:

Creating a symlink to a directory also follows the same approach, to create a symlink to a directory you do:

ln -s [path/to/directory] [symlink directory name]

For example, to link /home/user/document directory to 'file', I'll do:

ln -s /home/user/document file

So, a symlink isn't a clone of the original file, just a pointer to the original file's path.

Now that we've gone through both types of links, which should you use?

It depends! One lovely feature I love about a hard link is that you can move either file (the link or the original) to anywhere on the same filesystem without breaking the link.

On the other hand, a symbolic link won't exist if you move the original file, as the link will be pointing to a file that no longer exists at that location. All in all, symbolic link is the most used type as you can cross filesystems, can be linked to directories, and are easier to determine from the output where they lead.

Enjoy!

Related Post(s)

  • Send Mail with Attachment Using Mutt in GNU/Linux

    Mutt is a powerful text&#x2d;based mail client for Unix/Linux operating systems. It features color support, message threading, MIME support...

  • Using Pageant To Automatically Authenticate SSH key in Putty

    I can't count how many times I have typed my ssh key passphrase whenever my ssh connection times out, it is so annoying and repetitive. Well, thanks to the putty pageant, you can do that seamlessly.

  • How To Send Mail To Multiple Addresses Using (mailx)

    In this guide, you'll learn a couple of ways you can send mail to multiple addresses using mailx. mailx is a utility program for sending and receiving mail. I assume you already have mailx command, i

  • Monitoring Multiple Log Files In RealTime With MultiTail (Ubuntu)

    Oh my... I really find scanning through the logs file time consuming, and painful. Luckily for me, I founded Multitail, which is an awesome, and powerful tool for not only browsing through several f

  • Test Your Internet Download Speed from Terminal Using (Fast)

    Fast is a tiny utility program for testing your internet download speed from the terminal, to get started, you simply install via the snap store (Ubuntu): sudo snap install fast If you don't have sna

  • Access A Web Page In GNU/Linux Using (Links) [Web Browser in Text Mode]

    Links is a text-mode World Wide Web (WWW) browses that can be used to view a web page either via a local (file://) or remote ((http:// or ftp://) URLs. To get started with Links, install it using the