Finding The Largest Directories on Linux

Finding The Largest Directories on Linux

Managing disk space on a Linux machine is a critical aspect of system administration. As data accumulates over time, it’s essential to identify which directories are consuming the most space. One powerful command that can help you with this task is ‘du’ (disk usage) in combination with ‘sort’. In this article, I’ll go over how to use the ‘du’ command along with ‘sort’ to find the largest directories on a Linux machine.

To appease the people who just want to see the command they want to use, here you go:

du -a / | sort -n -r | head -n 10

Now for the one’s who want to understand what this command is doing, keep reading.

We start with the du command, which is for disk usage. Using du -a tells it to count all files, not just the directories. This lets it go recursively through all the subdirectories to find the size of every file. Then we’re specifying the location we want it to start searching from, in this case the root directory. For instance, if you wanted to search the /var/log directory, you would use du -a /var/log.

The next step is to pipe the results from the disk usage command into sort. We use the -n and -r flags to indicate we want to sort the results numerically in reverse order. So this will leave the largest folders from the disk usage command at the top of the results.

Next we can grab the results by piping the results from sort to the head command. We don’t want the entire result set since it’ll likely be too much to read through, so we can specify how many results we want to grab with head -n 10.

And that’s all there is to it. You’ll end up with output that looks something like this:

Leave a Reply