{"id":167,"date":"2024-01-27T01:57:40","date_gmt":"2024-01-27T01:57:40","guid":{"rendered":"https:\/\/buyandhost.in\/blog\/?p=167"},"modified":"2024-02-03T02:47:34","modified_gmt":"2024-02-03T02:47:34","slug":"sort-files-based-on-date-and-time-in-linux","status":"publish","type":"post","link":"https:\/\/buyandhost.in\/blog\/sort-files-based-on-date-and-time-in-linux\/","title":{"rendered":"Sort Files Based on Date and Time in Linux"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Linux, you can use the <code>find<\/code> command along with the <code>ls<\/code> and <code>sort<\/code> commands to find and sort files based on their date and time. <\/p>\n\n\n\n<p>When you&#8217;re dealing with <a href=\"https:\/\/buyandhost.in\/vps-hosting.html\" data-type=\"link\" data-id=\"https:\/\/buyandhost.in\/vps-hosting.html\">web hosting<\/a>, you often have limited access to the command line compared to a full Linux system. <\/p>\n\n\n\n<p>However, many web hosting platforms provide a web-based file manager that allows you to navigate through your files and perform basic operations. <\/p>\n\n\n\n<p>The ability to find and sort files based on date and time may vary depending on the features provided by your hosting provider.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here&#8217;s a basic example:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#find-and-sort-files-by-modification-time\">Find and Sort Files by Modification Time:<\/a><\/li><li><a href=\"#find-and-sort-files-by-access-time\">Find and Sort Files by Access Time:<\/a><\/li><li><a href=\"#linux-utilities-to-sort-files-in-linux\">Linux Utilities to Sort Files in Linux<\/a><ul><li><a href=\"#ls-command\">ls command<\/a><\/li><li><a href=\"#sort-command\">sort command<\/a><\/li><\/ul><\/li><li><a href=\"#sort-files-using-date-and-time\">Sort Files using Date and Time<\/a><ul><li><a href=\"#1-list-files-based-on-modification-time\">1. List Files Based on Modification Time<\/a><\/li><li><a href=\"#2-list-files-based-on-last-access-time\">2. List Files Based on Last Access Time<\/a><\/li><li><a href=\"#3-list-files-based-on-last-modification-time\">3. List Files Based on Last Modification Time<\/a><\/li><li><a href=\"#4-sorting-files-based-on-month\">4. Sorting Files based on Month<\/a><\/li><li><a href=\"#5-sort-files-based-on-date\">5. Sort Files Based on Date<\/a><\/li><li><a href=\"#6-sorting-files-based-on-time\">6. Sorting Files Based on Time<\/a><\/li><li><a href=\"#7-sorting-ouptut-of-ls-l-based-on-date\">7. Sorting Ouptut of ls -l based on Date<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"find-and-sort-files-by-modification-time\">Find and Sort Files by Modification Time:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/path\/to\/search -type f -exec ls -l --time=modification {} + | sort -k 6,7<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">The &#8211;time=creation option is used to show the creation time instead of modification time. Note that not all file systems support creation time, so this might not work on all systems.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"find-and-sort-files-by-access-time\">Find and Sort Files by Access Time:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/home\/buyandhost\/ -type f -exec ls -l --time=access {} + | sort -k 6,7<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"51\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-9-1024x51.png\" alt=\"\" class=\"wp-image-242\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-9-1024x51.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-9-300x15.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-9-768x38.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-9-1536x76.png 1536w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-9.png 1756w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The &#8211;time=access option is used to show the access time instead of modification time.<br>You can customize the command according to your specific requirements. <\/p>\n\n\n\n<p>The find command is powerful and allows you to filter files based on various criteria. The ls command with the &#8211;time option helps in displaying the relevant timestamp information, and sort is used to sort the output based on the specified columns.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"linux-utilities-to-sort-files-in-linux\">Linux Utilities to Sort Files in Linux<\/h2>\n\n\n\n<p>Some basic Linux command line utilities that are just sufficient for sorting a directory based on&nbsp;<strong>Date<\/strong>&nbsp;and&nbsp;<strong>Time<\/strong>&nbsp;are:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"ls-command\">ls command<\/h4>\n\n\n\n<p><strong>ls<\/strong>&nbsp;\u2013 Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"sort-command\">sort command<\/h4>\n\n\n\n<p><strong>sort<\/strong>&nbsp;\u2013 This command can be used to sort the output of any search just by any field or any particular column of the field.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sort-files-using-date-and-time\">Sort Files using Date and Time<\/h2>\n\n\n\n<p>Below are the list of commands to sort based on Date and Time.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"1-list-files-based-on-modification-time\">1. List Files Based on Modification Time<\/h4>\n\n\n\n<p>The below command lists files in long listing format, and sorts files based on modification time, newest first. To sort in reverse order, use&nbsp;<code>'-r'<\/code>&nbsp;switch with this command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -lt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"101\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-10-1024x101.png\" alt=\"\" class=\"wp-image-245\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-10-1024x101.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-10-300x30.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-10-768x76.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-10.png 1226w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"2-list-files-based-on-last-access-time\">2. List Files Based on Last Access Time<\/h4>\n\n\n\n<p>Listing of files in directory based on last access time, i.e. based on time the file was last accessed, not modified.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -ltu<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"100\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-11-1024x100.png\" alt=\"\" class=\"wp-image-246\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-11-1024x100.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-11-300x29.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-11-768x75.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-11.png 1242w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"3-list-files-based-on-last-modification-time\">3. List Files Based on Last Modification Time<\/h4>\n\n\n\n<p>Listing of files in directory based on last modification time of file\u2019s status information, or the&nbsp;<code>'ctime'<\/code>. <\/p>\n\n\n\n<p>This command would list that file first whose any status information like: owner, group, permissions, size etc has been recently changed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -ltc<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"96\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-12-1024x96.png\" alt=\"\" class=\"wp-image-247\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-12-1024x96.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-12-300x28.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-12-768x72.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-12.png 1311w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"4-sorting-files-based-on-month\">4. Sorting Files based on Month<\/h4>\n\n\n\n<p>Here, we use&nbsp;<code>find<\/code>&nbsp;command to find all files in root&nbsp;<code>(\u2018\/\u2019)<\/code>&nbsp;directory and then print the result as:&nbsp;<strong>Month<\/strong>&nbsp;in which file was accessed and then filename. Of that complete result, here we list out top&nbsp;<strong>11<\/strong>&nbsp;entries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/ -type f -printf \"\\n%Ab %p\" | head -n 11<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"285\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-13-1024x285.png\" alt=\"\" class=\"wp-image-248\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-13-1024x285.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-13-300x83.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-13-768x213.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-13.png 1299w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The below command sorts the output using key as first field, specified by&nbsp;<code>'-k1'<\/code>&nbsp;and then it sorts on Month as specified by&nbsp;<code>'M'<\/code>&nbsp;ahead of it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/ -type f -printf \"\\n%Ab %p\" | head -n 11 | sort -k1M<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"244\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-14-1024x244.png\" alt=\"\" class=\"wp-image-251\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-14-1024x244.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-14-300x71.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-14-768x183.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-14.png 1496w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"5-sort-files-based-on-date\">5. Sort Files Based on Date<\/h4>\n\n\n\n<p>Here, again we use&nbsp;<code>find<\/code>&nbsp;command to find all the files in root directory, but now we will print the result as:&nbsp;<strong>last date<\/strong>&nbsp;the file was accessed,&nbsp;<strong>last time<\/strong>&nbsp;the file was accessed and then filename. Of that we take out top 11 entries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/ -type f -printf \"\\n%AD %AT %p\" | head -n 11<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"263\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-15-1024x263.png\" alt=\"\" class=\"wp-image-252\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-15-1024x263.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-15-300x77.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-15-768x198.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-15.png 1411w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"6-sorting-files-based-on-time\">6. Sorting Files Based on Time<\/h4>\n\n\n\n<p>Here, again we use&nbsp;<code>find<\/code>&nbsp;command to list out top 11 files in root directory and print the result in format: last time file was accessed and then filename.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/ -type f -printf \"\\n%AT %p\" | head -n 11<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"260\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-16-1024x260.png\" alt=\"\" class=\"wp-image-253\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-16-1024x260.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-16-300x76.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-16-768x195.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-16.png 1413w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The below command sorts the output based on first column of the first field of the output which is first digit of hour.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/ -type f -printf \"\\n%AT %p\" | head -n 11 | sort -k1.1n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"245\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-17-1024x245.png\" alt=\"\" class=\"wp-image-254\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-17-1024x245.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-17-300x72.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-17-768x184.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-17.png 1503w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"7-sorting-ouptut-of-ls-l-based-on-date\">7. Sorting Ouptut of ls -l based on Date<\/h4>\n\n\n\n<p>This command sorts the output of&nbsp;<code>'ls -l'<\/code>&nbsp;command based on 6th field month wise, then based on 7th field which is date, numerically.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l | sort -k6M -k7n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"112\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-18-1024x112.png\" alt=\"\" class=\"wp-image-255\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-18-1024x112.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-18-300x33.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-18-768x84.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-18.png 1062w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We hope you&#8217;ve found this useful<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Linux, you can use the find command along with the ls and sort commands to find and sort files based on their date and time. When you&#8217;re dealing with web hosting, you often have limited access to the command line compared to a full Linux system. However, many web hosting platforms provide a web-based [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":260,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":6,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":261,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions\/261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/media\/260"}],"wp:attachment":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}