How to get the size of folders sorted by size

The du command returns the size of a directory in kb and human readable, it can be piped for sorting.

Run the following command:


 du -sk * | sort -nb -r | awk '{ print $2 }' | xargs du -h
 

The command first checks the size of the folder in kilobytes, then sort the folder according to the size descending. Then it prints the folder name and check the folder size in a human readable way.

The output will be:


[olivier@web1 test]# ls -al
total 20
drwxr-xr-x 5 olivier olivier 4096 Aug  3 02:59 .
drwxrwxrwt 5 olivier olivier 4096 Aug  3 03:08 ..
drwxr-xr-x 2 olivier olivier 4096 Aug  3 03:00 folder1
drwxr-xr-x 2 olivier olivier 4096 Aug  3 03:00 folder2
drwxr-xr-x 2 olivier olivier 4096 Aug  3 03:02 folder3
[olivier@web1 test]#  du -sk * | sort -nb -r | awk '{ print $2 }' | xargs du -h
20K     folder3
16K     folder1
12K     folder2
[olivier@web1 test]#

References:

Wikipedia

Recent Comments