Sub topic 6
Finding large files and directories with millions of files
This chapter covers the step after df shows a problem: finding what is using the space or the inodes. It assumes you can read df -hT and df -ih output, understand that blocks and inodes are separate limits, and are comfortable with pipes, sudo, and find basics.
Start from the filesystem, not the whole disk
df tells you which mount point is full. Every search after that should stay on that one filesystem. Two tools make this easy:
du -xdoes not descend into directories that sit on other filesystems.find -xdevdoes the same forfind.
Without these options, a search from / also walks /proc, network mounts, and other volumes. That wastes time and can point you at the wrong place. A large file on /data will not help you if / is the full filesystem.
The usual approach is to look at the top level first, then repeat inside the biggest entry until you reach the cause.
Drilling down with du
This command summarizes each directory directly under /var and shows the ten largest:
sudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -n 10Each part has a job:
-xstays on one filesystem.-hprints sizes like4.2G.--max-depth=1prints one line per immediate subdirectory, plus a total for/varitself.2>/dev/nullhides "permission denied" and "no such file" noise. Withoutsudoyou would see partial totals, so keepsudofor system directories.sort -hsorts human-readable sizes correctly. A plainsort -nwould put900Mabove2G.
The total line for /var is always last because it is the largest. Pick the biggest child, for example /var/log, and run the same command on it. Repeat until the numbers stop being spread across many directories and one name stands out.
What du measures
du reports disk usage, which is the number of blocks allocated to each file. This differs from the file's apparent length in two cases:
- A sparse file, such as a VM disk image, can claim 100 GB but use only a few GB of blocks.
dushows the blocks.ls -lshows the claimed length. - A tiny file still uses a whole block. Ten million 10-byte files can use tens of gigabytes.
du --apparent-size shows the length instead. Use the default when you are asking "what is filling the disk?"
Blind spots
- If a mount point hides files,
du -xwill not see them. Suppose data was written to/var/lib/appbefore a separate volume was mounted there. The old files still occupy space on the parent filesystem, but they are invisible under the mount.duof the parent will not account for them, anddfwill still count them. To inspect them, bind-mount the parent somewhere else:sudo mount --bind / /mnt/rootviewand runduon/mnt/rootview/var/lib/app. Unmount it afterward. - If
dutotals are well belowdf, the difference is usually deleted files that a process still holds open. That case has its own chapter.
Finding large files with find
du shows directories. When you want individual files, use find with a size test:
sudo find / -xdev -type f -size +500M -exec ls -lh {} + 2>/dev/null-xdevkeeps the search on the root filesystem.-type flimits it to regular files.-size +500Mmatches files larger than 500 MiB. The suffixesk,M, andGare powers of 1024. Sizes are rounded up to the next unit before comparing, so the test is slightly coarse near the boundary.-exec ls -lh {} +passes many files to eachlscall. It is faster than\;, which runs one process per file, and it handles spaces in names safely.
The output is not sorted. If you have many matches, sort by the size column:
sudo find / -xdev -type f -size +100M -exec ls -l {} + 2>/dev/null | sort -k5 -n | tail -n 20Here the plain -l output keeps the size in bytes so sort -n works. Column 5 is the size in the default ls -l format.
Typical results on a server:
| Finding | Usual meaning | Likely next step |
|---|---|---|
| Huge file in `/var/log` | Log never rotated or a runaway error loop | Fix the cause, then rotate |
| Large `.gz` or `.tar` backups in `/home` or `/tmp` | Manual backups that were never removed | Move off-host, then delete |
| Core dumps (`core.*`, files in `/var/lib/systemd/coredump`) | Crashing process | Investigate the crash, then clear old dumps |
| Large database or VM image | Normal data | Plan capacity, do not delete |
| Files under `/var/lib/docker` | Image layers or container logs | Use the container tool's own cleanup |
Finding directories with millions of files
When df -i shows high inode use but du shows little space, the problem is the file count. Many small files use many inodes and almost no blocks, so du -h will not find them.
The simplest approach is to count entries under each directory and rank them:
for d in /var/*; do
printf '%s %s\n' "$(sudo find "$d" -xdev 2>/dev/null | wc -l)" "$d"
done | sort -n | tail -n 5How it works:
- The loop visits each entry directly under
/var. find "$d" -xdevprints every path below it, one per line, andwc -lcounts them.sort -n | tail -n 5shows the five largest counts.
The quotes around "$d" protect names with spaces. Run the same loop on the winning directory, for example for d in /var/spool/*, until you find the one directory that holds the files.
With GNU coreutils 8.22 or later, du can count inodes directly, which is shorter and avoids the loop:
sudo du --inodes -x --max-depth=1 /var 2>/dev/null | sort -n | tail -n 5--inodes makes du report the number of inodes used rather than blocks. Check du --help if your system is older and the option is missing.
Caveats for counting
- Counting a directory with millions of entries can take minutes and generates heavy disk reads. On a loaded production server, run it with
nice -n 19 ionice -c3in front of the command so it yields to other work. wc -lcounts lines. A filename containing a newline counts as two. This is rare and does not matter for finding the big directory.- Hard links: several names for one file share one inode. A count of paths can therefore be higher than the inodes actually used. This matters in backup trees built with
rsync --link-dest. Usedu --inodes, which counts each inode once, to check these. lson a directory with millions of entries is slow and uses a lot of memory because it sorts. Usels -forfindinstead.
Putting it together on an example
Suppose df -ih shows / at 98% inode use, while df -h shows 40% space use. Start at the top of the filesystem:
sudo du --inodes -x --max-depth=1 / 2>/dev/null | sort -n | tail -n 5If /var is the largest, repeat inside it, then inside /var/spool, and so on. You might end at /var/spool/postfix/deferred with 4 million entries. That is a mail queue, and the cause is a mail problem, not a disk problem. The fix is to find out why mail is not being delivered, not to delete the queue blindly.
Exercise
A root filesystem shows 95% inode use but only 30% space use. Which command ranks the directories under /var by number of files, staying on one filesystem, and why would du -h not find the cause?
Show answer
Use sudo du --inodes -x --max-depth=1 /var 2>/dev/null | sort -n | tail -n 5, or the find -xdev | wc -l loop. du -h reports blocks, and millions of tiny files use very little space, so they do not stand out in a size listing.
Exercise
You run sudo find / -xdev -type f -size +500M and find nothing, yet / is nearly full by space. Name two reasons the large usage might not appear.
Show answer
The space may be spread across many files smaller than 500 MB, so lower the size threshold or use du to rank directories. Or files may be hidden under a mount point, or deleted but still held open by a process, so no path matches.