≡ Menu

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?

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.

The default output (sort by alphabetically)

Type the following command:

$ ls
$ ls *.py
$ ls *.avi

Sample outputs:

Fig.01: ls Command Output

Fig.01: ls Command Output

Force sort by size option

You need to pass the -S or --sort=size option as follows:
$ 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

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
Sample outputs:
Fig.03: Ls Command Sort By Size in Reverse (Lowest First) Order

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

See ls(1) command man page for more information.

{ 21 comments… add one }

  • Raphael January 13, 2013, 8:27 pm

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

    -r = reverse order

    • Roy January 13, 2013, 10:24 pm

      Or just do

      ll -Sr


      • dru January 14, 2013, 2:10 pm

        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 January 13, 2013, 8:51 pm

    Even better is:
    $ ls -lrS

  • nixCraft January 14, 2013, 11:49 am

    Raphael, Roy, and dru,

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

  • george January 15, 2013, 8:17 am

    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

  • foober August 7, 2013, 7:50 pm

    incorrectly using lower case s in these examples:

    $ ls -s

    $ ls -s *.avi

  • Vibin September 27, 2013, 9:43 am

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

  • Japan Shah October 9, 2013, 6:22 am

    Thanks, it worked.

  • Juan November 25, 2013, 2:21 am

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

    • dru November 26, 2013, 8:48 pm

      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 December 11, 2013, 10:56 pm

    It needs to be a capital S.

    ls -S


    ls -s
  • Pierce February 19, 2014, 4:14 pm

    ls -lSrh

    h makes the filesize a bit more readable :D

  • foo May 8, 2014, 11:35 pm

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

    • Nix Craft May 9, 2014, 5:03 am

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

  • noob May 27, 2014, 4:31 pm

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

    • dru May 28, 2014, 1:26 pm

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

  • E. Pluribus Dickhead September 30, 2014, 7:12 pm

    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? ;)

Leave a Comment

   Tagged with: , , , ,