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 can leave whole patterns out of a disk usage total

Disk
~/tips/tip-disk-du-exclude-pattern

du -sh --exclude=node_modules --exclude='*.iso' ~/projects

Measures a folder without the parts you don't care about, like ISO files or node_modules.

Sometimes the question isn’t “how big is this folder” but “how big is it without the stuff you can download again”. du can skip files and folders by pattern.

du -sh --exclude=node_modules --exclude='*.iso' ~/projects

Every file or folder whose name matches the pattern is left out, and for a folder that means everything inside it too. You can repeat --exclude as often as you like. Leaving out a big ISO file or a node_modules folder can make that number a lot smaller.

I find that handy before a backup, to see how much space the real work takes once caches, build output and virtual machine images are gone. It’s also a quick way to check whether an exclude list does what you think: if the number doesn’t drop, the pattern didn’t match.

For a long list, put the patterns in a file, one per line, and use -X:

du -sh -X skip.txt ~/projects

Quote patterns with a star, or the shell expands them first. The pattern is matched against the name and the path du sees, which can be confusing with slashes; plain names like node_modules or *.iso are the predictable ones. And this is GNU du. The du on macOS has -I for a similar job, with different rules.

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