Sorting and Analyzing Disk Usage
Now that we've identified the subdirectories using the most space, let's learn how to sort the results. This will help us quickly identify the largest consumers of disk space.
We'll use the sort
command in combination with du
. Don't worry if you're not familiar with sort
- we'll explain how it works.
First, let's sort the output of du
by size:
du -h | sort -h
This command does two things:
du -h
: Runs the disk usage command with human-readable output
|
: This is a pipe. It takes the output of the command on the left and feeds it as input to the command on the right.
sort -h
: Sorts the input numerically based on human-readable sizes
You might see output like this:
0 ./archive
0 ./system
5.0M .
5.0M ./application
The output is sorted from smallest to largest. But often, we're more interested in the largest directories first. To reverse the order, we can add the -r
option to sort
:
du -h | sort -hr
Output:
5.0M ./application
5.0M .
0 ./system
0 ./archive
Now we can clearly see which subdirectories within the logs folder are using the most space, in descending order.
To focus only on the immediate subdirectories and sort them, we can combine the techniques we've learned:
du -h --max-depth=1 | sort -hr
This command will show and sort only the immediate subdirectories of the current directory.
Remember, the power of the command line comes from combining simple commands to perform complex operations. We've just combined du
, sort
, and various options to quickly analyze disk usage!