Sort Files by Size with ls

I love reading man pages for even the most basic Unix commands like ls, because there’s always something interesting to learn. Today I discovered that it’s possible to sort ls output by file size.

Sort ls by file size

Simply add the capital S to command line options and you’ll see your output sorted by file size. Be sure to use long output form (-l option). I’m adding more command to the mix because I know that /usr/lib is a directory with quite a number of files:

greys@srv:/usr/bin $ ls -lS /usr/lib | more
total 492
-rwxr-xr-x  1 root 74688 Feb  1  2019 klibc-ae-2A4n9ZnfImcw9WIt7dF-3OvQ.so
-rw-r--r--  1 root 56480 Jan 14  2018 libdiscover.so.2.0.1
-rw-r--r--  1 root 22448 Aug 29  2016 libsupp.a
drwxr-xr-x 27 root 20480 Nov 24 01:00 python2.7
drwxr-xr-x 24 root 20480 Nov 26 22:12 x86_64-linux-gnu
-rw-r--r--  1 root 18336 Feb 26  2019 libau.so.2.10
drwxr-xr-x  3 root 12288 Sep 14 18:43 git-core
drwxr-xr-x 30 root 12288 Sep 14 18:44 python3.7
drwxr-xr-x 15 root 12288 Nov 26 22:12 systemd
drwxr-xr-x  2 root  4096 Aug 30 10:03 apparmor
drwxr-xr-x  5 root  4096 Aug 30 10:02 apt
drwxr-xr-x  2 root  4096 Aug 30 10:05 bfd-plugins

Reverse Sorting with ls command

Don’t know about you, but I prefer reverse sorting in most commands – this means largest objects are shown at the end of the output. The reason I do it this way is to avoid using pagers like more or less. Another reason is I can scroll up to explore further output of the ls command without having to rerun anything.

So here’s the same command run with the -r option for reverse output (I’m only showing last few lines):

...
drwxr-xr-x  2 root  4096 Aug 30 10:04 discover
drwxr-xr-x  2 root  4096 Aug 30 10:04 dbus-1.0
drwxr-xr-x  2 root  4096 Nov  3 20:02 console-setup
drwxr-xr-x  2 root  4096 Aug 30 10:05 compat-ld
drwxr-xr-x  2 root  4096 Nov 10 06:04 chrony
drwxr-xr-x  2 root  4096 May 24  2019 binfmt.d
drwxr-xr-x  2 root  4096 Aug 30 10:05 bfd-plugins
drwxr-xr-x  5 root  4096 Aug 30 10:02 apt
drwxr-xr-x  2 root  4096 Aug 30 10:03 apparmor
drwxr-xr-x 15 root 12288 Nov 26 22:12 systemd
drwxr-xr-x 30 root 12288 Sep 14 18:44 python3.7
drwxr-xr-x  3 root 12288 Sep 14 18:43 git-core
-rw-r--r--  1 root 18336 Feb 26  2019 libau.so.2.10
drwxr-xr-x 24 root 20480 Nov 26 22:12 x86_64-linux-gnu
drwxr-xr-x 27 root 20480 Nov 24 01:00 python2.7
-rw-r--r--  1 root 22448 Aug 29  2016 libsupp.a
-rw-r--r--  1 root 56480 Jan 14  2018 libdiscover.so.2.0.1
-rwxr-xr-x  1 root 74688 Feb  1  2019 klibc-ae-2A4n9ZnfImcw9WIt7dF-3OvQ.so
greys@srv:~ $

That’s it for today – hope you’ve learned something new today.

See Also




Contact Me

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!
Recent Articles
03 Sep 2024

macOS Sequoia 15.0

02 Sep 2024

Video: Has This Worked Before?

24 Mar 2024

Homelab: Mac Pro 2013 32GB

Recent Tweets