Glen Knight

NYC Based IT Professional

Links in the Linux File System

Hard Link vs Soft Link – a hard link is a file linked directly to an inode(a data structure on a file system that stores all the information about a file except its name and its actual data), while a soft/symbolic link is a link to another name in the file system. Deleting/renaming/moving the original file will not affect the hard link as it links to the underlying inode.

Hard Link Count

[glen.knight@expert-syn-srv-1 ~]$ ls -ld /etc
drwxr-xr-x. 109 root root 8192 Jul 17 12:01 /etc

The 109 here indicates the hard link count of /etc, or in other words, there are 109 names linked to the same metadata

[glen.knight@expert-syn-srv-1 ~]$ ls -ldi /etc
132 drwxr-xr-x. 109 root root 8192 Jul 17 12:01 /etc

Here, we added the ‘i’ switch to the ls command and the results are prefixed with 132, which represents the inode number of the directory

Current/Previous Directory

[glen.knight@expert-syn-srv-1 ~]$ ls -ldi /etc/.
132 drwxr-xr-x. 109 root root 8192 Jul 17 12:01 /etc/.

Notice the . in the /etc directory. We usually know of this dot as referring to the current directory. This is actually a file that is linked to the same metadata(inode) as the current directory.

If we create a new directory, named t1 for instance, and do an ls -l we will see the following

[glen.knight@expert-syn-srv-1 t1]$ ls -l
total 0

Meaning that there are no files in the directory. When we do an ls -la, however, we see the following

[glen.knight@expert-syn-srv-1 t1]$ ls -la
total 8
drwxr-xr-x  2 glen.knight linuxusers 4096 Jul 18 15:36 .
drwxrwxrwx 12 root        root       4096 Jul 18 15:36 ..

We see our . to represent the file that has a hard link to our current directory, and .. which is a hard link to the parent directory. There is also the ‘total’ line after the ls command. According to the ls documentation(found via coreutils), the meaning of this line is :

‘For each directory that is listed, preface the files with a line `total BLOCKS’, where BLOCKS is the total disk allocation for all files in that directory.’

A more detailed explanation can be found at the following StackOverflow post:
What is that “total” in the very first line after ls -l? [closed]

To drive the point home of directory links we can observe the following

[glen.knight@expert-syn-srv-1 mnt]$ ls -ldi t1
22020097 drwxr-xr-x 2 glen.knight linuxusers 4096 Jul 18 15:54 t1

We can see that our t1 directory has 2 hard links, which are ‘.’ and ‘..’ the minimum number of links for any directory.

If we create a new directory of s1 within t1, and run the same command as above on the t1 directory, we can observe the following:

[glen.knight@expert-syn-srv-1 mnt]$ ls -ldi t1
22020097 drwxr-xr-x 3 glen.knight linuxusers 4096 Jul 18 16:00 t1

Notice that the hard link count has increased to 3, so we can now easily observe how many subdirectories are within a particular directory.

Hard/Soft Link Files
We can demonstrate creating hard links to files by first creating a simple file with the contents ‘hello’:

[glen.knight@expert-syn-srv-1 t1]$ echo hello > f1

We can execute the following command to link the file f1 to f2

[glen.knight@expert-syn-srv-1 t1]$ ln f1 f2

We can then observe the results by executing an ls -li

[glen.knight@expert-syn-srv-1 t1]$ ls -li f1 f2
22020099 -rw-r--r-- 2 glen.knight linuxusers 6 Jul 18 16:04 f1
22020099 -rw-r--r-- 2 glen.knight linuxusers 6 Jul 18 16:04 f2

From here we can observe that we created a hard link between f1 and f2 since they point to the same inode number. They do not show as links, but as regular files. If we display the contents of the files we will see the exact same output:

[glen.knight@expert-syn-srv-1 t1]$ cat f1
hello
[glen.knight@expert-syn-srv-1 t1]$ cat f2
hello

If we execute the following command:

[glen.knight@expert-syn-srv-1 t1]$ ln -s f1 f3

and do a ls -li

[glen.knight@expert-syn-srv-1 t1]$ ls -li
total 12
22020099 -rw-r--r-- 2 glen.knight linuxusers    6 Jul 18 16:04 f1
22020099 -rw-r--r-- 2 glen.knight linuxusers    6 Jul 18 16:04 f2
22020100 lrwxrwxrwx 1 glen.knight linuxusers    2 Jul 18 17:03 f3 -> f1
22020098 drwxr-xr-x 2 glen.knight linuxusers 4096 Jul 18 16:00 s1

We can observe a new type of file. It has a different node number from f1 and f2 and has a hard link count of 1. This means that f3 is a hard link passing through to the path of the original file f1.

An advantage of soft links is that they can span across file systems, where hard links cannot.

Recap
**Everything in Linux is represented as a file***

Regular files – denoted by the dash at the beginning of the file listing

-rw-r--r-- 2 glen.knight linuxusers    6 Jul 18 16:04 f1

Directories – denoted by a d at the beginning of the file listing

drwxr-xr-x 2 glen.knight linuxusers 4096 Jul 18 16:00 s1

Symbolic Links – denoted by an l in the file listing, and map back to the original file

lrwxrwxrwx 1 glen.knight linuxusers    2 Jul 18 17:03 f3 -> f1

Block Devices – denoted by b in the file listing, used to refer to devices such as hard disks, etc,

brw-rw---- 1 root disk    202,   0 Jul 17 12:01 xvda

Character Devices – denoted by c in the file listing, used to refer to character devices

crw-rw-rw- 1 root tty       5,   0 Jul 17 18:25 tty

Named Pipes – denoted with a p in their file listing
Sockets – denoted with an s in their file listing

Leave a Reply

Your email address will not be published. Required fields as marked *.

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