Use ln Command to Create Links
The ln command in Ubuntu is used for creating soft and hard links. Learn to use this command in this tutrorial.
The ln command is used to create symbolic links in Linux.
And in case, you don't know, there are two types of symbolic links: soft links and hard links.
The key difference between a soft link and a hard link is where it is pointing to.
The soft link points to the filename, whereas the hard link points to the data block.
And in this guide, I will show you how you can create both using the ln command.
Using the ln command in Ubuntu
To learn any command, it is necessary to learn the syntax first.
So to use the ln command, you'd have to use follow the given syntax:
ln [options] [source-file] [Link-name]
Here,
[options]
: are used to tweak the default behavior of the ln command[source-file]
: This is where you'd enter the name or the absolute path to the file of which you want to create the symlink.[Link-name]
: is where you'd enter the name of the symlink or you can also provide the absolute path here if you want to save it somewhere else.
Now, let's have a look at some examples of how you can use the ln command.
1. Create a soft link using the ln command
To create a soft link, you'd have to use the -s
flag with the ln command as shown:
ln -s [target-file] [link-name]
So let's say I want to create a soft link named MintLink
for the file LinuxMint.iso
located under /home/Test
, the, I will be using the following:
ln -s ~/Test/LinuxMint.iso MintLink
To list the symbolic links in the directory, here, I have used the tree command which will also show the path by following the symlink:
2. Create a file backup by creating a hard link
Hard links are a bit different than soft links and I personally find them more useful than soft links.
The key advantage is you can have a backup for your special files and the best part is it won't take any at all.
Let me explain.
The hard link points at the data block where the data is stored so even if you delete the actual file, you can still access it using the hardlink!
I won't go into much detail, but if you want to learn more, you can check out our detailed guide on the hard link:
Now, let's jump to how you can create a hard link.
To create a hard link, all you have to do is use the ln command without any options and it will create a hard link from the given source:
ln [target-file] [link-name]
Here, I will be creating a hard link for the LinuxMint.iso
inside the Tutorial
directory:
ln LinuxMint.iso ~/Tutorial/LinuxMint
But unlike the soft link, the hard link looks absolutely the same as any other file.
To find the hard link, you can check the inode number of the file using the ls command with the -li
flag:
And as you can see, the inode numbers are exactly the same for both files!
3. Create a symlink pointing to a directory
To create a soft link pointing to a directory, all you have to do is use the -s
flag and give the path to the directory (the same one you did with files):
ln -s /path/to/directory LinkName
So let's say I want to create a soft link of the Downloads
directory, then, I will be using the following:
ln -s ~/Downloads Downloads
And as you can see, the soft link that I created is pointing to the Downloads
directory!
4. Create a soft link for another soft link
Think of a scenario where you want to create a soft link pointing to another soft link.
Sure you can do that!
But if you use the normal method for creating a soft link that is pointing to the soft link, the link will be created for the original file and not for the soft link itself.
So in that case, you'd want to treat that soft link as a normal file.
And this can be done using the -n
option:
ln -n [existing-symlink] [symlink]
For example, here, I have created a soft link named Hello
which will point to another soft link MintISO
:
ln -sn MintISO Hello
And as you can see, the Hello
soft link is only pointing to another soft link MintISO
!
5. Force the creation of symlink
If you try creating the symlink with the existing file name, it will throw an error saying "Failed to create the symbolic link, file already exist"
To force the creation of the symlink, all you have to do is use -f
flag:
ln -f [target-file] [link-name]
Here, I tried creating the symlink with the existing file name but this time, the ln command is paired with the -f
flag:
And as you can see, the symlink was created without any errors!
Want to find broken symlinks? Here you have it
Finding broken symlinks is indeed a headache. Especially, if you are doing everything manually.
But no more headaches! We have a detailed guide for that topic:
I hope you will find this guide helpful.
And if you have any queries, feel free to ask in the comments.