{"id":402,"date":"2018-10-17T10:33:15","date_gmt":"2018-10-17T10:33:15","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/ls-command-syntax-for-files-sorting-by-size-in-linux\/"},"modified":"2018-10-17T10:33:15","modified_gmt":"2018-10-17T10:33:15","slug":"ls-command-syntax-for-files-sorting-by-size-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/ls-command-syntax-for-files-sorting-by-size-in-linux\/","title":{"rendered":"ls Command Syntax for Files Sorting by Size in Linux"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" alt=\"ls command sort by size linux\" height=\"341\" src=\"https:\/\/linoxide.com\/wp-content\/uploads\/2018\/09\/ls-command-sort-by-size-linux.png\" width=\"713\" \/><\/p>\n<p>If you are new user in Linux world, the ls command is most popular and very useful command for listing the content of directories.<\/p>\n<p>In this article, we will explain how to use ls sort option to list directory content by size.<\/p>\n<h2>1) List content details of directory upon size sorting<\/h2>\n<p>To list a content of a specific directory with size sorting, we will use -lS options with ls command.<\/p>\n<p>$ ls -lS \/run<br \/>\noutput&#xD;<br \/>\ntotal 24&#xD;<br \/>\n-rw-rw-r&#8211;. 1 root utmp 2304 Sep 8 14:58 utmp&#xD;<br \/>\ndrwxr-xr-x. 16 root root 400 Aug 21 13:18 systemd&#xD;<br \/>\ndrwxr-xr-x. 7 root root 160 Aug 26 14:59 udev&#xD;<br \/>\ndrwxr-xr-x. 4 root root 100 Aug 21 13:18 initramfs&#xD;<br \/>\ndrwxr-xr-x. 4 root root 100 Sep 8 03:31 lock&#xD;<br \/>\ndrwxr-xr-x. 3 root root 100 Aug 21 13:18 NetworkManager&#xD;<br \/>\ndrwxr-xr-x. 2 root root 60 Aug 21 13:18 dbus&#xD;<br \/>\ndrwxr-xr-x. 3 root root 60 Aug 21 13:18 log&#xD;<br \/>\ndrwx&#8211;x&#8211;x. 3 root root 60 Aug 21 13:18 sudo&#xD;<br \/>\ndrwxr-xr-x. 2 root root 60 Aug 21 13:18 tmpfiles.d&#xD;<br \/>\ndrwxr-xr-x. 2 root root 60 Aug 21 13:18 tuned&#xD;<br \/>\ndrwxr-xr-x. 3 root root 60 Sep 7 23:11 user&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 console&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 faillock&#xD;<br \/>\ndrwxr-x&#8212;. 2 root root 40 Aug 21 13:18 firewalld&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 mount&#xD;<br \/>\n&#8230;&#8230;..&#xD;<\/p>\n<p>To list with file size, we will use -s option with ls command.<\/p>\n<p>$ ls -s<br \/>\noutput&#xD;<br \/>\ntotal 1316&#xD;<br \/>\n 4 anaconda-ks.cfg 4 Downloads 180 index.html 0 smart.docx&#xD;<br \/>\n 4 apache2 4 echo.txt 4 nano.txt 0 smart.txt&#xD;<br \/>\n 4 cat.txt 0 file.txt 4 original-ks.cfg 0 test.txt&#xD;<\/p>\n<h2>2) List content of directory with size reverse sorting<\/h2>\n<p>To list the content of a specific directory with size reverse sorting, we will use -lSr options with ls command.<\/p>\n<p>$ ls -lSr \/run<br \/>\noutput&#xD;<br \/>\ntotal 24&#xD;<br \/>\n-rw&#8212;&#8212;-. 1 root root 0 Aug 21 13:18 xtables.lock&#xD;<br \/>\n-rw&#8212;&#8212;-. 1 root root 0 Aug 21 13:18 ebtables.lock&#xD;<br \/>\n&#8212;&#8212;&#8212;-. 1 root root 0 Aug 21 13:18 cron.reboot&#xD;<br \/>\n-rw&#8212;&#8212;-. 1 root root 3 Aug 21 13:18 syslogd.pid&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Aug 21 13:18 sshd.pid&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Sep 9 08:17 dhclient-eth0.pid&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Aug 21 13:18 crond.pid&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Aug 21 13:18 auditd.pid&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 setrans&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 sepermit&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 plymouth&#xD;<br \/>\ndrwxrwxr-x. 2 root root 40 Aug 21 13:18 netreport&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 mount&#xD;<br \/>\ndrwxr-x&#8212;. 2 root root 40 Aug 21 13:18 firewalld&#xD;<br \/>\n&#8230;&#8230;..&#xD;<\/p>\n<h2>3) Sort output and print sizes in human readable format (e.g., 1K 48M 1G)<\/h2>\n<p>to sort output and print sizes in human readable format, we will use -h option with ls command.<\/p>\n<p>$ ls -lSh<br \/>\noutput&#xD;<br \/>\ntotal 1.3M&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 1.1M Aug 26 15:45 GeoIP-1.5.0-11.el7.x86_64.rpm&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 177K Aug 26 15:29 index.html&#xD;<br \/>\ndrwxr-xr-x. 2 root root 4.0K Sep 8 13:32 apache2&#xD;<br \/>\ndrwxr-xr-x. 2 root root 4.0K Sep 8 13:31 Desktop&#xD;<br \/>\ndrwxr-xr-x. 2 root root 4.0K Sep 8 13:32 Documents&#xD;<br \/>\ndrwxr-xr-x. 2 root root 4.0K Sep 8 13:32 Downloads&#xD;<br \/>\ndrwxr-xr-x. 2 root root 4.0K Sep 8 13:32 Pictures&#xD;<br \/>\n&#8230;&#8230;.&#xD;<\/p>\n<p>Also, we can print sizes in human readable format for specific extention.<\/p>\n<p>ls -l -S -h *.mp3<br \/>\nls -l -S -h ~\/Downloads\/*.mp4 | more<\/p>\n<h2>4) List in alphabetical sorting<\/h2>\n<p>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.<\/p>\n<p>$ ls<br \/>\noutput&#xD;<br \/>\nanaconda-ks.cfg Desktop echo.txt index.html Pictures smart.txt&#xD;<br \/>\napache2 Documents f.txt nano.txt printf.txt vim.txt cat.txt&#xD;<br \/>\nDownloads GeoIP-1.5.0-11.el7.x86_64.rpm original-ks.cfg smart.docx vi.txt&#xD;<\/p>\n<p>To list a content of a specific directory with details, add the path of the directory.<\/p>\n<p>$ ls -l \/run<br \/>\noutput&#xD;<br \/>\ntotal 24&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Aug 21 13:18 auditd.pid&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 console&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Aug 21 13:18 crond.pid&#xD;<br \/>\n&#8212;&#8212;&#8212;-. 1 root root 0 Aug 21 13:18 cron.reboot&#xD;<br \/>\ndrwxr-xr-x. 2 root root 60 Aug 21 13:18 dbus&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Sep 9 08:17 dhclient-eth0.pid&#xD;<br \/>\n-rw&#8212;&#8212;-. 1 root root 0 Aug 21 13:18 ebtables.lock&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 faillock&#xD;<br \/>\ndrwxr-x&#8212;. 2 root root 40 Aug 21 13:18 firewalld&#xD;<br \/>\n&#8230;&#8230;&#8230;.&#xD;<\/p>\n<h2>5) List in alphabetical reverse sorting<\/h2>\n<p>To list a content of a specific directory with details upon alphabetical reverse sorting, we will use -lr options with ls command.<\/p>\n<p>$ ls -lr \/run<br \/>\noutput&#xD;<br \/>\ntotal 24&#xD;<br \/>\n-rw&#8212;&#8212;-. 1 root root 0 Aug 21 13:18 xtables.lock&#xD;<br \/>\n-rw-rw-r&#8211;. 1 root utmp 2304 Sep 8 14:58 utmp&#xD;<br \/>\ndrwxr-xr-x. 3 root root 60 Sep 7 23:11 user&#xD;<br \/>\ndrwxr-xr-x. 7 root root 160 Aug 26 14:59 udev&#xD;<br \/>\ndrwxr-xr-x. 2 root root 60 Aug 21 13:18 tuned&#xD;<br \/>\ndrwxr-xr-x. 2 root root 60 Aug 21 13:18 tmpfiles.d&#xD;<br \/>\ndrwxr-xr-x. 16 root root 400 Aug 21 13:18 systemd&#xD;<br \/>\n-rw&#8212;&#8212;-. 1 root root 3 Aug 21 13:18 syslogd.pid&#xD;<br \/>\ndrwx&#8211;x&#8211;x. 3 root root 60 Aug 21 13:18 sudo&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Aug 21 13:18 sshd.pid&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 setrans&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 sepermit&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 plymouth&#xD;<br \/>\n&#8230;&#8230;.&#xD;<\/p>\n<h2>6) List hidden content of directory in alphabetical sorting<\/h2>\n<p>To list hidden contents of specific directory, we will use -a or &#8211;all options with ls command.<\/p>\n<p>$ ls -a \/etc<br \/>\n output&#xD;<br \/>\n . default gss logrotate.d pm rsyslog.conf sysctl.d&#xD;<br \/>\n .. depmod.d host.conf machine-id polkit-1 rsyslog.d systemd&#xD;<br \/>\n adjtime dhcp hostname magic popt.d rwtab system-release&#xD;<br \/>\n aliases DIR_COLORS hosts makedumpfile.conf.sample postfix rwtab.d system-release-cpe&#xD;<br \/>\n &#8230;&#8230;..&#xD;<\/p>\n<h2>7) List content of directory in alphabetical sorting<\/h2>\n<p>To list a content of specific directory with details, such as the file permissions, number of links, owner\u2019s name and group owner, file size, time of last modification and the file\/directory name,we will use -l option with ls command.<\/p>\n<p>$ ls -l \/run<br \/>\noutput&#xD;<br \/>\ntotal 24&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Aug 21 13:18 auditd.pid&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 console&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Aug 21 13:18 crond.pid&#xD;<br \/>\n&#8212;&#8212;&#8212;-. 1 root root 0 Aug 21 13:18 cron.reboot&#xD;<br \/>\ndrwxr-xr-x. 2 root root 60 Aug 21 13:18 dbus&#xD;<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Sep 8 12:41 dhclient-eth0.pid&#xD;<br \/>\n-rw&#8212;&#8212;-. 1 root root 0 Aug 21 13:18 ebtables.lock&#xD;<br \/>\ndrwxr-xr-x. 2 root root 40 Aug 21 13:18 faillock&#xD;<br \/>\ndrwxr-x&#8212;. 2 root root 40 Aug 21 13:18 firewalld&#xD;<br \/>\ndrwxr-xr-x. 4 root root 100 Aug 21 13:18 initramfs&#xD;<br \/>\ndrwxr-xr-x. 4 root root 100 Sep 8 03:31 lock&#xD;<br \/>\ndrwxr-xr-x. 3 root root 60 Aug 21 13:18 log&#xD;<br \/>\n&#8230;&#8230;..&#xD;<\/p>\n<p>Thanks for reading my article and please leave your comments.<\/p>\n<h3>Read Also:<\/h3>\n<ul>\n<li><a href=\"https:\/\/linoxide.com\/linux-command\/15-examples-screen-command-linux-terminal\/\" target=\"_blank\">15 Linux Screen Command for Dealing Terminal Sessions<\/a><\/li>\n<li><a href=\"https:\/\/linoxide.com\/linux-command\/20-ls-command-linux\/\" target=\"_blank\">Exploring 20 &#8216;LS&#8217; Command in Linux for Displaying Entries of Directory<\/a><\/li>\n<\/ul>\n<p> <a href=\"https:\/\/linoxide.com\/linux-how-to\/ls-command-files-sorting-size-linux\/\" target=\"_blank\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/ls-command-syntax-for-files-sorting-by-size-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;ls Command Syntax for Files Sorting by Size in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-402","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=402"}],"version-history":[{"count":0,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/402\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}