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

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

 

last updated March 19, 2013 in CategoriesBASH ShellDebian / UbuntuLinuxUbuntu 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

 

[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]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值