Shell One-Liner I

For some things a shell one-liner is not only scribbled faster than some clickedy-clicking clicked but also executes faster. One of these cases is the short question where the hell all of that hard drive space went to. You can start one of these very colorful GUI programs, for example. They are able to analyse you hard drive down to the last bit but they need to parse the whole drive to do that. That will need some time. Way too much time for such a simple task as to find the directory with the biggest load which is in most cases enough information to nail the culprit, e.g.:Videos/, Pictures/, Music/, .mozilla/, .chrome/, p0rn/ etc. A simple evocation of du -sm ~/*/ ~/.*/ | sort -n will do? Uhm, no, at least not in the way you would expect. The second wildcard ~/.*/ expands also to ~/../*/ and if you have several users on that machine it will take a long time and may even get you in trouble if it is not your own machine. Even without any other users it will need at least twice as long. To be on the save side of that spiel try the following:

du -sm ~/[^.]*/ ~/.[^.]*/ | sort -n

It asks du(1) for the sums (switch -s) of the directories and print the found amount in megabibytes (switch m). The two RegExes are for “all directories in the home directory not starting with a period” and “all directories in the home directory starting with one and only one period”. Pipe it to sort(1) and let is get sorted in ascending order with the digits sorted as numbers and not as characters (switch -n, also possible and working here are the switches -g and -h).
Tested with
GNU bash, Version 4.1.5(1)-release
Debian Squeeze
with unchanged kernel


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s