Navigating: Home » Cloud Computing » Ubuntu » Understanding & Using Symbolic & Hard Links (Ubuntu) Understanding & Using Symbolic & Hard Links (Ubuntu) Posted on July 2, 2020July 20, 2020 by devsrealm 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!