{"id":2421,"date":"2018-11-02T19:45:06","date_gmt":"2018-11-02T19:45:06","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=2421"},"modified":"2018-11-04T08:03:32","modified_gmt":"2018-11-04T08:03:32","slug":"guide-to-sort-files-by-date-using-ls-commandline-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/02\/guide-to-sort-files-by-date-using-ls-commandline-in-linux\/","title":{"rendered":"Guide to Sort Files by Date Using LS Commandline in Linux"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linoxide.com\/wp-content\/uploads\/2018\/11\/ls-command-sort-by-date.png\" alt=\"ls command sort by date\" width=\"713\" height=\"341\" \/><\/p>\n<p>The ls command is used to list directory contents and the results can be sorted upon several criteria such as by date, alphabetical order of filenames, modification time, access time, version and file size.<\/p>\n<p>In this article, I will show you how to sort files by date using ls command in Linux.<\/p>\n<p>Hope you interested in reading: <a href=\"https:\/\/linoxide.com\/linux-how-to\/ls-command-files-sorting-size-linux\/\" target=\"_blank\" rel=\"noopener\">How to Sort all Files by Size Using ls command in Linux<\/a><\/p>\n<h2>1) List Files directory with Last Modified Date\/Time<\/h2>\n<p>To list files and shows the last modified files at top, we will use -lt options with ls command.<\/p>\n<p>$ ls -lt \/run<br \/>\noutput<br \/>\ntotal 24<br \/>\n-rw-rw-r&#8211;. 1 root utmp 2304 Sep 8 14:58 utmp<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Sep 8 12:41 dhclient-eth0.pid<br \/>\ndrwxr-xr-x. 4 root root 100 Sep 8 03:31 lock<br \/>\ndrwxr-xr-x. 3 root root 60 Sep 7 23:11 user<br \/>\ndrwxr-xr-x. 7 root root 160 Aug 26 14:59 udev<br \/>\ndrwxr-xr-x. 2 root root 60 Aug 21 13:18 tuned<\/p>\n<h2>2) List Files with Last Modified Date\/Time (most recent at bottom)<\/h2>\n<p>We will use -ltr options with ls command to list files of a specific directory with recently modified files at the bottom.<\/p>\n<p>$ ls -ltr \/run<br \/>\n$ ls -ltr \/run<br \/>\ntotal 13404<br \/>\ndrwxr-xr-x 2 root root 4096 Dec 14 2016 scripts<br \/>\n-rwxr-xr-x 1 root root 4688 Dec 14 2016 perms.py<br \/>\n-rw-r&#8211;r&#8211; 1 root root 9718 Jun 23 14:47 ddagent-install.log<br \/>\n-rw-r&#8211;r&#8211; 1 root root 1457471 Jun 26 01:26 rocket.zip<br \/>\ndrwxr-xr-x 2 root root 4096 Jun 26 10:40 ssl-21APR2018-11JUN2020<br \/>\ndrwxr-xr-x 6 root root 4096 Jun 27 09:29 incubator-pagespeed-ngx-latest-stable<br \/>\ndrwxr-xr-x 9 root root 4096 Jun 27 09:29 nginx-1.15.0<br \/>\ndrwxr-xr-x 3 root root 4096 Jul 2 19:55 rocket-nginx<br \/>\n-rw-r&#8211;r&#8211; 1 root root 18186 Jul 11 13:17 memcachy.zip<br \/>\n-rwxr-xr-x 1 root root 12202195 Sep 4 12:21 Linux_64bit.install<br \/>\n:~#<\/p>\n<p>If you want to sort by directory, then by dates use<\/p>\n<p>$ ls -Rltr<\/p>\n<h2>3) Display in Human Readable format<\/h2>\n<p>We will use -halt options with ls command to list files of a specific directory in human readable file sizes, long format. It uses K, M, G, and T suffixes (or no suffix for bytes)<\/p>\n<p>$ ls -halt \/run<br \/>\ntotal 28K<br \/>\n-rw-rw-r&#8211;. 1 root utmp 1.9K Oct 28 06:02 utmp<br \/>\ndrwxr-xr-x. 3 root root 60 Oct 28 06:02 user<br \/>\ndrwxr-xr-x. 4 root root 100 Oct 28 03:48 lock<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Oct 28 02:50 dhclient-eth0.pid<br \/>\ndrwxr-xr-x. 7 root root 160 Oct 25 12:16 udev<br \/>\ndrwxr-xr-x. 21 root root 600 Oct 25 12:15 .<\/p>\n<h2>4) Find files modified in Last 10 minutes<\/h2>\n<p>We can get files modified in last 10 minutes with command below:<\/p>\n<p>$ find . -mmin -10 -type f -exec ls -l {} +<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 53 Nov 1 01:58 .\/smart.txt<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 15 Nov 1 02:00 .\/test\/file1<\/p>\n<h2>5) Recently modified 10 files<\/h2>\n<p>Let see how to check recently modified 10 files in a directory using ls commands. We will use a combination of &#8216;ls&#8221; and &#8216;head&#8217; command.<\/p>\n<p>Below command will show modified 10 files with the recently updated file at the top<\/p>\n<p>$ ls -lt \/run\/ | head -10<br \/>\n-rw-rw-r&#8211;. 1 root utmp 1920 Oct 31 01:57 utmp<br \/>\ndrwxr-xr-x. 3 root root 60 Oct 31 01:57 user<br \/>\ndrwxr-xr-x. 16 root root 400 Oct 30 23:06 systemd<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Oct 30 18:42 dhclient-eth0.pid<br \/>\ndrwxr-xr-x. 4 root root 100 Oct 30 03:06 lock<br \/>\ndrwxr-xr-x. 7 root root 160 Oct 28 06:09 udev<br \/>\n-rw&#8212;&#8212;-. 1 root root 3 Oct 25 12:15 syslogd.pid<br \/>\ndrwxr-xr-x. 2 root root 60 Oct 25 12:15 tuned<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Oct 25 12:15 sshd.pid<\/p>\n<p>Or<\/p>\n<p>with tail combination, it shows recently updated file at the bottom.<\/p>\n<p>$ ls -ltr \/run\/ | tail -10<br \/>\ndrwxr-xr-x. 3 root root 100 Oct 25 12:15 NetworkManager<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Oct 25 12:15 sshd.pid<br \/>\ndrwxr-xr-x. 2 root root 60 Oct 25 12:15 tuned<br \/>\n-rw&#8212;&#8212;-. 1 root root 3 Oct 25 12:15 syslogd.pid<br \/>\ndrwxr-xr-x. 7 root root 160 Oct 28 06:09 udev<br \/>\ndrwxr-xr-x. 4 root root 100 Oct 30 03:06 lock<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 4 Oct 30 18:42 dhclient-eth0.pid<br \/>\ndrwxr-xr-x. 16 root root 400 Oct 30 23:06 systemd<br \/>\ndrwxr-xr-x. 3 root root 60 Oct 31 01:57 user<br \/>\n-rw-rw-r&#8211;. 1 root utmp 1920 Oct 31 01:57 utmp<\/p>\n<p>Thanks for reading this article and please comment below if you find any other options useful.<\/p>\n<h3>Read Also:<\/h3>\n<ul>\n<li><a href=\"https:\/\/linoxide.com\/linux-command\/20-ls-command-linux\/\" target=\"_blank\" rel=\"noopener\">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\/how-sort-files-date-using-ls-command-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ls command is used to list directory contents and the results can be sorted upon several criteria such as by date, alphabetical order of filenames, modification time, access time, version and file size. In this article, I will show you how to sort files by date using ls command in Linux. Hope you interested &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/02\/guide-to-sort-files-by-date-using-ls-commandline-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Guide to Sort Files by Date Using LS Commandline 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-2421","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\/2421","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=2421"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2421\/revisions"}],"predecessor-version":[{"id":2525,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2421\/revisions\/2525"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=2421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=2421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=2421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}