Disk usage statistics
Or after dusk a star comes out...
Disk space is like cupboard storage space: you can never have enough. In both cases you can suddenly find that those empty drives and shelves get filled up pretty quickly, but while it is relatively easy to see the big boxes taking up all your shelf space, it is often harder to quickly spot where your disk space has gone.
The situation is more fun when a remote server suddenly reports that it is rapidly approaching having zero disk space available. So, what’s a quick way of seeing where that space has gone? Enter df for disk free status and du for disk usage statistics.
A quick way to see the disk free status is df -lh which reports on locally mounted filesystems, and displays the amount used in ‘human-readable’ sizes.
david$ df -lh Filesystem Size Used Avail Capacity Mounted on /dev/disk0s3 77G 72G 4.0G 95% / /dev/disk1s3 77G 27G 50G 35% /Volumes/Hd2
So once you’ve confirmed that your drive is nearing 100% capacity, how do you quickly discover where the space has gone? First navigate to whichever directory you want to start the detective work in and du -sk * will list the sizes of the contents of that directory.
david$ du -sk * 5423152 Deliver 20072 Design 502224 Develop 984 Discuss 377140 Support 7968 Train
The flag -s displays an entry for each specified file or folder, the -k displays the size in 1Kb blocks, and the * indicates that each file and directory in the current directory should be displayed. My mnemonic for remembering the command du -sk * in moments of panic when the drive is at 100%, and everything is in danger of falling apart, is that “after dusk a star comes out”.
I prefer the use of 1Kb blocks for the sizes, rather than -h which uses ‘human-readable’ units, simply because it is easier to spot the big files or directories from the digits alone without having to interpret the units. As an added enhancement, pipe the result of du through sort to, well, sort the listing, using the -n flag to sort numerically: du -sk * | sort -n
du -sk * | sort -n 984 Discuss 7968 Train 20072 Design 377140 Support 502224 Develop 5423152 Deliver
And in case you’re wondering: the cause of the zero free disk space on the server? A run-away log process that was generating multiple copies of extremely large logs.







Post your own comment here:
Note: be friendly! We reserve the right to moderate inappropriate, offensive, or wildly off-topic comments. Your email address is required, but won't be displayed. All comments are moderated, so don’t expect them to show up instantly.