0

After checking a lot of information on the Internet, it is still not quite clear for me how many inodes a file and a folder takes.

Also, I noticed that inodes are shown in kilobytes and not as cardinal numbers.

So how to count the number of inodes required for a file or a folder?

P.S. My bad, it seems that K and M in the output of $ sudo df -ih/ mean thousands and millions and not kilobytes and megabytes.

1 Answer 1

2

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.

7
  • Why are inodes shown in kilobytes and megabytes then $ sudo df -ih/ ? By the way, I also saw the information that a file can take a different amount of inodes depending on the file system opensource.com/article/18/4/ext4-filesystem
    – t7e
    Commented Aug 20, 2020 at 9:20
  • The "Inodes" count in df -i is the number of inodes in the file system (the number of files it can hold). It is a count, not a size in KB/MB. Did you check the Wikipedia article?
    – xenoid
    Commented Aug 20, 2020 at 9:58
  • About: opensource.com/article/18/4/ext4-filesystem this severely contradicts Wikipedia, so unless you find other references mentioning this, I'll ascribe this to a brain lapse by the author.
    – xenoid
    Commented Aug 20, 2020 at 10:08
  • Yes, I checked Wiki - it did not answer my question. So K and M there just means thousands and millions? I thought they were kilobytes and megabytes.
    – t7e
    Commented Aug 20, 2020 at 10:09
  • Yes. Just counts, not sizes.
    – xenoid
    Commented Aug 20, 2020 at 10:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .