In this lecture we’ll discuss inodes and hard links. Each file on the disk has a data structure called 1
index node or inode associated with it which is fixed in length. 2
This structure stores metadata information about the file such as the type, file’s permission, file’s owner and 3
group, timestamp information, file size and so on. 4
You can see most of the information stored in a file’s inode in the output of ls -l 5
The inode structure contains all file information except the file contents and the name. Each inode 6
is uniquely identified by an integer number called inode number. To see it run ls with -i option 7
You can combine it with other options as well. 8
Yes, you heard me right, the file name and the file contents are not part of the Inode. In the inode 9
structure 10
there are only some pointers to the data blocks where the file contents are saved. 11
But where's the filename if it's not in the file's 12
inode structure? A filename is an entry in a directory. 13
Basically directories are two column tables where the first column is the filename and the second 14
column is the Inode number. 15
Something like this, this is a column and this is another. 16
This is a simplified approach, but I think it's a good one for you to understand. I'm creating a new 17
file using the touch command. 18
When I referred to a file like, say, cat a.txt it will search in the current directory for 19
the corresponding Inode number of a.txt, for this number. 20
Having that number, the kernel can identify the file and all of the information about it. So as a 21
conclusion 22
the file name doesn't belong to the file structure. 23
It’s just a string, and the pointing between the file structure and the filename happens through the 24
data structure of the directory that contains the filename. 25
An association between a file structure and a file name is called hard link. 26
In fact, this is a hardlink. In the current directory a.txt was associated with 27
this number. By the way, that's what this 1 means. 28
It means 1 hardlink. 29
You can have more associations and I mean names to the same file structure on disk, so the same file 30
can be accessed through many names in the same directory or different ones. To see the number of hardlinks 31
run ls -l or stat. 32
This regular file has one hard link. 33
You can create a new hardlink by running the ln command. 34
ln comes from link. 35
At this moment, the file called a.txt has only one hard link, because there is one name that points 36
to the inode structure. To create a new link to the same file structure 37
I run ln, the existing name and the new name: ln a.txt b.txt 38
So, in fact, I've created a new file name for the same file structure. 39
We can see that both files have the same inode number and the number of hardlinks is now 2. 40
Note that b.txt is not a copy of a.txt. 41
The information on the disk is saved only once and both files point to the same data. 42
If I change one of them, the other will be changed as well. 43
For example, I'm using a direction: ifconfig > a.txt 44
I'm changing a.txt 45
I have redirected the output of the command to the file. 46
The other file name to the same structure has the same contents. 47
It's the same. 48
You can also create a hardlink in another directly. 49
For example,ln, the existing file, b.txt and then you hardlink mybackup/c. 50
txt 51
Now there are 3 hard links or 3 names to the same file structure on the disk. 52
You see, there are 3 links. 53
And it's the same Inode number. 54
If you delete a file using rm then only the entry from the directory that contains the file is 55
removed and the link count of the inode is decremented. 56
Only when it reaches zero, the disk space associated with the inode and the inode itself is freed 57
for further reuse. 58
I'm removing a.txt 59
They have the same Inode number in the number of hardlinks is 2. 60
You know that there are two hard links for the file to which .txt points and want to find 61
out the other one. Suppose you don't know that 62
it's c.txt in mybackup is a hard link. To do that 63
use the find command with the 64
-inum option: like these. 65
Find the current director Inum and the 66
Inode number. 67
And it found all the links. 68
Also note that you cannot create a hard link to a directory or a hard link to a file on a different partition 69
or disk. A hardlink cannot cross the filesystem boundaries. 70
Let's try to create a hard link to /etc 71
hardlink not allowed for directory. 72
However, if you run the ls -l command on a directory like this, you notice a very large 73
number of links. 74
The /etc directory has 130 hardlinks. 75
Maybe you wonder how that's possible. 76
And the answer is that there are 130 names to the same structure, the /etc directory. 77
One is etc, 78
another reference is the DOT, which belongs to each directory and its reference to itself, this 79
one. 80
This is the second link. 81
And then there are 128 subdirectories, and each of them contains the double dott, which is a reference 82
to its parent. 83
So in this case, to etc. 84
If I take any directly from /etc, for example this one and I run ls-lia 85
and the directory you you'll notice there the double dot, this one; this is in fact etc. 86
So another hardlink. 87
This is inode number which is equal to the inode number of /etc 88
It's the same number. 89
One useful application of the hard links is to allow files, programs and scripts to be easily accessed 90
in a different directory without making copies of the same file. 91
And if you want to find files by the number of hardlinks, use the find command with t-links 92
option. 93
Let's find all the files in /usr with more than one link: 94
find /usr -type f -links +1 95
All these files have more than one link. 96
Let's add the -ls option. 97
5 hardlinks 10 hardlinks 3 and so on. 98
The number is always greater than 1. Besides hardlinks there's another type of link called softlink, 99
symbolic link or symlink. 100
We’ll take a short break and in the next lecture we’ll discuss symlinks.