Linux inode


The inode is the data structure of all files in a filesystem (except file contents and filename). It consumes around 1% of the disk space. inode stores metadata about each file.

Following are some properties stores in an inode.

  • Inode number
  • Extended attribute
  • Number of blocks
  • File access, change and modification time
  • File size
  • File type
  • Group
  • Number of links
  • Owner
  • Permissions
  • Status flags
  • Access Control List (ACL)

 


How to list metadata of a file?

$stat /tmp
  File: '/tmp'
  Size: 4096      	Blocks: 8          IO Block: 4096   directory
Device: 10303h/66307d	Inode: 4398        Links: 8
Access: (1777/drwxrwxrwt)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-02-18 22:06:12.250171624 +0000
Modify: 2019-02-18 22:17:01.972964003 +0000
Change: 2019-02-18 22:17:01.972964003 +0000
 Birth: -

 


How to list inode number of files?

$ls -i

 


How to find a file with an inode number?

$find ~/ -inum <inode number>

Things to remember:

    • inode has a count of links pointing to that inode number (just like hard links)
    • a file can only be deleted if the number of links pointing to it are 0.
    • Mapping of filename and inode is different.
    • Softlinks will have different inode but will point to the same data block as the original file
    • Hardlink will point to the same inode (original file’s inode).

 


Why do I see a large number of inodes in my filesystem? I have reached the maximum number of inodes capacity in my filesystem.

You probably have too many small files in the filesystem, you can increase the maximum number of inodes for that fs (it’s an NFS share without taking down the machine or fs). If it’s an EXT3/4 partition there is no way to dynamically increase inodes, you might have to take backup and recreate a new partition using mkfs -N command and passing number of inodes . Once you reach the inodes capacity you won’t be able to create a new file. Quicker options are to delete/zip smaller files (if possible). Again any open files will not reduce in inode count and if a file has multiple hard links.

 


How to list inodes count and used inodes?

$df -i
Filesystem      Inodes  IUsed   IFree IUse% Mounted on
udev            249195    333  248862    1% /dev
tmpfs           251193    451  250742    1% /run
/dev/nvme0n1p1 2560000 257261 2302739   11% /
tmpfs           251193      1  251192    1% /dev/shm
tmpfs           251193      7  251186    1% /run/lock

 


How to remove a file with an inode number?

Sometimes you might see a file with multiple special characters and it might be easier to just delete it via inode number.

$find . -maxdepth 1 -type f -inum <inode number> -delete

 


How to cat / print contents of a file with an inode number?

$ find -inum <inode number> -exec cat {} \;

 


Can two files have the same inode number?

Yes, if they belong to different partitions or if they a file is a hard link.

 


How go get stats of a file using python?

>>> import os
>>> from stat import *
>>> print(os.stat("favicon.ico"))
posix.stat_result(st_mode=33204, st_ino=134484763, st_dev=64770L, st_nlink=1, st_uid=1000, st_gid=1000, st_size=894, st_atime=1551046130, st_mtime=1551046127, st_ctime=1551046127)
>>> 

in the above example, we are printing the stat of a file “favicon.ico”

Categories