What is eating my disk space? Disk Usage & Inode usage Investigation on Redhat Linux server

Servers will run out of space, whether it be due to inode usage or actual disk space due to file sizes. Inodes are the amount of files that your disk can handle, while the disk space is how much actual space the disk capacity can contain.

Here are two different find commands to help investigate these issues. You should only be removing what you know is OK to remove, usually log files, old backups, or directories with millions of useless files due to some cron command that is running over and over. If it is the later where a cron command is causing the issue the cron command should be fixed after the directory is cleared. Also if you do find a directory with millions of files you may have to do multiple find commands to actually remove the files because the rm command won’t be able to handle the amount of arguments.

Here is the first command.

find / -type f ! -path "/home/virtfs/*" ! -path "/proc/*" ! -path "/run/*" ! -path "/sys/*" ! -path "/backup/*" -size +50M -exec ls -lh {} \; | awk '{print $4" "$5" "$6$7"\t"$8" "$9" "$10" "$11" "$12}'

This command above is looking for files larger than 50MB and prints them out, including the user of the file, and the location. You may want to run these commands in a screen session because these commands can often take hours to run on overloaded servers.

Here is the second command.

for drec in `find / -type d ! -path "/proc/*" ! -path "/run/*" ! -path "/sys/*" -size +140k`; do echo $drec >> /home/inodes.txt &&echo "Expected files for ~8KB/File" >> /home/inodes.txt && stat $drec | grep Size | awk '{print $4*7}' >> /home/inodes.txt && echo "Actual files" >> /home/inodes.txt && find $drec -type f | wc -l >> /home/inodes.txt && echo -e "\n" >> /home/inodes.txt ; done

The second command runs a for loop which prints out directories over 140KB in size to a text file /home/inodes.txt and the amount of files in those directories. This is extremely useful for finding directories that are taking up all the inode usage, such as email directories. Usually these directories when they house millions of files will be hundreds of GB in size as well, which makes sense if you do the math, 1 million * 8KB a file is 8GB of space used. Also 8kB is on the small side of a file size.

Both of these commands are completely safe to run. Make sure you are competent when clearing or removing large files / amounts of files.

Here is my youtube video showing these commands in action.

https://youtu.be/dqJiDx6DwwA

Consider supporting me on twitch if you found this useful.
https://twitch.tv/djrunkie

Leave a Reply

Your email address will not be published. Required fields are marked *