To list or sort all files in a directory by size, we will use the ls command, which will list computer files in Unix and Unix-like operating systems.
Releated Read: Fun: SL (Steam Locomotive) Runs a Train in Your Linux Terminal
When invoked without any arguments, ls
lists the files in the current working directory.
[root@linuxshelltips:~]# ls anaconda-ks.cfg bridge-nf-call-iptabley~ fwbackups-1.43.7 ImageMagick-7.0.8-28 remi-release-7.rpm bridge-nf-call-iptables~ bridge-nf-call-iptablez~ fwbackups-1.43.7.tar.bz2 ImageMagick.tar.gz report.xml bridge-nf-call-iptablex~ caddy highlight.min.js initial-setup-ks.cfg rh6_CloudBerryBackup.rpm
List All Files Ordered by Size
To list or sort all the files by size, use the -S
option, that tells the ls
command to sort the file listing by size and the -h
option makes the output a human-readable format.
In the following output, the largest files are shown in the beginning.
[root@linuxshelltips:~]# ls -lhS total 52M -rw-r--r--. 1 root root 37M Sep 3 2018 rh6_CloudBerryBackup.rpm -rw-r--r--. 1 root root 14M Feb 17 2019 ImageMagick.tar.gz -rw-r--r--. 1 root root 1.8M Oct 4 2019 fwbackups-1.43.7.tar.bz2 -rw-r--r-- 1 root root 98K Jul 23 17:31 highlight.min.js -rw-r--r--. 1 root root 16K Dec 21 2018 remi-release-7.rpm drwxrwxr-x 5 root root 4.0K Dec 26 2017 fwbackups-1.43.7 drwxrwxr-x 15 root root 4.0K Feb 17 2019 ImageMagick-7.0.8-28 -rw-r--r--. 1 root root 3.1K Sep 19 2018 report.xml -rw-r--r--. 1 root root 2.0K Sep 4 2018 initial-setup-ks.cfg -rw-------. 1 root root 1.9K Jan 1 2018 anaconda-ks.cfg drwxr-xr-x. 3 root root 47 Sep 19 2018 caddy -rw-r--r--. 1 root root 2 Jan 14 2020 bridge-nf-call-iptables~ -rw-r--r--. 1 root root 2 Jan 14 2020 bridge-nf-call-iptablex~ -rw-r--r--. 1 root root 2 Jan 14 2020 bridge-nf-call-iptabley~ -rw-r--r--. 1 root root 2 Jan 14 2020 bridge-nf-call-iptablez~
List All Files Sizes in Reverse Order
You can also sort files by size in reverse order by using the -r
option as shown.
[user@linuxshelltips:~]# ls -lhrS total 52M -rw-r--r--. 1 root root 2 Jan 14 2020 bridge-nf-call-iptablez~ -rw-r--r--. 1 root root 2 Jan 14 2020 bridge-nf-call-iptabley~ -rw-r--r--. 1 root root 2 Jan 14 2020 bridge-nf-call-iptablex~ -rw-r--r--. 1 root root 2 Jan 14 2020 bridge-nf-call-iptables~ drwxr-xr-x. 3 root root 47 Sep 19 2018 caddy -rw-------. 1 root root 1.9K Jan 1 2018 anaconda-ks.cfg -rw-r--r--. 1 root root 2.0K Sep 4 2018 initial-setup-ks.cfg -rw-r--r--. 1 root root 3.1K Sep 19 2018 report.xml drwxrwxr-x 15 root root 4.0K Feb 17 2019 ImageMagick-7.0.8-28 drwxrwxr-x 5 root root 4.0K Dec 26 2017 fwbackups-1.43.7 -rw-r--r--. 1 root root 16K Dec 21 2018 remi-release-7.rpm -rw-r--r-- 1 root root 98K Jul 23 17:31 highlight.min.js -rw-r--r--. 1 root root 1.8M Oct 4 2019 fwbackups-1.43.7.tar.bz2 -rw-r--r--. 1 root root 14M Feb 17 2019 ImageMagick.tar.gz -rw-r--r--. 1 root root 37M Sep 3 2018 rh6_CloudBerryBackup.rpm
If you have any other Linux tips, questions, or doubts? do ask for help in the comments section.