Sort Files By Size in Linux

Last updated on Dec 28, 2019 in Linux

One of the most basic command in Linux is the ls which is a utility to list files and their details. By default, the sort is done in alphabetical order.

There are built-in option -S to enable sorting by file size.

Sort Files By Size (Descending Order)

$ ls -lS
total 44
-rwxr-xr-x 1 webadmin apache 29615 Dec 16 14:57 booking.php
-rw-r-xr-- 1 webadmin apache    22 Jun 14  2019 README.md
-rw-r--r-- 1 webadmin apache    20 Dec 18 14:57 file3.php
-rw-r--r-- 1 webadmin apache    15 Dec 28 14:54 file1.php

Descending order is the default, meaning that the biggest file is first. The l stands for list mode. Without using it, only file names are viewed without sizes, but still the sorting is done by size.

Sort Files By Date (Ascending Order)

$ ls -lSr
total 44
-rw-r--r-- 1 webadmin apache    15 Dec 28 14:54 file1.php
-rw-r--r-- 1 webadmin apache    20 Dec 18 14:57 file3.php
-rw-r-xr-- 1 webadmin apache    22 Jun 14  2019 README.md
-rwxr-xr-x 1 webadmin apache 29615 Dec 16 14:57 booking.php

The r stands for reverse and it can be used in general in any listing.