A file has one single inode since it is the inode that uniquely identifies the file. You can have several names/paths pointing to the same inode, this is called "hard links".
When you list a file with ls -l
, a column is the number of references to the inode (i.e. the number of paths that lead to it). For files it is usually 1
, unless hard links have been explicitly created:
drwxr-xr-x 2 root root 4096 Aug 20 12:11 ./
drwxrwxrwt 29 root root 86016 Aug 20 12:11 ../
-rw-r--r-- 3 root root 0 Aug 20 12:10 linked1
-rw-r--r-- 3 root root 0 Aug 20 12:10 linked2
-rw-r--r-- 3 root root 0 Aug 20 12:10 linked3
-rw-r--r-- 1 root root 0 Aug 20 12:10 single
Above, single
is the name of a file with a single path to it, while linked*
are three names for the same inode. If you do ls -il
to show the inodes:
>>>ls -il *
24641901 -rw-r--r-- 3 root root 0 Aug 20 12:10 linked1
24641901 -rw-r--r-- 3 root root 0 Aug 20 12:10 linked2
24641901 -rw-r--r-- 3 root root 0 Aug 20 12:10 linked3
24641866 -rw-r--r-- 1 root root 0 Aug 20 12:10 single
This confirms that the three linked*
files are actually the same inode. If we rm linked2
, the reference count in the remaining names is decreased:
>>>ls -l
drwxr-xr-x 2 root root 4096 Aug 20 12:15 ./
drwxrwxrwt 29 root root 86016 Aug 20 12:15 ../
-rw-r--r-- 2 root root 0 Aug 20 12:10 linked1
-rw-r--r-- 2 root root 0 Aug 20 12:10 linked3
-rw-r--r-- 1 root root 0 Aug 20 12:10 single
For directories the references count is normally 2 plus the number of subdirectories (AFAIK, the "normal" reference (from the parent), the .
in the directory itself, and the ..
in each subdirectory).
Otherwise, a file (or directory) can use several allocation blocks, depending on contents and this can change during the lifetime of the file.