Linux ls Command Sort Files By Size

How do I sort all *.avi or *.py files in $HOME/Download/ directory by file size using Linux ls command line utility? How do I list all files ordered by size in Linux using ls command?

The ls command is used to list directory contents under Linux and Unix like operating systems. If no options or operands are given, the contents of the current directory are displayed on the screen. By default entries are sorted alphabetically if none of the -cftuvSUX nor --sort option passed to the ls command. Let us see how to use the ls command to sort files by size on Linux and Unix-like systems such as FreeBSD.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements GNU ls/BSD ls
Est. reading time 4 mintues

The default output (sort by alphabetically)

Type the following command:
$ ls
$ ls *.py
$ ls *.avi

Fig.01: ls Command Output

Linux force sort by size option

You need to pass the -S or --sort=size option as follows to Linux or Unix command line:
$ ls -S
$ ls -S -l
$ ls --sort=size -l
$ ls --sort=size *.avi
$ ls -S -l *.avi

Sample outputs:

Fig.02: Sort files / folders (directories) by size

You will see largest file first before sorting the operands in lexicographical order. The following command will sort file size in reverse order:
$ ls -l -S | sort -k 5 -n
OR try (see comments below, thanks!):
$ ls -lSr

Fig.03: Ls Command Sort By Size in Reverse (Lowest First) Order

Sort output and print sizes in human readable format (e.g., 1K 234M 2G)

Pass the -h option to the ls command as follows:
$ ls -lSh
$ ls -l -S -h *.avi
$ ls -l -S -h ~/Downloads/*.mp4 | more

Sorting ls command output by file size on Linux

Run the following ls command:
ls -Slh
ls -Slhr

ls Command Sort Files By Size Command Options

Table 1: ls command options to list all files ordered by size
Option Description
-l Long listing
-S Sort by file size, largest first
--sort=size sort by size instead of file name(s)
-r Reverse order while sorting
-h Human readable output. with -l and -s option, print sizes like 1K 234M 2G etc.

How to exclude directories when all files ordered by size

Try filtering outputs of ls command using grep command:
ls -lS | grep -v '^d'
ls -Slh | grep -v '^d'
ls -Slhr | grep -v '^d'

total 289836
-rw-rw-r--.  1 vivek vivek 295599646 Jun  9  2018
-rw-rw-r--.  1 vivek vivek    501221 Dec 27 15:49 top.cvs
-rw-r--r--.  1 vivek vivek    270466 Jan 30  2018 out
-rw-------.  1 vivek vivek    124866 Aug 13 12:38 Receipt Details.pdf
-rwxr-xr-x.  1 vivek vivek     47441 Nov 18  2017 speedtest-cli
-rw-r--r--.  1 vivek vivek     19419 Feb  9  2018 final.txt
-rw-r--r--.  1 vivek vivek     13654 Jan 13 17:09 updated.txt
-rw-r--r--.  1 vivek vivek      8247 Aug 14 09:23 nixcraft.conf
-rw-r--r--.  1 vivek vivek      5083 Jan  8 00:58 wanted.txt
-rw-r--r--.  1 vivek vivek      2674 Jan  3 02:47 vpn.config.ovpn
-rw-rw-r--.  1 vivek vivek      1539 Jun  8  2018 email-ids.txt

One can use find command as well along with sort command to just list dir size in sorted order:
find . -type d -ls | sort -n -r
Or just list files and exclude all dirs when sorting:
find . -type f -ls | sort -n -r

1619988315      0 drwxr-xr-x   2  vivek    vivek         164 Dec 20 15:24 ./images/password-change
1619988314      0 drwxr-xr-x   2  vivek    vivek          84 Nov  7 16:26 ./images/dig-not-found
1619988310      0 drwxr-xr-x   2  vivek    vivek         106 Sep 21 00:34 ./pfsense/fw0.nixcraft.lan
1615242013      0 drwxr-xr-x   2  vivek    vivek          83 Jun 19  2018 ./hello-web-app/pdf
1615241993      0 drwxr-xr-x   2  vivek    vivek         156 Feb  8  2018 ./books/ssh/ssh2\ twp\ 3\ formats\ 2018-02-07


This page showed how to list all files ordered by size in Linux or Unix like systems. See ls(1) command man page for more information.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 29 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
29 comments… add one
  • Raphael Jan 13, 2013 @ 20:27

    Instead of
    $ ls -l -S | sort -k 5 -n
    this works also
    $ ls -l -S -r

    -r = reverse order

    • Roy Jan 13, 2013 @ 22:24

      Or just do

      ll -Sr


      • dru Jan 14, 2013 @ 14:10

        ll is alias to ‘ls -l’. Although it it configured on many linux distros, it does not have to be and therefore not guaranteed to work.

  • dru Jan 13, 2013 @ 20:51

    Even better is:
    $ ls -lrS

  • 🐧 nixCraft Jan 14, 2013 @ 11:49

    Raphael, Roy, and dru,

    Thanks for the heads up. The faq has been updated.

  • george Jan 15, 2013 @ 8:17

    what I do is this:
    du -xak . | sort -n | tail -100
    it finds me the biggest 100 files or directories on the filesystem I’m in

    • CLaude Jan 4, 2017 @ 19:52

      or du -csh * | sort -n | tail -100

  • foober Aug 7, 2013 @ 19:50

    incorrectly using lower case s in these examples:

    $ ls -s

    $ ls -s *.avi

  • Vibin Sep 27, 2013 @ 9:43

    ls -lhS in human readable format.Sorted in descending order

  • Japan Shah Oct 9, 2013 @ 6:22

    Thanks, it worked.

  • Juan Nov 25, 2013 @ 2:21

    How would I perform a sort on my home directory by the size of the file??

    • dru Nov 26, 2013 @ 20:48

      Assuming you have an ls version that supports ‘–group-directories-first’ you can do:

      # ls -ldhS --group-directories-first ~/*
      # ls -ldhrS --group-directories-first ~/*
  • CJ Dennis Dec 11, 2013 @ 22:56

    It needs to be a capital S.

    ls -S


    ls -s
  • Pierce Feb 19, 2014 @ 16:14

    ls -lSrh

    h makes the filesize a bit more readable :D

  • foo May 8, 2014 @ 23:35

    seriously, fix your “-s” typo or stop being the top hit on google :P

    • 🐧 Nix Craft May 9, 2014 @ 5:03

      @foo and rest: Sorry about that :( The faq has been updated.

  • noob May 27, 2014 @ 16:31

    % ls -S
    ls: illegal option — S
    usage: ls -1RaAdCxmnlhogrtuvVcpFbqisfHLeE@ [files]

    • dru May 28, 2014 @ 13:26

      That’s more than likely Solaris. However, if you have coreutils installed you can do ‘gls -S’ ;-)

  • E. Pluribus Dickhead Sep 30, 2014 @ 19:12

    The one thing I find most annoying about Linux “ls” is the fact that it (at least on Ubuntu) it sorts things in alphabetical order, no matter if it’s a dot file, or capitalized, or whatever. This highly offends my BSD-like sensibilities and makes me think linux “ls” belongs to DOS.

    Is there a way to make it stop this vile and pernicous behavior? ;)

    • dru Oct 1, 2014 @ 4:50

      Don’t have a Linux box right now to test but I think you’re looking for this:
      alias lsdot='LC_COLLATE=C ls -alF'

      • E. Pluribus Dickhead Oct 1, 2014 @ 19:44

        Cool — I’ll check it out.

    • Gaurav Khurana Feb 16, 2015 @ 14:33

      i think you may alias it

      alias ‘ls=ls -lrt’ and put this instrcutionion into your .profile so that everytime you open a session ls will get replaced by ls -lrt

  • Rihab Jan 8, 2016 @ 10:16

    I also like this one:
    *) ls –sort=size -sh

  • m0ZamBiQki Jan 22, 2016 @ 12:21

    no option for -S in SunOS 5.10 sun4v sparc
    $ ls -lSr
    ls: illegal option -- S

    neither the below:
    $ ls --sort=size -l
    ls: illegal option -- sort=size

    • Chandan Kumar Naik Mar 2, 2021 @ 11:33

      These commands only work on Linux and macOS/FreeBSD etc. You are using really older version of Unix. See man page

      man ls
  • Rooney Apr 14, 2021 @ 1:26

    Thank you. Your article help me on macOS.

  • Anonymous Jun 18, 2021 @ 15:50

    Hi Vivek,
    Thanks for the article – I found the command where you exclude directories via `grep -v ‘^d’` super useful here ; I did not understand how to exclude a PWD’s directories until I saw the usage of this control character in the beginning of a long listing ( since they are listed with this interesting drwxrwxrwx esque format )!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum