Create And Change Hard And Symbolic Links In Linux (104.6 LPIC1)
This post covers the Linux Professional Institute 1 certification (LPIC-1) topic of how to “Create and change hard and symbolic links”.
There is a sleight prerequisite to this post.. In that you should have a general understanding of what an inode is, along with general hard drive and partitioning.
Unfortunately at the time of writing this post I haven’t yet covered inodes on this site. However here is the Wikipedia page on inodes.
Just remember that every file has a unique inode number.
By the end of reading the following material you should hopefully have a good understanding of what links are, as well as why and how we use them.
Thankfully this linux topic is not particularly complex to learn..
But when you understand linking in linux then this will make your life a whole lot easier as a System Administrator.
What Is A Link In Linux?
A linux link is a file that can be created by the user to point to data stored on the file system.
There are two types of links that a user can create in linux. These are:
- Hard links
- Soft links (Symbolic links)
Hard links are used to point directly to the data itself by using inodes whereas soft/symbolic links point to another file.
Think of a link as a shortcut if you’re familiar with a Windows environment.
Note that “soft link” and “symbolic link” is synonymous with each other. Meaning that the pretty much mean the same thing.
And there is one important linux command that we will use for creating links:
Why Use Links In Linux?
It’s common for a user to create links in linux for convenience.
One good example of the use of a link would be to access a configuration file that exists deep in the file system.
The link can be placed in a convenient location that is easy to remember and navigate to, for the user.
This saves the user time from having to look around the file system for a particular file every time they require access to it.
What Is The Difference Between HARD And SOFT Links?
Both a soft link file and a hard link file can be created to point to a regular file.
A regular file and a hard link file both point to the same inode, which in-turn points to the data.
However, a soft link points to the path and file name of a regular file, rather than the same inode.
How Is This Different?
The regular file and the hard link file is both the exact same file, just with two different names. However they both have the same inode.
In-fact, the hard link file is also a regular file too.
We can delete one of the files and the data will still exist.
We can change the name of each file if we wanted to and that’s OK because they both point to the same inode.
However if we have a soft link file that points to a regular file then we can’t change the filename of the regular file without breaking the soft link.
So Why Use Soft Links Over Hard Links?
Soft links are generally used in linux when linking to files that exist on other hard drives or partitions.
Other partitions can be mounted onto the file system and so we can create a soft link which will point to a regular file by specifying the path to it.
However, hard links can’t point to the same inode as another file that exists on another partition.
This is one reason to use a soft link over a hard link. We can’t create a hard link across file systems.
The system can’t point to an inode that exists on a different drive (hard drive and/or partition)
we can’t create hardlinks that cross devices.
We can mount another drive to the linux system, and so we can then create a soft link because a soft link points to a files path.
soft links are much more versatile however hard links do provide some use as we can change a file from different locations.
We can view file inodes in a directory by executing the following ‘ls’ command:
FREE Download: Linux Mint Tool Collection!
Install tons of great software from the Linux Mint repository with my personal tool list that I’ve built up over the years. I also keep this list updated from time-to-time.
For more information Click Here
How To Create A Hard Link In Linux
To create a hard link file pointing to the same inode as a regular file, the regular file must firstly exist on the filesystem.
Let’s create a regular file by using the ‘touch’ command. The following example shows me creating a regular file called “source_file1.txt”.
You can name this file whatever you please:
We can see that the file now exists by executing the following ‘ls’ command:
Now that the regular file has been created, I can now create the hard link to it.
In the following example command I will use ‘ln’ to create a hard link file.
I will call this hard link file “destination_hard_link.txt” and this link will point to the same inode of the regular file which is called “source_file1.txt”.
ln source_file1.txt destination_hard_link.txt
I have now successfully created two files that are literally the exact same file.
By executing the following command in a terminal we can see these files exist in the directory:
It’s important to know that these two files are not copies of each other.
It’s now possible to edit one of these files and we would see the changes by viewing the other file.
We can also delete one of these files if we wish to do so and the other file will still exist.
The hard link file is also a regular file!
How To Create A Soft Link In Linux
To create a soft link file pointing to a regular file, the regular file must firstly exist on the filesystem. (The same is true when creating a hard link)
Here I will take you through an example.
Why not follow along with me and we can create it together?
Open up a terminal window and create a source file by using the following command.
We will call it “source_file.txt” but you can name it anything you please:
Then we can view the file details by executing the following command:
Now that our source file has been created we can go ahead and create a soft link to that source file.
Here I will give the soft link file the name “soft_linked_file.txt”:
ln -s source_file.txt soft_linked_file.txt
Notice how the source file needs to be specified first.
Also notice that the “-s” switch is used with the “ln” command when we want to create a soft link?
We don’t use the “-s” switch when creating a hard link!
By executing the following command we can see the new soft link file has been created and it indicates that it is pointing to the source file:
Specify The Full Path To Source File For Good Practice
Although the above steps were successful, it’s good practice to specify the FULL path to the regular file when creating a soft link.
Let’s create a second soft link, but this time I will specify the full path to the regular file. I will call the second soft link “soft_linked_file_2.txt”:
ln -s /home/installtekz/LPIC1/source_file.txt soft_linked_file_2.txt
And once again we can view the files in the current directory with the following command:
Broken Soft/Symbolic Links
We need to be very careful when we specify the path and filename of the source file.
If we make a typo then the soft link will still be created, but it will be broken!
The below screenshot shows me typing the source filename wrong when I create a third link as I specify a file that doesn’t exist.
Notice how the bash environment highlights the broken link in red?
Create A Soft Link File And Place It Into A Folder
We can also link a file and place it into a folder in the same command.
For this to work correctly and not end up with a broken link, we certainly need to specify the whole path to the source file. In my case this is “/home/installtekz/LPIC1/”
Let’s now create a folder with the following command:
Now I’m going to create a soft link file that points to the source file “source_file.txt” and place this link into the new folder called “folder1”.
However, in this example I’m not going to specify a new name for the soft linked file.
In this case, the new soft linked file will also be called “source_file.txt” as the command will automatically name the new file by using the source file name:
ln -s /home/installtekz/LPIC1/source_file.txt folder1/
If we are creating the soft link in a different directory then we don’t need to specify a destination file name if we choose not to.
However we DO need to specify a new destination file name if we are creating the soft in the same folder as the original file!
Why is this…?
Because we can not have two files exist in the same directory using the same file name.
Why not take a look inside of “folder1” by executing the following command:
ls -l folder1/
Copy VS Linking
don’t get confused with linking a file over creating a copy of a file as these are two very different tasks that create a different result.
By creating a copy of a file we end up with two files with different inodes meaning they both point to two different sources of data.
Hopefully by now you have a good understanding of how to create and change hard and symbolic links in linux.
Don’t forget to practice as this is the key to learning linux.
If you have followed along through the examples with me then you should have a good idea what to expect on the LPIC-1 exam.
One of the key points to take away from this post is to remember that:
- The “ln” command WITHOUT the “-s” switch will create a hard link
- And the “ln” command WITH the “-s” switch will create a soft link.
A helpful resource can be found Here for a Question and Answer guide to help you pass the LPIC-1 certification!