One day you can have such a problem. After trying to upload a file to a Linux Web-server you can receive the error: “No Space Left On Device”. Although there is free disk space.
Or maybe your server just started to show strange behavior: you can see freezes, and sometimes a server does not respond.
One of the possible reasons is the end of free memory for inodes.
What is an Inode?
The inode is a special data structure, which describes files and directories in Unix-style file systems. Inodes contain all meta information about files and all data about files’ location in disc block space. Usually, inodes have a size of around 1 percent of the whole available memory.
And there are cases when inodes can run out faster than the rest of the memory.
Why can inodes run out?
For example, in the code of your website, there is the function call: session_start().
Because of this, many sessions are created and they are not closed. So, a huge number of tiny files that contain information about sessions will be stored on the server. And one day they will run out of memory for inodes.
How to check free space
If you want to check the current status of space for inodes do the following:
- Connect to your server via SSH
- Execute the command: df -hi
After that, you will see a table. If your disk is not completely full, but here you see that the size of the used memory is 99% or 100% (IUse% column), then the problem is related to the inodes.
Finding a directory with files
We need to find a directory where there is the huge number of files. To do this you need to use the following command.
find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n
This command prints a sorted list of directories from your server with quantities of files in each folder.
Here at the bottom of the image, we see the directory with the largest number of files. This directory contains information about sessions. We can understand it by the folder path. Now we can safely clear the directory with sessions.
How to clean up a directory
To do that we need to go to the desired directory by using the command cd.
After that use this command to start cleaning the current folder:
ls -f . | perl -pe 'select(undef,undef,undef,0.01)' | xargs -t -n 50 rm
Because the number of files is huge it can take a lot of time.