brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find -printf '%h' counts files per folder

Find
~/tips/tip-find-printf-h-count-per-folder

find /var -xdev -type f -printf '%h\n' | sort | uniq -c | sort -rn | head

Shows which folders hold the most files, for finding the place with a million cache files.

A disk can run out of inodes or a backup can crawl because one folder holds a huge number of small files. To find that folder, count files per folder.

find /var -xdev -type f -printf '%h\n' | sort | uniq -c | sort -rn | head

%h prints the folder a file lives in, without the file name. Every file gives one line with its folder, sort | uniq -c counts how often each folder appears, and the last sort puts the busiest folder on top. That gives lines like 3 /t/a and 1 /t/a/b.

This counts files directly inside each folder, not in its subfolders. That’s usually what I’d want, because it points at the exact folder that’s full, like a session or thumbnail cache, instead of a parent folder with a big total.

Two notes. Paths with a newline in them will mess up the count, since everything works per line; on normal systems that’s rare. And on a tree with millions of files, the sort step needs memory and temporary space. If /tmp is small, point sort somewhere else with sort -T /path/with/space.

Further reading: find(1) manual page, GNU findutils manual

More Find tips find / -maxdepth 1 -fstype proc find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find /srv -xdev -samefile /srv/downloads/movie.mkv All Find tips (46) All topics

Link