Hard link and soft link?

I would like to give some examples to describe practical meaning of hard links,soft links and cache in memory in a linux system. I will start creating two ramdisk partitions:

Now lets look at the free memory usage of my PC;

According to this output, my cache data is 658MB. As you might know, cached amount isn’t actually used by processes actively but kernel keeps them in memory as they might be referenced. Instead of keeping them in the secondary storage (e.g hard disk), as long as memory is available, kernel caches them in the memory. Now we will increase this number sharply.

Above we have created two ram disk partitions each of which has 256M capacity and mounted. Lets create a 200MB file on /ramdisk1

When we check the memory usage once again;

Can you see that we incremented cached data by 200MB ? Now lets skip to hard & soft link concept:

The number 201439 is the inode number and orig_file1 name is pointing to this inode. We can create a soft link to this file as below;

We can see that the new soft link has a different inode number. Let’s see what happens if we create a hard link to this orig_file1 ?

The new hard link has the same inode like the original file (orig_file1). As soon as you create the hard link, link counter is increment by 1 and you see that link count as 2 in the “ls” output but hang on a second! /ramdisk1 file system has the 256M capacity how can I have 400M in this file system. This isn’t the reality indeed. Both file names orig_file1 and orig_file1-hard referring to the same inode which is 201439 therefore there is no duplication.
If you delete orig_file1;

“rm” command will only decrease the link counter by 1 and nothing will happen except our symbolic link will reference a non-existent file because it doesn’t point to the location of the file (inode) but the removed file itself.
If you run “rm” command on orig_file1 once more, link count will drop to 0 and linux will delete the data by releasing its resources.

You have a feedback?

This site uses Akismet to reduce spam. Learn how your comment data is processed.