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 Files in the directory by size (Sorting)
To list a content of a specific directory with size sorting, we will use -lS
options with ls
command. It will display files with the highest size at the top.
$ 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 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 To list with file size but it wont be sorted, we will use-s
option withls
command.
$ ls -s
2) List Files with size (Reverse sorting)
To list the content of a specific directory with size reverse sorting (ie lowest size files be at top), 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 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 Aug 21 13:18 auditd.pid
drwxr-xr-x. 2 root root 40 Aug 21 13:18 setrans
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
Few Basic Tips
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 the 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 files in alphabetical sorting
To list a content of the specified directory with details, such as the file permissions, the 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 ........
You can also user dir
command to list files, dir -S
will sort by size. Thanks for reading my article and please leave your comments.
Read Also:
- 15 Linux Screen Command for Dealing Terminal Sessions
- Exploring 20 'LS' Command in Linux for Displaying Entries of Directory
Comments