Sub topic 6
Finding Large Files and Directories with Millions of Files
Assume you can use a Linux shell, understand files and permissions, and have basic experience with sudo, pipes, and shell variables. This chapter shows how to find both block-space consumers and inode consumers. A directory can contain millions of small files while using less space than one large database file.
Start with the filesystem boundary
Use df first to identify the full filesystem. Then use du to inspect directories on that filesystem. The -x option, also written -xdev in find, prevents the search from crossing into another mounted filesystem.
df -hT
sudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -n 10du measures space attributed to files and directories. It does not explain every byte reported by df; deleted files still held open by processes are one important exception.
The --max-depth=1 option reports each directory directly below /var. Sorting by human-readable values with sort -h makes the largest entries appear last.
| Command | Best use | Important limitation |
|---|---|---|
| `df -hT` | Compare total filesystem space | Does not identify individual directories |
| `du -xh` | Find directories using blocks | Can be slow and may not match `df` |
| `find -size` | Locate individual large files | Does not find many-small-file inode usage |
Find large files
After identifying a filesystem, search it without crossing mount points:
sudo find / -xdev -type f -size +500M -exec ls -lh {} + 2>/dev/nullThis finds regular files larger than 500 MiB on the root filesystem. Change / to a more focused directory when possible. Searching /var is usually faster than searching the entire root filesystem.
The command hides permission errors. Do not interpret an empty result as proof that no large files exist if the account could not read all directories.
Find directories with many entries
When inode usage is high, file size is not the right first measure. Count directory entries instead:
for d in /var/*; do
printf '%s %s\n' "$(sudo find "$d" -xdev 2>/dev/null | wc -l)" "$d"
done | sort -n | tail -n 5This counts entries below each immediate child of /var and prints the five highest counts. The count includes directories, so it is an indicator rather than an exact inode total.
Common inode-heavy locations include mail queues, temporary session files, package caches, application uploads, and poorly rotated logs. A cache containing millions of tiny files can exhaust inodes while df -h still shows free gigabytes.
Follow the evidence
Use a narrowing process:
For a suspected directory, inspect ownership and timestamps before changing anything:
sudo du -sh /var/cache/example
sudo find /var/cache/example -xdev -type f -printf '%TY-%Tm-%Td %TH:%TM %s %p\n' \
2>/dev/null | sort | head -n 20The second command shows older files first by date text only when the date format sorts chronologically. Test a sample before using a deletion command. Prefer the application's documented cleanup command or package-manager cache cleanup over manually removing active data.
Exercise
A filesystem has 90% inode usage but only 35% space usage. Which investigation should come first, and why?
Show answer
Count entries under likely directories with find and wc -l. Many small files consume separate inodes even when their combined block usage is modest.