Check Inode Counts with Formatting
Luke Hebert
- 3 minutes read - 502 wordsInode exhaustion is one of those silent failures that can take down a service long before disk space runs out. A quick per-directory inode count can point you straight at the culprit. The snippets below give you a fast, formatted view of where your inodes are going, plus a disk-usage summary for the top directories.
Count Inodes per Directory (Simple)
This loop walks each top-level directory and prints the file count:
for d in *; do echo -n "Directory: "$d": "Inode Count: "; find $d -type f | wc -l; doneCount Inodes with Formatted Output
A slightly more structured version that uses printf for tab-aligned columns and prints a running total:
for d in `find -maxdepth 1 -type d | cut -d\/ -f2 | grep -xv . | sort`; do c=$(find $d | wc -l); printf "$c\t\t- $d\n"; printf "Total: \t\t$(find $(pwd) | wc -l)\n"; doneThe grep -xv . filter removes the current directory (.) from the list so you do not get a duplicate entry.
Disk Usage for Top Directories (with Progress)
This one-liner uses du to report the largest directories, applies a nice/renice adjustment so it does not starve other processes, and shows a small spinner while it works:
many=25; niceness=-13; echo; on=$(nice); renice $niceness -p $$ 2>&1>/dev/null; echo -e "## [\e[1;36mspace/deep\e[0m] Top $many from \e[1;37m$PWD\e[0m"; c=0; echo -n "Please wait... " 1>&2; find -type d 2>/dev/null | while read stuff; do du -Ss ${stuff#\.\/} 2>/dev/null; let "c+=1"; case $c in 1) echo -ne "\b\\" 1>&2;; 25) echo -ne "\b|" 1>&2;; 50) echo -ne "\b/" 1>&2;; 75) echo -ne "\b-" 1>&2;; 100) c=0;; esac; done | sort -rn | head -n$many | awk '{total+=$1; i++; if(i==5){top5=total}; if($1/1024>=1) printf("\r\033[1;33m%\0478i\033[0m MB - %s\n", $1/1024, $2);} END{printf("\n\033[1;33m%\0478i\033[0m MB in top %i listed directories\n", total/1024, i); printf("\033[1;33m%\0478i\033[0m MB in top 5 listed directories\n", top5/1024);}' ; renice $on -p $$ 2>&1>/dev/null; echo;What it does, step by step
- Sets
many=25(number of top directories to report) andniceness=-13(a higher priority for the scan). - Saves the current nice value, applies the new one, and restores it when done.
- Walks every directory under the current path with
find -type d, runsdu -Sson each, and cycles a spinner character every 25 iterations. - Pipes the results through
sort -rn | headto keep only the largest entries. - Uses
awkto convert kilobytes to megabytes, highlight values in yellow, and print a summary of the top 5 and overall total.
The renice call temporarily raises the process priority. If you run this on a production host, make sure the brief priority bump will not interfere with time-sensitive workloads.
When to Use These
- Inode counts: Run the first two snippets when
df -ishows high inode usage and you need to identify which directory is consuming the most inodes. - Disk usage: Use the
duone-liner when you want a quick, human-readable breakdown of the largest directories without installing additional tools.
Both approaches are read-only and safe to run on live systems.