Using ‘ducks’ to solve VPS disk space issues

Most use cases for a VPS, like running a website, aren’t all that demanding when it comes to hard drive storage space, but there are some obvious exceptions—hosting audio/video media, backups, large-scale user databases, and more.

For those who might run into issues with hard drive space, keeping tabs on what’s using the most isn’t a bad idea. It just might give you some visibility into what you need to rm before you hit the limit.

That’s when it’s time to bring in the “ducks.”

The df command (short for disk free) displays the amount of disk space available on every partition that your user has read access to. (You can also use the -h argument to makes the output “human readable,” as in display file usage in KB, MB, and so on.)

$ df
/dev/xyz            30828540 3006136  26249748  11% /
devtmpfs            2097152       0   2097152   0% /dev
tmpfs               2097152       4   2097148   1% /dev/shm
tmpfs               2097152   58500   2038652   3% /run
tmpfs                  5120       0      5120   0% /run/lock
tmpfs               2097152       0   2097152   0% /sys/fs/cgroup
none                2097152       0   2097152   0% /run/shm

In contrast, the du program (short for disk usage) estimates usage under a directory or file system that you specify. By default, it returns disk usage in KB.

$ du *
7831    some-file.tar.gz
23441   directoryA/subdirectoryY
183641  directoryA/subdirectoryZ
237189    directoryA/
464 directoryB/
226     directoryC/subdirectoryX
832     directoryC/subdirectoryY
483     directoryC/subdirectoryZ
1541  directoryC/

In most cases, the output will be far lengthier than this example. And that’s where you can bring in the the “ducks” to help you investigate a little further.

$ du -cks *
7831    some-file.tar.gz
237189  directoryA/
464 directoryB/
1541  directoryC/
247026  total

The -c argument displays the total usage, -k sets the block size to 1024 bytes, and -s displays a summary for only the current directory, and not any subdirectories.

Now it’s possible to more readily see that directoryA is the largest disk hog.

You can take du -cks a little further by piping it into a few other commands, like sort and head. The following command will sort the results from largest to smallest, and reduce the output to only the 10 largest.

$ du -cks * |sort -rn |head -11

Some people like to take this a step further and create an alias for this command. You can drop the following in your .profile or .zshrc file.

alias ducks='du -cks * |sort -rn |head -11'