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: du -ch adds up the disk space of only the files you pick

Disk
~/tips/tip-disk-du-ch-total

du -ch /var/log/*.log | tail -n 1

Gives one total for a set of matching files, like all logs or all ISO images.

du -sh on a folder gives the size of everything in it. For the size of just the logs, or just the ISO files, let du add them up.

du -ch /var/log/*.log | tail -n 1

-c adds a line called total at the end, and -h makes the numbers readable. tail -n 1 keeps only that line. With two log files, the output ends in a line like 492K total.

Combined with find, it works across a whole tree:

find /srv -name '*.iso' -print0 | du -ch --files0-from=- | tail -n 1

--files0-from=- reads the NUL-separated file list from the pipe, so names with spaces are fine and there’s no limit on how many files you can pass.

Two things can make the number look wrong. du counts disk blocks, not bytes, so many tiny files add up to more than their real size; I’d add --apparent-size if you want the byte count. And with a glob, a file that matches twice, or hard links to the same file, are counted only once, which is usually what you want for disk space but can confuse you when comparing with a file manager. Also, a pattern with thousands of matches can hit “Argument list too long”; that’s when the find version helps.

Further reading: du invocation, GNU coreutils, du(1) manual page

More Disk tips sudo e2freefrag /dev/sdb1 sudo smartctl -a --json /dev/sda | jq '.smart_status.passed' lsscsi -s udisksctl power-off -b /dev/sdX du -sh — .[!.]* * 2>/dev/null | sort -h All Disk tips (44) All topics

Link