Wednesday, November 30, 2011

Hard links vs Symbolic links

Working with Linux and UNIX offers some handy extras not typically found on Windows based systems. One great feature is hard and soft links, which is somewhat like a shortcut that you can create but looks just like a normal file.

This works great when you need to share a file but do not want to give permissions to the file directly. So how does this work? Every file on a Linux or UNIX based system is mapped to an inode. Inodes are the identification of every single file and object on the system. This includes users, files, rights, etc.

To see the inode values, you can run the command "ls -i".


rob@robmint ~/sandbox $ ls -li
total 4
2098382 -rw-r--r-- 1 rob rob 5 2011-11-30 17:29 testfile

The file "testfile" has a inode value of "2098382".

Now there are two different links we can create. One is a hard link, the other is a soft link. Here's the difference.

Hard link - These are files associated to the same inode. They are duplicate files and are independent from the original file. Also as you can see below, look the same but there is a slight hint.

Let's first create the hard link using the "ln" command.


rob@robmint ~/sandbox $ ln testfile testfilehardlink

Now let's see what we created. 



rob@robmint ~/sandbox $ ls -li
total 8
2098382 -rw-r--r-- 2 rob rob 5 2011-11-30 17:29 testfile
2098382 -rw-r--r-- 2 rob rob 5 2011-11-30 17:29 testfilehardlink

Again, we see the inode information is the same, but also note the value of "2". Which tells us that the original file "testfile" is linked two times, once to "testfile" and another to "testfilehardlink". 


So we mentioned that hard links are duplicates of the original file, but what happens when we delete the original file? Let's find out. We will delete the file "testfile" and leave behind "testfilehardlink".


rob@robmint ~/sandbox $ rm testfile
rob@robmint ~/sandbox $ ls -li
total 8
2098382 -rw-r--r-- 1 rob rob  5 2011-11-30 17:29 testfilehardlink

Now let's see if we can access "testfilehardlink". 

rob@robmint ~/sandbox $ cat testfilehardlink
1234

It works! Now, even after deleting the original, you can still access the data. But this looses the benefit of using link since now the file is no longer being updated from the original file. 

So now we know hard links, what about soft links? Let's create the soft links using the "ln -s" command. 

rob@robmint / $ ln -s testfile2 testfilesoftlink

Now let's look at the soft link we created. Notice how there is a visual reminder that it's a soft link. 

rob@robmint ~/sandbox $ ls -li
total 8
2097736 -rw-r--r-- 1 rob rob 10 2011-11-30 17:41 testfile2
2098382 -rw-r--r-- 1 rob rob  5 2011-11-30 17:29 testfilehardlink
2098638 lrwxrwxrwx 1 rob rob  9 2011-11-30 17:48 testfilesoftlink -> testfile2

Here we created the the soft link, but let's see how this works if we delete the original file. 

rob@robmint ~/sandbox $ rm testfile2
rob@robmint ~/sandbox $ ls -li
total 8
2098383 -rw-r--r-- 2 rob rob 5 2011-11-30 17:50 testfile
2098383 -rw-r--r-- 2 rob rob 5 2011-11-30 17:50 testfilehardlink
2098638 lrwxrwxrwx 1 rob rob 9 2011-11-30 17:48 testfilesoftlink -> testfile2


Now the original file is deleted, let's try to access the file "testfilesoftlink".


rob@robmint ~/sandbox $ cat testfilesoftlink
cat: testfilesoftlink: No such file or directory

As you can see, once the original file is gone, so it the soft link file. This is because the soft link is a link to the inode, not a duplicate file. 

Now, why would you want to use soft links over hard links? Soft links are a path back to the file, not to the inode, which means that you can access links across partitions. It's pretty cool feature and when working with longer path names this could be extremely handy. 

Rob








No comments: