Skip to content

How to sort du -h command output by size

Question – How do I sort du -h command output by there sizes?

In the GNU Coreutils >= 7.5 package, sort command provides -h parameter allows to compare human-readable numbers (e.g., 10K 15M 1G etc). This helps up to compare the results of `du -h` and short them.

du -h * | sort -h

The above will show the results in the ascending order by size. You can reverse this using -r to show results in descending order.

du -h * | sort -rh

15M     btmp.1
7.2M    apache2
2.2M    auth.log.1
1.9M    btmp
1.5M    auth.log
1.3M    redis
656K    letsencrypt
468K    auth.log.4.gz
264K    auth.log.2.gz
260K    cloud-init.log
240K    auth.log.3.gz
168K    mail.log.1

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top