Linux Sort du -h (human-readable) Output By Size

See all GNU/Linux related FAQ
I have a large number of files stored in ~/Downloads/ directory. How do I sort and print sizes in human readable format using du -h command under Ubuntu Linux LTS version 12.04 or any other Linux distributions? How can I sort du outputs by size on macOS or FreeBSD?

You can pass the -h or --human-numeric-sort option to the sort command to sort and compare human readable numbers such as 2K, 300M, 1G and more. This is a new option added the gnu/sort command. Let us see how to use sort du -h command (human-readable) output by size on Linux or macOS or FreeBSD Unix systems.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category File Management
OS compatibility AIX AlmaLinux Alpine Arch Debian Fedora FreeBSD HP-UX Linux macOS Mint NetBSD OpenBSD openSUSE Pop!_OS RHEL Rocky Stream SUSE Ubuntu Unix WSL
Est. reading time 3 minutes
Advertisement

The sort du -h (human-readable) command syntax

Here is the syntax for Linux. Open the Terminal app and type:
$ du -h
However, the syntax is as follows for GNU or BSD (macOS) sort command when you need to use with the du command:
command | sort -h
du -h | sort -h

The “Human-readable” output means du command would use unit suffixes such as based on powers of 1024:

  1. Byte
  2. Kilobyte
  3. Megabyte
  4. Gigabyte
  5. Terabyte
  6. Petabyte

Sorting using human readable numbers format outputs

To sort du command output in human readable format by size, enter:
$ du -h | sort -h
$ du --human-readable | sort --human-numeric-sort

Fig.01: du | sort in action

Fig.01: du | sort in action

To reverse the result of comparisons pass the -r option:
$ du -h | sort -h -r
$ du --human-readable | sort --human-numeric-sort -r

To see top 10 files pass the output to the head command, enter:
$ du -h | sort -h | head
$ du -h | sort -hr | head
$ du --human-readable | sort --human-numeric-sort | head
$ du --human-readable | sort --human-numeric-sort -r | head

A note about sorting du outputs on macOS or FreeBSD Unix

The syntax is as follows:
$ du -h | sort -h
$ du -h | sort --human-numeric-sort
$ du -h | sort --sort=human-numeric

Outputs from my FreeBSD 13 system:

512B	./.config/lftp
6.5K	./.config/htop
 13K	./.local/share/lftp
 13K	./.ssh
 14K	./.local
 14K	./.local/share
 17K	./.config/neofetch
 25K	./.config
 11G	./nixcraft-wks01-feb-2022
 31G	./nixcraft-wks01
 42G	.

See How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD for more info.

Summing up

You learned how to sort du command outputs using the sort command on Linux, macOS, and FreeBSD Unix machine. The du and sort commands have many more options. Hence, use the man command or help command to read them. For instance:
$ man sort
$ man du
$ du --help
$ sort --help

See also:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

14 comments… add one
  • agentooer Jul 28, 2014 @ 4:01

    It seems `sort -h` doesn’t work with sort 8.4.
    it works with sort 8.21.

  • dan levit Mar 31, 2016 @ 8:34

    Hi every one ,i’m new in Unix and i try to write the following script in csh
    i want to find all the dirctorys that start with the same name lets say “Jonson”
    and the output should be a sorted list :
    name size date
    in this order( the date means the last time the current directory was modified) .
    and the output should be sorted by the size of the the directory from small to big ..

    I’m pretty lost ..

    thanks .

  • Igors Feb 11, 2022 @ 15:48

    Thank You very much! Simple but wasn’t obvious for me :-)

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.