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


ls command sort by file size - examples

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 the size of the files/dir in greatest to lowest. If you wish to display the list in reverse order smallest to the 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!
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency