{"id":2037,"date":"2018-09-25T14:27:14","date_gmt":"2018-09-25T13:27:14","guid":{"rendered":"http:\/\/linoxide.com\/how-tos\/ls-command-files-sorting-size-linux\/"},"modified":"2026-01-30T04:55:57","modified_gmt":"2026-01-30T04:55:57","slug":"ls-command-files-sorting-size-linux","status":"publish","type":"post","link":"https:\/\/linoxide.com\/ls-command-files-sorting-size-linux\/","title":{"rendered":"How to Sort all Files by Size Using ls command in Linux"},"content":{"rendered":"\n<p>The&nbsp;<code>ls<\/code>&nbsp;command is most popular and very useful command for listing the content of directories.&nbsp;In this article, we will explain how to use&nbsp;&nbsp;<code>ls<\/code>&nbsp;sort option to list directory content by size.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1) List Files in the directory by size (Sorting)<\/h2>\n\n\n\n<p>To list a content of a&nbsp;specific directory with size sorting, we will use&nbsp;<code>-lS<\/code>&nbsp;options with&nbsp;<code>ls<\/code>&nbsp;command. It will display files with the highest size at the top.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -lS \/run<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>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 with ls command.<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -s<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2) List Files with size (Reverse sorting)<\/h2>\n\n\n\n<p>To list the content of a specific directory with size reverse sorting (ie lowest size files be at top), we will use&nbsp;<code>-lSr<\/code>&nbsp;options with&nbsp;<code>ls<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -lSr \/run<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>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<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3) Sort output and print sizes in human readable format (e.g., 1K 48M 1G)<\/h2>\n\n\n\n<p>To sort output and print sizes in human readable format, we will use&nbsp;<code>-h<\/code>&nbsp;option with&nbsp;<code>ls<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -lSh<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>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 .......<\/code><\/pre>\n\n\n\n<p>Also, we can print sizes in human readable format for specific extention.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l -S -h *.mp3<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l -S -h ~\/Downloads\/*.mp4 | more<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Few Basic Tips<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">4) List in alphabetical sorting<\/h2>\n\n\n\n<p>To list a content of a specific directory with alphabetical sorting, we will use&nbsp;<code>ls<\/code>&nbsp;command only without option, because alphabetical sorting is the default.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>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<\/code><\/pre>\n\n\n\n<p>To list a content of a specific directory with details, add the path of the directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -l \/run<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>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 ..........<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5) List in alphabetical reverse sorting<\/h2>\n\n\n\n<p>To list a content of a specific directory with details upon alphabetical reverse sorting, we will use&nbsp;<code>-lr<\/code>&nbsp;options with&nbsp;<code>ls<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -lr \/run<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>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 .......<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6) List hidden content of the directory in alphabetical sorting<\/h2>\n\n\n\n<p>To list hidden contents of specific directory, we will use&nbsp;<code>-a<\/code>&nbsp;or&nbsp;<code>--all<\/code>&nbsp;options with&nbsp;<code>ls<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -a \/etc<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code> 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 ........<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7) List files in alphabetical sorting<\/h2>\n\n\n\n<p>To list a content of the specified directory with details, such as the file permissions, the number of links, owner\u2019s name and group owner, file size, time of last modification and the file\/directory name, we will use&nbsp;<code>-l<\/code>&nbsp;option with&nbsp;<code>ls<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -l \/run<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>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 ........ <\/code><\/pre>\n\n\n\n<p>You can also user&nbsp;<code>dir<\/code>&nbsp;command to list files,&nbsp;<code>dir -S<\/code>&nbsp;will sort by size. Thanks for reading my article and please leave your comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;ls&nbsp;command is most popular and very useful command for listing the content of directories.&nbsp;In this article, we will explain how to use&nbsp;&nbsp;ls&nbsp;sort option to list directory content by size. 1) List Files in the directory by size (Sorting) To list a content of a&nbsp;specific directory with size sorting, we will use&nbsp;-lS&nbsp;options with&nbsp;ls&nbsp;command. It will display files with the highest size at the top. 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&nbsp;-lSr&nbsp;options with&nbsp;ls&nbsp;command. 3) Sort output and print sizes in<\/p>\n","protected":false},"author":1,"featured_media":2036,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[26],"tags":[],"class_list":["post-2037","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/posts\/2037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/comments?post=2037"}],"version-history":[{"count":1,"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/posts\/2037\/revisions"}],"predecessor-version":[{"id":2054,"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/posts\/2037\/revisions\/2054"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/media\/2036"}],"wp:attachment":[{"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/media?parent=2037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/categories?post=2037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linoxide.com\/wp-json\/wp\/v2\/tags?post=2037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}