When Sorting Fails

When investigating disk usage issues on a very full disk, the investigation may actually be hampered by the lack of disk space. For example:

du -x -m / | sort -nr | head -25

can fail if you have a lot of directories and your disk is completely full: sort will only process "so much" before it resorts using a temporary file, and it cannot create a temporary file because the disk is full!

Here we need to resort to an often overlooked option on the sort command: --buffer-size: this will force it to allocate a memory buffer of the given size - just make sure that the buffer is big enough to contain all of the output from du:

du -x -m / | sort -nr --buffer-size=50M | head -25

50 Mb should be enough for most cases. If your server also so short on memory that this is impossible: You're SOL!

Another way of working around this is to direct the sort command to place its temporary files somewhere else, by setting the TMPDIR environment variable:

export TMPDIR=/path/to/dir
du -x -m / | sort -nr | head -25

But this assumes that you have some free diskspace somewhere...