Recently I had to fix a linux system whose root file system was allowed to fill up. Not having much knowledge of the system, I needed a quick way to find out which files were consuming the most space to see if any of those could be purged. The likely culprit was some huge log files, but how to find them?
du and sort to the rescue!
This simple command dumped out the sizes of all files on the system:
du -x / | sort -rn | more
Let’s pick it apart:
The du command is used to summarize disk usage of each file in a file system, recursively for directories.
The -x option tells the du command to skip directories on different file systems. In our case, the root file system is full, and so we don’t care about the file sizes on other file systems.
After the options we tell du the directory to start at, which is “/”, the root directory.
The sort command is used to sort lines of text.
The -r option sorts in reverse order, so what would normally be at the bottom of the output is now at the top. For a numeric sort, the largest numbers are at the top.
The -n option sorts in numeric mode, as opposed to the default lexicographic sort.
The more command is a filter for paging through text one screenful at a time.