Inode is a data structure that keeps track of all the information about a
file. You keep your information in a file and the OS stores the
information about a file in an
inode. Information about files is sometimes called
metadata. We can say that an inode is metadata of the data.
How does the structure of an inode look like?A directory with it’s corresponding inodes looks like this:
Inodes Directory/file names
4204851 ./
4203424 ../
4195429 dir1/
4205752 dir2/
4205722 file1
4205723 file2
4194941 .hidden_dir/
4205589 .hidden_file1
======================================
Inode structure of a directory consists of a name to inode mapping of files and directories in that directory.
In the above example, you probably noticed the first two entries of ./ [dot] and ../ [dot dot]. You might have seen them whenever you list the contents of a directory. You might also know that executing the command
cd .
cd . will change the directory to the current directory itself and the command
cd ..
cd ..will take you to the previous directory (the parent directory of the current directory).
Why that happens?
Let’s use an example. Let’s have a look at our test directory:
user@sree ~/tmp/Anchal % ls -i
4204851 ./ 4205723 file2
4203424 ../ 4195429 folder1/
4205722 file1 4205752 folder2/
Let’s have a look at the inode numbers of . (dot) and .. (dot dot):
. (dot) = 4204851
.. (dot dot) = 4203424
We’ll do the same for ~/tmp/ directory and note the inodes there:
user@sree ~/tmp/Anchal % ls -i
4203424 ./
3816836 ../
4204851 test/
The inode numbers of ~/tmp/test/ directory and . (dot) from the directory listing of ~/tmp:
. (dot) = 4203424
~/tmp/test/ = 4204851
You can see that inode number of . (dot) inside ~/tmp/test/ directory is equal to inode of test directory – 4204851. And inode of .. (dot dot) inside ~/tmp/test/ is equal to inode of . (dot) inside ~/tmp/ directory – 4203424.
=====================================
How to check Inode Utilization?
===============
user@sree ~ % df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
rootfs 5840896 659354 5181542 12% /
udev 217137 406 216731 1% /dev
tmpfs 220131 450 219681 1% /run
tmpfs 220131 6 220125 1% /dev/shm
tmpfs 220131 11 220120 1% /sys/fs/cgroup
tmpfs 220131 6 220125 1% /run/lock
tmpfs 220131 11 220120 1% /run/user
/dev/sda1 124496 265 124231 1% /boot
=============================
How to access a file using inode?
"ls -i" lists the inode of a file
or
ls -ai
====================================
As you can see, our test directory contains a file with special characters (UNICODE) – µfile¤. You wouldn’t be able to access the file by it’s filename, as your keyboard most definitely doesn’t have the micro character defined. You could access the file in several ways. The harder one, would be to find the UNICODE hex code for the micro sign and this would involve looking for the UNICODE characters table. The easier way would be to use the file inode. However, the most obvious and easiest way to deal with such signs would be to use tab-completion, yet not always a file could be deleted by it’s name. Sometimes the filename contains broken characters, not recognized by the system. Here inode comes in hand. You can access and modify (delete, edit, move, etc.) the file using its inode:
# edit the file with vim
user@sree ~/tmp/test % find ./ -inum 4195034 -exec vim {} \;
# remove the file
user@sree ~/tmp/test % find ./ -inum 4195034 -exec rm {} \;
# change the file name
user@sree ~/tmp/test % find ./ -inum 4195034 -exec mv {} repaired_file \;
# edit the file with vim
user@sree ~/tmp/test % find ./ -inum 4195034 -exec vim {} \;
# remove the file
user@sree ~/tmp/test % find ./ -inum 4195034 -exec rm {} \;
# change the file name
user@sree ~/tmp/test % find ./ -inum 4195034 -exec mv {} repaired_file \;
You can also change directory using inode.To get the inode numbers of the directories, you can use the command:
===============================
user@machine ~ % tree -a -L 1 --inodes /
/
├── [3276801] bin
├── [ 2] boot
├── [ 1026] dev
├── [1179649] etc
├── [3538945] home
├── [5505025] lib
├── [ 11] lost+found
├── [2752513] media
├── [3670017] mnt
├── [1703937] opt
├── [ 1] proc
├── [3145729] root
The inode number for the specified directory in the brackets. To access a directory use the command:
===========================
user@sree ~ % cd $(find / -inum 1572865)
or
user@sree ~ % find -inum 1572865 -exec cd {} \;
=====================================================
To remove unwanted inode usage by cpanel
======================
Just run the below script
====================
To find the inode usage per location (/ directory here)
for i in /*; do echo $i; find $i |wc -l; done
=====================
/usr/local/cpanel/bin/purge_dead_comet_files
=====================
By Sreejth Anchal