Linux file Shortcuts → Symlinks

Viky
6 min readOct 22, 2023

Exploring the Linux Symbolic Links.

For people who just switched from Windows to Linux you might be wondering how to create shortcut files in linux right? because shortcut files in windows are inevitable almost everyone who used windows must came across shortcut files atleast once. our windows desktop is filled with shortcut files for game, folder, apps, etc… but the moment you entered into the world of Linux the chances seeing a shortcut file is really low.

file shortcuts in windows | source: google images

The linux way of handling Shortcuts:

Unlike windows shortcuts, linux shortcuts are way more different, in windows the process of creating a shortcut for files or apps or games or any folders is pretty much same. but in linux there are two ways, you can create shortcuts for files and folders using symbolic links or if you want your shortcut to be executable (i.e. double click on it to open a software) you might have to create a desktop file for it which is different from symbolic links that is used to create a shortcut file.

if you want to know about how to create a desktop files check this

Symbolic Links:

In linux there are two types of links are available they are hard-links and soft-links or sym-links and soft-links is what most of the internet article guides refers to create a shortcut file in linux. “ln” is the command that is used to create symbolic links and most of the time only soft-links are referred by those internet articles. now lets understand what these both link types are.

Hard Links:

This is the original unix way of creating links for a file, basically all file names are hard-link for the file’s content. for example if you create a file called secret.txt with the content Hi, How are you, basically files are made of two parts.

  • The data part containing the file’s contents
  • The name part that holds the file’s name

So our OS creates a hard-link with name secret.txt which points to the content Hi, How are you in the disk. basically the file system assigns an inode number for the file’s content therefore the name hard-link is assigned with the inode of the file’s content. So every file you create has one hard-link by default which is the file’s name itself. with hard-links it’s like having multiple names for a same file, you create a new name part which points to the same inode number of the file content.

But hard-links comes with few disadvantage, you can’t simply create a hard-link for a directory in linux and you can’t create a hard-link for a file in different file system for some reasons.

To know more about why hard-links are restricted for directories click here

Soft Links:

Soft links are created to overcome this restrictions of hard-links, with soft-links you can basically create symbolic links for any file or folder in any files system, you can also create link to files in a file system on a network. unlike hard-links creating a new name part which refers to the inode of the file content. soft links are separate file that contains text strings that are interpreted by operating system itself and opens the file or directory that is points to. thus avoiding any circular reference issue in hard-links.

since soft-links are separate files it has it’s own inode number unlike hard-links which share same inode number of file’s content. thus making it more secure and flexible to work with.

The soft-links has some of it’s disadvantages like data confusion or data missing it occurs when original file is deleted the soft-link is broken thus making it useless and another disadvantage is it consumes more space than hard-link since it is a separate file.

Symbolic Links | Source: google images

Tip: you can find the number of links a file or folder have and it’s inode number using the ls -li command

In the above image we can see that file1 has 3 links (3rd column of output) which are the following:

  • file1 (file name itself)
  • file1-hard (hard-link)
  • file1-hard (soft-link)

we can also see that the file1 and file1-hard points to same inode number 18875659 which is the inode of the file1’s content and file1-soft has different inode number 18875658 which is the content of the file1-soft.

When to use which:

In case of hard-links imagine if you have a file which is 2 GB in size which is in same file system and you want to access it even though if it is deleted accidentally, one way is you can create multiple copies of that file to different locations which results in consuming a lot of space.

for example if you create 2 copies of that file the total size you sacrified for that file is 3*2=6GB which is practically not feasible if you have multiple files that you want to make copies of.

Another way is to create multiple hard-links for the same file instead of making a copy of it thus resulting in only 2GB of space consumed overall(the hard-link size is very low) because only the name parts are created which points to the same inode of the file content in the disk but still preserves the original file!

NOTE: you need to conscious while handling with hard-link, you need to delete all of the hard-links to delete a file completely! you also should not create hard-link of directories even if your OS allows it!

Soft-links are used wherever hard-links cannot be used for example you want to create a link for a directory you can do it using soft-link, you can also use it create links for files that are in different file system or different file system in a network.

But unlike hard-links soft-links will not preserve the original file content, if original file is deleted soft-link is broken and you cannot access the file content via that link.

How to create links:

Soft-links:

You can create soft-links using the “ln” command and -s, source file path and destination file path as arguments.

ln -s <source_file_path> <destination_file_path>

Hard-links:

Creating hard-links is same as soft-links except you don’t have to use -s flag to create hard-link.

ln <source_file_path> <destination_file_path>

Note: you need to use sudo if you want to create a link for a file which you don’t have access to.

Conclusion:

Symbolic links can seemingly simple and easy but it can be easily misunderstood by beginners, In my case I misunderstood hard-links as just creating a copy of original file and thought “what is the need of hard-link if it is same as creating a copy of the original file!” but as soon as I understood it, it all makes sense! I hope this article helps you to understand the basics of symbolic links in linux.

Thank you for reading!

--

--