I’d love to write a bash script that helps me find opportunities for reducing disk usage.
The script would accept 2 arguments: a parent folder (which for me will usually be /apps/
), and a threshold (such as “200M”).
My current approach is not ideal (doesn’t use a threshold and shows a lot of redundant output).
Currently I run cd /apps/ && du -aBM 2>/dev/null | sort -nr | head -n 15
and see output like:
8975M .
1448M ./delta
1387M ./alpha
1350M ./alpha/releases
1144M ./bravo/releases
1144M ./bravo
1137M ./charlie
1117M ./delta/releases
902M ./alpha/releases/202210091311
871M ./charlie/releases
796M ./echo
794M ./echo/releases
791M ./alpha/releases/202210091311/node_modules
703M ./scoreboard
684M ./scoreboard/node_modules
I’d like the output to omit lines like:
8975M .
1448M ./delta
1387M ./alpha
1350M ./alpha/releases
1144M ./bravo
1137M ./charlie
902M ./alpha/releases/202210091311
796M ./echo
703M ./scoreboard
because those were a waste of my attention since the output above had also included subfolders of those folders that were above the threshold that I care about (200M).
These are the more interesting lines:
1144M ./bravo/releases
1117M ./delta/releases
871M ./charlie/releases
796M ./echo
791M ./alpha/releases/202210091311/node_modules
684M ./scoreboard/node_modules
I don’t think the du -aBM 2>/dev/null | sort -nr
approach is the right starting place to achieve my actual goal, though.
Because in reality, maybe any of those folders (in my most recent example) aren’t even as nested / deep as they could be (the lowest level subfolders that also satisfy the threshold of 200 MB).
For example, maybe /echo/subfolder1
and /echo/subfolder2
are each 300M.