Software >> OS >> Unix >> Solaris >> How to list files and sort by size

 

To sort in ascending order of bytes

bash-3.2# ls -l | sort -nk5
dr-xr-xr-x   1 root     root           1 Feb 18 22:09 home
dr-xr-xr-x   1 root     root           1 Feb 18 22:09 net
drwxr-xr-x   2 root     root           2 Mar 22  2013 Documents
drwxr-xr-x   2 root     root           2 Mar 22  2013 rpool
drwxr-xr-x   3 root     sys            3 Mar 22  2013 export
drwxr-xr-x   3 root     sys            3 Mar 26  2014 mnt
drwxr-xr-x   4 root     root           4 Mar 22  2013 system
drwxr-xr-x   4 root     nobody         5 Aug 17  2014 cdrom
drwxr-xr-x   5 root     sys            5 Mar 22  2013 platform
drwxrwxr-x   5 root     other          5 Apr 27  2014 app
drwxr-xr-x   2 root     root           9 Apr 27  2014 Desktop
.....

 

To sort in descending order of bytes

bash-3.2# ls -l | sort -nrk 5
dr-xr-xr-x  86 root     root      437952 Feb 18 22:17 proc
drwxrwxrwt   8 root     sys          671 Feb 18 22:11 tmp
dr-xr-xr-x   6 root     root         512 Feb 18 22:09 vol
drwxr-xr-x  19 root     sys          330 Feb 18 22:09 dev
drwxr-xr-x  87 root     sys          250 Feb 18 22:09 etc
drwxr-xr-x   8 root     bin          243 Mar 24  2014 lib
drwxr-xr-x  41 root     sys           55 Mar 26  2014 usr
drwxr-xr-x   2 root     sys           54 Mar 22  2013 sbin
drwxr-xr-x  49 root     sys           49 Mar 26  2014 var

.....

 

-n => key treated as numeric value
-r => reverse order
-k => column number of the sort key in this case size of the file in bytes