Question

How to sort the output of the du -h command by size?

When troubleshooting any disk space problems on a server or just trying to find the largest directories, the du command is essential!

You can use the du command which estimates the directory space usage.

For example, let’s say that we want to check the size of the directories located in the /home directory. To do that let’s first cd into the /home folder:

  1. cd /home/

And then run the du command:

  1. du -h --max-depth=1
[secondary_label Output]
16K ./demo
5.0G ./bobby
28K ./sammy
1.8G ./dev
7.1G .

I like running the du command with the --max-depth=1 argument which only shows the size of the folders located inside the current directory and not the subfolders.

As you can see from the output the size is not sorted by size, here is how to do that!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
December 10, 2020
Accepted Answer

What you could usually do in order to sort values in Linux is to use the sort command.

However, if you pipe the output of the du -h command directly into sort, it will not take into consideration the human-readable size format.

Lukily sort comes with a -h argument as well which would take the human-readable format into consideration and will compare the human-readable numbers.

So the full command would look like this:

  1. du -h --max-depth=1 | sort -h

For more information you can take a look at the official documentation here:

https://linux.die.net/man/1/sort

Hope that this helps! Regards, Bobby

alexdo
Site Moderator
Site Moderator badge
October 12, 2023

Heya,

There is also a ncurses interface for du, appropriately called ncdu, that you can install:

  1. sudo apt install ncdu

This will graphically represent your disk usage:

  1. ncdu
Output--- /root ----------------------------------------------------------------------
    8.0KiB [##########] /.ssh
    4.0KiB [#####     ] /.cache
    4.0KiB [#####     ]  .bashrc
    4.0KiB [#####     ]  .profile
    4.0KiB [#####     ]  .bash_history

You can step through the filesystem by using the up and down arrows and pressing Enter on any directory entry.

Hope that this helps!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel