Linux: Sort du -h (human-readable) Output By Size
last updated March 19, 2013 in CategoriesBASH Shell, Debian / Ubuntu, Linux, Ubuntu Linux
Ihave 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?
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.
sort syntax
The syntax is:
command | sort -h
To sort du command output in human readable format by size, enter:
du -h | sort -h du --human-readable | sort --human-numeric-sort |
Sample outputs:
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 |
See also
- If you are not using the latest version of the gnu/sort command, try commands listed on this page for more information.
- man pages – sort and du command.
[root@localhost mnt]# du -h
3.5G .
[root@localhost mnt]# ls -alh
total 3.5G
drwxr-xr-x. 2 root root 86 Oct 11 22:42 .
dr-xr-xr-x. 18 root root 4.0K Apr 14 03:34 ..
-rw-r--r--. 1 root root 0 Oct 11 22:41 file1
-rw-r--r--. 1 root root 0 Oct 11 22:41 file2
-rw-r--r--. 1 root root 0 Oct 11 22:41 file3
-rw-r--r--. 1 root root 0 Oct 11 22:41 file4
-rw-r--r--. 1 root root 0 Oct 11 22:41 file5
-rw-r--r--. 1 root root 3.5G Oct 11 22:49 usr.tar
[root@localhost mnt]#