Links and inode in Linux

An inode (or Index node) is a data structure in Linux File System which stores information about a file system object. Each object in file system gets an unique inode number which distinguishes it from other inodes. Every file and directory in the Linux File System has an inode number.

You can check the inode number of files and dirs of your present working dir using ls -i command.

$ ls -i
7361517 dir1  7361518 file1  7361519 test1

or use ls -lih to get a more reliability

$ ls -lih
total 28K
7361517 drwxr-xr-x 2 user user 4.0K Aug 22 13:43 dir1
7361518 -rw-r--r-- 2 user user   24 Aug 22 13:45 file1
7361519 -rw-r--r-- 1 user user   24 Aug 22 13:46 test1

To check the information stored in an inode you can use the stat command followed by the file or directory name.

$ stat file1
  File: file1
  Size: 24              Blocks: 24         IO Block: 4096   regular file
Device: 0,52    Inode: 7361518     Links: 2
Access: (0644/-rw-r--r--)  Uid: ( 1000/  user)   Gid: ( 1000/  user)
Access: 2024-08-22 13:45:16.873752928 +0530
Modify: 2024-08-22 13:45:13.685749263 +0530
Change: 2024-08-22 13:45:13.685749263 +0530
 Birth: -

This will show you the following details.

  • Name
  • Size
  • Disk block location
  • IO Block
  • Device ID
  • Inode number
  • Links
  • Permissions
  • GID
  • UID
  • Last accessed
  • Last modified
  • Last changed
  • And the birth time

NOTE: a file system object exist when it has at least 1 link which points to its inode.

“No Space Left on Device” issue

Sometimes it could happen that an operating system reposts that “No Space Left on Device”. But when you check the available space on your partition, it has a lot space available.

One of the possible reason is that the system has gone out of inodes. To confirm run df -i and will show the disk utilization of inode on the system.

$ df -i
Filesystem              Inodes   IUsed    IFree IUse% Mounted on
udev                    420615     464   420151    1% /dev
tmpfs                   431491     907   430584    1% /run
/dev/sda3             14409728 1446844 12962884   11% /
tmpfs                   431491     926   430565    1% /dev/shm
tmpfs                   431491       6   431485    1% /run/lock
/dev/sda1                    0       0        0     - /boot/efi
tmpfs                    86298     110    86188    1% /run/user/1000
/home/user/.Private 14409728 1446844 12962884   11% /home/user

Above is the output of the df -i command.

This is how normally a inode usage will look like. But if your are getting the “No Space Left on Device” error then the “IUsed%” would be shown 100%. During the creation of partition, the inode limit is configured.

Links

On a Linux system you can create two types of link to a file.

  • Soft Link(or symbolic link)
  • Hard Link

Soft links are similar to links on a windows computer. When you want a file to be stored on a different location but able to access the file using a link on your desktop or anywhere convenient.

You can create a soft link or a symbolic link on Linux using the ln command followed by -s (for symbolic) tag and below is an example:

$ ln -s /path/to/your/file /path/to/destination/dir/

If you want to name the soft link the same as the source file then you can just point to the target dir without file name. But if you want the soft link to be named different then you can enter the name after the target dir.

The inode number of the soft link is different than the source file inode number.

If you the source file get deleted then the soft link becomes invalid.

When a hard link is created, another file get created with the same underlying inode. Which means, both the files are referencing to the same inode. If you edit any one of the files, it gets reflected on the other file as well. But if you delete any one of the file the other file does not get deleted.

You can create a hard link without using the -s tag and below is an example:

$ ln /path/to/your/file /path/to/destination/dir/

The inode number of the hard linked files are the same.

If you want to move or transfer the hard linked file on a different drive, you may have a problem as the inode number of the file may be unknown to that drive’s file system.

Thanks for reading till the end.