If you are new user in Linux world, the ls command is most popular and very useful command for listing the content of directories.
In this article, we will explain how to use ls sort option to list directory content by size.
1) List content details of directory upon size sorting
To list a content of a specific directory with size sorting, we will use -lS options with ls command.
$ ls -lS /run
output
total 24
-rw-rw-r–. 1 root utmp 2304 Sep 8 14:58 utmp
drwxr-xr-x. 16 root root 400 Aug 21 13:18 systemd
drwxr-xr-x. 7 root root 160 Aug 26 14:59 udev
drwxr-xr-x. 4 root root 100 Aug 21 13:18 initramfs
drwxr-xr-x. 4 root root 100 Sep 8 03:31 lock
drwxr-xr-x. 3 root root 100 Aug 21 13:18 NetworkManager
drwxr-xr-x. 2 root root 60 Aug 21 13:18 dbus
drwxr-xr-x. 3 root root 60 Aug 21 13:18 log
drwx–x–x. 3 root root 60 Aug 21 13:18 sudo
drwxr-xr-x. 2 root root 60 Aug 21 13:18 tmpfiles.d
drwxr-xr-x. 2 root root 60 Aug 21 13:18 tuned
drwxr-xr-x. 3 root root 60 Sep 7 23:11 user
drwxr-xr-x. 2 root root 40 Aug 21 13:18 console
drwxr-xr-x. 2 root root 40 Aug 21 13:18 faillock
drwxr-x—. 2 root root 40 Aug 21 13:18 firewalld
drwxr-xr-x. 2 root root 40 Aug 21 13:18 mount
……..
To list with file size, we will use -s option with ls command.
$ ls -s
output
total 1316
4 anaconda-ks.cfg 4 Downloads 180 index.html 0 smart.docx
4 apache2 4 echo.txt 4 nano.txt 0 smart.txt
4 cat.txt 0 file.txt 4 original-ks.cfg 0 test.txt
2) List content of directory with size reverse sorting
To list the content of a specific directory with size reverse sorting, we will use -lSr options with ls command.
$ ls -lSr /run
output
total 24
-rw——-. 1 root root 0 Aug 21 13:18 xtables.lock
-rw——-. 1 root root 0 Aug 21 13:18 ebtables.lock
———-. 1 root root 0 Aug 21 13:18 cron.reboot
-rw——-. 1 root root 3 Aug 21 13:18 syslogd.pid
-rw-r–r–. 1 root root 4 Aug 21 13:18 sshd.pid
-rw-r–r–. 1 root root 4 Sep 9 08:17 dhclient-eth0.pid
-rw-r–r–. 1 root root 4 Aug 21 13:18 crond.pid
-rw-r–r–. 1 root root 4 Aug 21 13:18 auditd.pid
drwxr-xr-x. 2 root root 40 Aug 21 13:18 setrans
drwxr-xr-x. 2 root root 40 Aug 21 13:18 sepermit
drwxr-xr-x. 2 root root 40 Aug 21 13:18 plymouth
drwxrwxr-x. 2 root root 40 Aug 21 13:18 netreport
drwxr-xr-x. 2 root root 40 Aug 21 13:18 mount
drwxr-x—. 2 root root 40 Aug 21 13:18 firewalld
……..
3) Sort output and print sizes in human readable format (e.g., 1K 48M 1G)
to sort output and print sizes in human readable format, we will use -h option with ls command.
$ ls -lSh
output
total 1.3M
-rw-r–r–. 1 root root 1.1M Aug 26 15:45 GeoIP-1.5.0-11.el7.x86_64.rpm
-rw-r–r–. 1 root root 177K Aug 26 15:29 index.html
drwxr-xr-x. 2 root root 4.0K Sep 8 13:32 apache2
drwxr-xr-x. 2 root root 4.0K Sep 8 13:31 Desktop
drwxr-xr-x. 2 root root 4.0K Sep 8 13:32 Documents
drwxr-xr-x. 2 root root 4.0K Sep 8 13:32 Downloads
drwxr-xr-x. 2 root root 4.0K Sep 8 13:32 Pictures
…….
Also, we can print sizes in human readable format for specific extention.
ls -l -S -h *.mp3
ls -l -S -h ~/Downloads/*.mp4 | more
4) List in alphabetical sorting
To list a content of a specific directory with alphabetical sorting, we will use ls command only without option, because alphabetical sorting is the default.
$ ls
output
anaconda-ks.cfg Desktop echo.txt index.html Pictures smart.txt
apache2 Documents f.txt nano.txt printf.txt vim.txt cat.txt
Downloads GeoIP-1.5.0-11.el7.x86_64.rpm original-ks.cfg smart.docx vi.txt
To list a content of a specific directory with details, add the path of the directory.
$ ls -l /run
output
total 24
-rw-r–r–. 1 root root 4 Aug 21 13:18 auditd.pid
drwxr-xr-x. 2 root root 40 Aug 21 13:18 console
-rw-r–r–. 1 root root 4 Aug 21 13:18 crond.pid
———-. 1 root root 0 Aug 21 13:18 cron.reboot
drwxr-xr-x. 2 root root 60 Aug 21 13:18 dbus
-rw-r–r–. 1 root root 4 Sep 9 08:17 dhclient-eth0.pid
-rw——-. 1 root root 0 Aug 21 13:18 ebtables.lock
drwxr-xr-x. 2 root root 40 Aug 21 13:18 faillock
drwxr-x—. 2 root root 40 Aug 21 13:18 firewalld
……….
5) List in alphabetical reverse sorting
To list a content of a specific directory with details upon alphabetical reverse sorting, we will use -lr options with ls command.
$ ls -lr /run
output
total 24
-rw——-. 1 root root 0 Aug 21 13:18 xtables.lock
-rw-rw-r–. 1 root utmp 2304 Sep 8 14:58 utmp
drwxr-xr-x. 3 root root 60 Sep 7 23:11 user
drwxr-xr-x. 7 root root 160 Aug 26 14:59 udev
drwxr-xr-x. 2 root root 60 Aug 21 13:18 tuned
drwxr-xr-x. 2 root root 60 Aug 21 13:18 tmpfiles.d
drwxr-xr-x. 16 root root 400 Aug 21 13:18 systemd
-rw——-. 1 root root 3 Aug 21 13:18 syslogd.pid
drwx–x–x. 3 root root 60 Aug 21 13:18 sudo
-rw-r–r–. 1 root root 4 Aug 21 13:18 sshd.pid
drwxr-xr-x. 2 root root 40 Aug 21 13:18 setrans
drwxr-xr-x. 2 root root 40 Aug 21 13:18 sepermit
drwxr-xr-x. 2 root root 40 Aug 21 13:18 plymouth
…….
6) List hidden content of directory in alphabetical sorting
To list hidden contents of specific directory, we will use -a or –all options with ls command.
$ ls -a /etc
output
. default gss logrotate.d pm rsyslog.conf sysctl.d
.. depmod.d host.conf machine-id polkit-1 rsyslog.d systemd
adjtime dhcp hostname magic popt.d rwtab system-release
aliases DIR_COLORS hosts makedumpfile.conf.sample postfix rwtab.d system-release-cpe
……..
7) List content of directory in alphabetical sorting
To list a content of specific directory with details, such as the file permissions, number of links, owner’s name and group owner, file size, time of last modification and the file/directory name,we will use -l option with ls command.
$ ls -l /run
output
total 24
-rw-r–r–. 1 root root 4 Aug 21 13:18 auditd.pid
drwxr-xr-x. 2 root root 40 Aug 21 13:18 console
-rw-r–r–. 1 root root 4 Aug 21 13:18 crond.pid
———-. 1 root root 0 Aug 21 13:18 cron.reboot
drwxr-xr-x. 2 root root 60 Aug 21 13:18 dbus
-rw-r–r–. 1 root root 4 Sep 8 12:41 dhclient-eth0.pid
-rw——-. 1 root root 0 Aug 21 13:18 ebtables.lock
drwxr-xr-x. 2 root root 40 Aug 21 13:18 faillock
drwxr-x—. 2 root root 40 Aug 21 13:18 firewalld
drwxr-xr-x. 4 root root 100 Aug 21 13:18 initramfs
drwxr-xr-x. 4 root root 100 Sep 8 03:31 lock
drwxr-xr-x. 3 root root 60 Aug 21 13:18 log
……..
Thanks for reading my article and please leave your comments.