Recently I've done an interview for an infrastructure role, which was a charming expereince. I've had an opportunity to talk with one of the greatest rock stars in the Iranian computer industry.
During the interview, i remember that he changed the subject to the sym-links and hard-links in Linux systems. The difference between sym-links and hard-links haven't really got my attention, because i didn't use both of them on daily basis. More specifically, unlike soft-links, the concept of hard-link in Linux file system is rarely useful.
Long story short, I've been rejected in the interview but the interviewer told me some interesting facts about the subject.
He demonstrated the fact that, the process of removing a file in Linux is also commonly called as the unlinking process. And related to this phenomenon, the python's standard library doesn't really have a method named os.remove() . I mean, it has, but it is simply an alias to another method which does the actual work os.unlink(). Here is where you can dig into it.
So why its called unlinking? Let's see what does the book How linux works has to offer for us:
A traditional Unix filesystem has two primary components: a pool of data
blocks where you can store data and a database system that manages the
data pool. The database is centered around the inode data structure.
Right. I know that i can inspect the inode number for any file using ls -i.
An inode is a set of data that describes a particular file, including its type, per
missions, and—perhaps most important—where in the data pool the file
data resides. Inodes are identified by numbers listed in an inode table.
Fair enough.
Now, related to this is issue, we already seen the output of the ls -l command:
total 15668
drwxr-xr-x. 1 parsajr parsajr 512 Oct 2 11:52 ./
drwxr-xr-x. 1 parsajr parsajr 176 Sep 1 23:22 ../
-rw-r--r--. 1 parsajr parsajr 175362 Jun 3 17:54 'Create Or Hate Successful People Make Things.epub'
-rw-r--r--. 1 parsajr parsajr 365979 Jun 3 17:54 'Industrial Society and Its Future.pdf'
-rw-r--r--. 1 parsajr parsajr 2280448 Jun 3 17:54 'Medical_Myths,_Lies,_and_Half-Truths_(guidebook).pdf'
What's the number after the permission sets? Which appears to be "1" for every file that i have here? It's called a link count.
The link count field is the number of total directory entries (across all directories) that point to an inode. Most of the files have a link count of 1 because they occur only once in the directory entries. This is expected.
Fair enough. Now, hard-links in Linux by definition are just a manually created entry in a directory to an inode that already exists. So if we create an hard-link for one of our documents, we expect to see the number "2" after the permission sets:
$ ln 'Create Or Hate Successful People Make Things.epub' cool_book.epub
total 15840
drwxr-xr-x. 1 parsajr parsajr 540 Oct 2 12:01 ./
drwxr-xr-x. 1 parsajr parsajr 176 Sep 1 23:22 ../
-rw-r--r--. 2 parsajr parsajr 175362 Jun 3 17:54 cool_book.epub
-rw-r--r--. 2 parsajr parsajr 175362 Jun 3 17:54 'Create Or Hate Successful People Make Things.epub'
-rw-r--r--. 1 parsajr parsajr 365979 Jun 3 17:54 'Industrial Society and Its Future.pdf'
-rw-r--r--. 1 parsajr parsajr 2280448 Jun 3 17:54 'Medical_Myths,_Lies,_and_Half-Truths_(guidebook).pdf'
Which is indeed true!
Now, if you run rm 'Create Or Hate Successful People Make Things.epub' , you aren't lose your file. The end result is that the link count
of its inode goes from 2 to 1 (because the file cool_book.epub still points there), and the kernel knows not to remove the inode.
This issue is conceptually the same in the process of garbage collection in High-level programming languages like Python. If a ref_count of an object goes to 0, then language runtime frees the memory allocation of the object.