Introduction
Today we’re going to learn the difference between Linux hard links and symbolic links on an Amazon Linux 2023 operating system. Soft links, often referred to as symbolic links, correctly reference symbolic or soft links. Both terms, soft and symbolic, are acceptable for describing these types of links.
Hard Links
What is a Hard Link?
So, what are we talking about? What’s the difference between a hard link and a soft link? Well, it comes down to what they’re referencing. A hard link is a file. So, this is file 1, and it references or points to a spot on a hard drive. This down here is my representation of a hard drive, actually, the inode layout on a hard drive, and how the hard drive stores data. So, file 1 here points to a specific spot on the hard drive, and that’s where it stores its data. If you create a hard-linked file, like name it file 2, for example, it’s pointing to the same spot on the hard drive.
The behavior of Hard Links
Now, this effectively provides you with two separate files that act just like separate files, except that if you edit file 1, file 2 will automatically be edited because they point to the same spot on the hard drive. If you change the data down here that they’re both pointing to, they’re both going to change. That’s just how hard links work.
Soft Links (Symbolic Links)
What is a Soft Link?
Soft links or symbolic links, on the other hand, work differently. In this case, file 1 originally points to a spot on the hard drive and stores its data there. When you create a symbolic link, however, this file simply points to either the file 1 descriptor or the file 1 name, rather than another file pointing to the same spot on the hard drive. It points to the name of the file instead of pointing to a spot on the hard drive.
Behavior of Soft Links
So, what does that mean? The file 2 takes up almost no space because all it does is point to that file. Creating a symbolic link, even if it’s a 5-terabyte file, will result in file 2 just pointing to it and being very small. However, deleting file 1 will render file 2 useless since it’s pointing to something that no longer exists. If someone deletes the original File 1, File 2 will still be viable because it points to the storage location on the hard drive. This allows for the deletion of one or both of these hard-linked files without affecting the data integrity. But with the symbolic link, if you delete the original file, the symbolic link is useless.
Practical Demonstration
Let’s go to the command line and see how that appears in practice because once you grasp what’s happening, it’s not terribly confusing. So, here I am on my Ubuntu system, and I’ve opened up a terminal window.
Creating and Editing Hard Links
Now, if I type ls
, you’ll see that there’s a file called testfile.txt
. Hey, file 1, that’s what we were just talking about. If we say cat
, it will show us the contents of this testfile.txt
, and basically, it’s just a text file that says, “This is a regular file.”

So, let’s start out by creating a hard link to testfile.txt
. To do that, you use the ln
command with no flags or anything, just ln
the source, which is file 1, and what we want to call the second hard-linked file. Let’s call it testfilehardlink.txt
.

[root@ip-172-31-25-66 html]# ls -l /var/www/html/testfile.txt
-rw-r--r--. 2 root root 10 Jul 17 11:43 /var/www/html/testfile.txt
[root@ip-172-31-25-66 html]# ls -l /var/www/html/wordpress/testfilehardlink.txt
-rw-r--r--. 2 root root 10 Jul 17 11:43/var/www/html/wordpress/testfilehardlink.txt
Hit enter, get no feedback, but if we type ls -l
now, you’ll see we have two files in this directory. We have our original file1.txt
, which is 17 bytes long. We also have a new file that behaves and looks exactly like testfile.It’s named testfilehardlink.txt
, and it also has 17 bytes. Why does it also have the same bytes? Because it’s pointing to the same spot on the hard drive.
[root@ip-172-31-25-66 html]# cat /var/www/html/testfile.txt
test file
[root@ip-172-31-25-66 html]# cat /var/www/html/wordpress/testfilehardlink.txt
test file
Now, let’s see how that works in practice. Let’s edit testfilehardlink.txt
. Hey, there’s that file that we looked at earlier, right? The same data’s in there. If we change “regular” to “hard linked” and save it, now if we type cat testfile1.txt
, you’ll see that the text has changed up here. We got this as a regular file.
[root@ip-172-31-25-66 html]# vi /var/www/html/wordpress/testfilehardlink.txt
[root@ip-172-31-25-66 html]# cat /var/www/html/testfile.txt
test file added hardlink
[root@ip-172-31-25-66 html]# cat /var/www/html/wordpress/testfilehardlink.txt
test file added hardlink
We ca
t the same file1.txt
, and because we modified it to testfilehardlink.txt
, the data on the underlying hard drive changed, and so now both of them reference a chunk of text on the hard drive that says, “This is a hard-linked file.”Now, the cool news is if we delete file1.txt
, our original file, we can still look at the contents of hardlink.txt
, and of course, it’s still there because it’s pointing to that spot on the hard drive where the data is stored. So now, you’ll see here we just have one file called hardlink.txt
.
[root@ip-172-31-25-66 html]# rm -rf testfile.txt
[root@ip-172-31-25-66 html]# cat testfile.txt
cat: testfile.txt: No such file or directory
[root@ip-172-31-25-66 html]# cat /var/www/html/wordpress/testfilehardlink.txt
test file added hardlink
Creating and Editing Soft Links
On the contrary, soft links or symbolic links work differently. Let’s clear the screen, and I’ll show you how that works. So here we have testfile1.txt
. To create a soft link or a symbolic link to that file, you use the same ln
command but now use them -s
for soft or symbolic, and then you say the source file, which in our case is now testfile1.txt
, and let’s call it testfile1softlink.txt
.
[root@ip-172-31-25-66 html]# ln -s testfile1.txt wordpress/testfile1softlink.txt

Again, no feedback here, but if we type ls -l
to see what’s going on, we get this really nice display by ls
of what’s actually going on. So, our original file, testfile1.txt
, is still pointing to the hard drive where the data on the hard drive is stored. However, testfilesoftlink.txt
is not pointing to the hard drive; it’s just pointing to the file name testfile1.txt
.


Now, if we cat testfile1.txt
, it’s going to show us the contents of that file because it’s soft-linked to this location, you know, this testfile1.txt
. The problem comes if we rm testfile1.txt
. Now, if we type ls -l
, it gives us those red errors here. Danger, Will Robinson! All of a sudden, there’s something wrong. It’s in red text, and it says, “OK, testfile1oftlink.txt
is pointing to the file testfile1.txt
, but that doesn’t exist, so something is broken right now.” This soft link is a broken-link, and if we try to cat
it now, it’s gonna say there’s no such file or directory. And that’s a little confusing. It says testfile1softlink.txt
has no file or directory, but what it’s talking about is there’s no file or directory that it’s trying to point us to. testfile1.txt
is gone, so that data that we had there, since the soft link doesn’t point to the hard drive location, it just points to a filename, we’ve lost that data.
Summary
So, hard links are more forgiving when you delete a file. Soft links take up less data because it’s just a pointer. However, soft links don’t store the actual data; they just store the location of the original file and the name of the file.
Hopefully, that clears things up. Hard links and soft links exist on the Amazon Linux 2023 operating system, and they both have individual quirks. Some are useful in situations where others aren’t. Now you understand how they work, and hopefully, you’ll be able to figure out the best type of link to use in your particular situation.
Conclusion
I hope that this has been informative for you, and I’d like to thank you for viewing it.
techlinux.in