ls command sort by file size [Linix/UNIX/macOS/bash]


You need to make use of -S option in order to sort the list of files using ls command based on file sizes. -S option will sort all the result in the ls command based on size of the files/dir in greatest to lowest. If you wish to display the list in reverse order smallest to bigger file size you can use reverse you can add -r. example ls -lSr

Syntax: ls -lS

Example:

Without sorting by file size

$ ls -l
total 32
drwxr-xr-x    1 root root 87188 Aug 18 09:54 bin
drwxr-xr-x    2 root root  4096 Apr 24  2018 boot
drwxr-xr-x    1 root root  4096 Aug 12 20:40 abc
drwxr-xr-x    5 root root   360 Aug 22 10:57 dev
drwxr-xr-x    1 root root  4896 Aug 18 10:47 etc
drwxr-xr-x    1 root root   232 Aug 18 11:17 ext
drwxr-xr-x    1 root root  4096 Aug 12 20:40 home
drwxr-xr-x    1 root root 17526 Aug 18 09:53 lib
drwxr-xr-x    1 root root  2422 May 27 12:25 lib32
drwxr-xr-x    1 root root    40 May 25 12:51 lib64
drwxr-xr-x    1 root root  2544 May 27 12:25 libx32
drwxr-xr-x    2 root root  4096 Jul 13 14:48 media
drwxr-xr-x    2 root root  4096 Jul 13 14:48 mnt
drwxr-xr-x    1 root root    64 Jul 30 12:41 opt
dr-xr-xr-x 1045 root root     0 Aug 22 10:57 proc
drwx------    2 root root  4096 Jul 13 14:49 root
drwxr-xr-x    1 root root  4096 Aug 12 20:40 run
drwxr-xr-x    1 root root  8994 Aug 18 09:53 sbin
drwxr-xr-x    4 root root  4096 Aug 22 10:57 secrets
drwxr-xr-x    1 root root     0 Apr 23 11:06 srv
dr-xr-xr-x   13 root root     0 Aug 22 10:57 sys
drwxrwxrwt    5 root root   100 Aug 22 11:01 tmp
drwxr-xr-x    1 root root   122 May 28 17:35 usr
drwxr-xr-x    1 root root   108 May 27 12:21 var

With sorting for file size:

$ ls -lS
total 32
drwxr-xr-x    1 root root 87188 Aug 18 09:54 bin
drwxr-xr-x    1 root root 17526 Aug 18 09:53 lib
drwxr-xr-x    1 root root  8994 Aug 18 09:53 sbin
drwxr-xr-x    1 root root  4896 Aug 18 10:47 etc
drwxr-xr-x    2 root root  4096 Apr 24  2018 boot
drwxr-xr-x    1 root root  4096 Aug 12 20:40 abc
drwxr-xr-x    1 root root  4096 Aug 12 20:40 home
drwxr-xr-x    2 root root  4096 Jul 13 14:48 media
drwxr-xr-x    2 root root  4096 Jul 13 14:48 mnt
drwx------    2 root root  4096 Jul 13 14:49 root
drwxr-xr-x    1 root root  4096 Aug 12 20:40 run
drwxr-xr-x    4 root root  4096 Aug 22 10:57 secrets
drwxr-xr-x    1 root root  2544 May 27 12:25 libx32
drwxr-xr-x    1 root root  2422 May 27 12:25 lib32
drwxr-xr-x    5 root root   360 Aug 22 10:57 dev
drwxr-xr-x    1 root root   232 Aug 18 11:17 ext
drwxr-xr-x    1 root root   122 May 28 17:35 usr
drwxr-xr-x    1 root root   108 May 27 12:21 var
drwxrwxrwt    5 root root   100 Aug 22 10:57 tmp
drwxr-xr-x    1 root root    64 Jul 30 12:41 opt
drwxr-xr-x    1 root root    40 May 25 12:51 lib64
dr-xr-xr-x 1050 root root     0 Aug 22 10:57 proc
drwxr-xr-x    1 root root     0 Apr 23 11:06 srv
dr-xr-xr-x   13 root root     0 Aug 22 10:57 sys

✔️ This command works on all terminals: [Linix/UNIX/macOS/bash]


Comments:

  • Perfect.. this is what I was looking for! Thanks
    anonymous 05 Sep 2020 18:09:01 GMT
  • Further comments disabled!