Blog

03/06/2019

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.

The default output (sort by alphabetically)

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

Sample outputs:

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
Sample outputs:
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
OR
ls -Slhr

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 lnxpcs-master.zip
-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

Conclusion

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.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....