October 12, 2020

OpenBSD: Keep track of the size of your files

OpenBSD is an intelligent, smart OS but unfortunately the same does not apply to all software out there which you can install on your computer. I recently found out thanks to the script that I post in this page that there was a mysterious .cache in my /home taking as much as about 30G.

The following script should work for any UNIX-like OS, but I am not sure. It is fast and allows you to find out where the potential memory eater is.


#!/bin/sh
echo""
echo "Non-hidden files and directories"
echo "--------------------------------"
du -sk ./* | sort -n | awk 'BEGIN\
                                 { pref[1]="K"; pref[2]="M"; pref[3]="G";} \
                                 { total = total + $1; x = $1; y = 1; while( x > 1024 )\
                                 { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); }\
                             END\
                                 { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; }\
                             printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'
echo""
echo "Hidden files and directories"
echo "----------------------------"
du -sk ./.* | sort -n | awk 'BEGIN\
                                  { pref[1]="K"; pref[2]="M"; pref[3]="G";} \
                                  { total = total + $1; x = $1; y = 1; while( x > 1024 )\
                                  { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); }\
                              END\
                                  { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; }\
                              printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'