{"id":985,"date":"2009-09-03T19:35:58","date_gmt":"2009-09-03T14:05:58","guid":{"rendered":"https:\/\/www.suramya.com\/blog\/?p=985"},"modified":"2022-06-16T15:21:33","modified_gmt":"2022-06-16T09:51:33","slug":"sort-a-file-list-by-date-in-linux-including-subdirectories","status":"publish","type":"post","link":"https:\/\/www.suramya.com\/blog\/2009\/09\/sort-a-file-list-by-date-in-linux-including-subdirectories\/","title":{"rendered":"Sort a file list by Date in Linux (Including Subdirectories)"},"content":{"rendered":"<p>I use Amarok to play music and I really like it, however my Playlist was created in Winamp\/XMMS which I initially thought that Amarok couldn&#8217;t read. So all the new music I got since I switched to Amarok was no longer being added to a playlist, I just added the new songs manually then used the select 50 random songs option to play music.<\/p>\n<p>Yesterday I figured out that Amarok does indeed read .m3u files (Winamp Play lists) so I wanted to add all the new music I have to the top of the old playlist. So basically I needed a list of all music in my system sorted by date. If you have been using Linux and are reasonably familiar with it then I think the first thing that came to your mind must have been: &#8220;Why can&#8217;t we just use ls -lRrt&#8221;? That&#8217;s pretty much what I thought. But unfortunately this command doesn&#8217;t work very well for what I wanted to do.<\/p>\n<p>After playing around for a while I finally managed to get the system to show me a list of all music files in my system sorted in reverse chronological order. The command I used for this is:<\/p>\n<p><code>find -name \"*.mp3\" -print0 | xargs -0 stat --format '%Y %n'|sort -r<\/code><\/p>\n<p>Now the explanation:<\/p>\n<ul>\n<li><i>find -name &#8220;*.mp3&#8221;<\/i> : returns a list of all mp3 files in the current directory and any subdirectory under it.<\/li>\n<li><i>-print0<\/i>: This tells <i>find<\/i> to use the ASCII NUL character instead of space to separate the filenames. If we don&#8217;t use this then xargs chokes on the spaces in file names.<\/li>\n<li><em>xargs -0<\/em>: This tells <i>xargs<\/i> to use the ASCII NUL character instead of space to separate the filenames. <\/li>\n<li><em>stat &#8211;format &#8216;%Y %n&#8217;<\/em>: This runs the stat command on every file returned by find. This command returns the Time of last modification as seconds since Epoch followed by the name of the file.\n<p>e.g. <\/p>\n<p><code>suramya@Wyrm:~$ stat --format '%Y %n' unison.log<br \/>\n1251802152 unison.log<br \/>\n<\/code><\/li>\n<li><i>sort -r<\/i>: sorts the list using the first column (the creation time) and displays the result.<\/li>\n<\/ul>\n<p>&#8212;&#8212;-<\/p>\n<p>Example execution result in my Scripts directory:<\/p>\n<pre>suramya@Wyrm:~\/bin$ ls -l\r\ntotal 2648\r\n-rwx------ 1 suramya suramya      77 2009-01-10 04:30 bookfixer.sh\r\n-rwx------ 1 suramya suramya      76 2009-01-10 04:30 cardDesigner.sh\r\n-rwx------ 1 suramya suramya     548 2009-01-10 04:30 change_wan.sh\r\n-rwx------ 1 suramya suramya     478 2009-01-10 04:30 compare_entire_dir.sh\r\n-rwx------ 1 suramya suramya    3462 2009-01-10 04:30 delay.pl\r\n-rwx------ 1 suramya suramya      66 2009-01-10 04:30 EditPlus\r\n-rwx------ 1 suramya suramya      88 2009-01-10 04:30 export_xterm\r\n-rwx------ 1 suramya suramya     203 2009-01-30 04:29 extract.sh\r\n-rwx------ 1 suramya suramya   82558 2009-01-10 04:30 lit2html\r\n-rwx------ 1 suramya suramya     320 2009-01-10 04:30 makedir.sh\r\n-rwx------ 1 suramya suramya      49 2009-01-10 04:30 mapsql\r\n-rwx------ 1 suramya suramya     369 2009-01-15 02:11 MuteUnmute.sh\r\n-rwx------ 1 suramya suramya 2350194 2009-01-10 04:30 pdftohtml\r\ndrwx------ 2 suramya suramya    4096 2009-01-10 04:30 Poet\r\ndrwx------ 2 suramya suramya    4096 2009-01-10 04:30 Remote\r\n-rwx------ 1 suramya suramya     109 2009-02-08 19:47 resize.sh\r\n-rwx------ 1 suramya suramya      48 2009-01-10 04:30 rootscan\r\n-rwx------ 1 suramya suramya     397 2009-01-10 04:30 S41firewall\r\n-rwx------ 1 suramya suramya     111 2009-02-21 20:15 sync_website.sh\r\n-rwx------ 1 suramya suramya     116 2009-01-10 04:30 text2img\r\n-rwx------ 1 suramya suramya     122 2009-01-10 04:30 uncompress.sh\r\n-rwx------ 1 suramya suramya  180580 2009-01-10 04:30 vcdgear\r\n<\/pre>\n<p><code><br \/>\nsuramya@Wyrm:~\/bin$ find -name \"*\" -print0 | xargs -0 stat --format '%Y %n'|sort -r<br \/>\n1235227520 .\/sync_website.sh<br \/>\n1235227520 .<br \/>\n1234102660 .\/resize.sh<br \/>\n1233269990 .\/extract.sh<br \/>\n1231965676 .\/MuteUnmute.sh<br \/>\n1231542033 .\/vcdgear<br \/>\n1231542033 .\/uncompress.sh<br \/>\n1231542033 .\/text2img<br \/>\n1231542033 .\/S41firewall<br \/>\n1231542033 .\/rootscan<br \/>\n1231542033 .\/Remote\/StarFlight<br \/>\n1231542033 .\/Remote\/Firestorm<br \/>\n1231542033 .\/Remote\/CyberNibble<br \/>\n1231542033 .\/Remote<br \/>\n1231542033 .\/Poet\/poet.tar.gz<br \/>\n1231542033 .\/Poet<br \/>\n1231542033 .\/pdftohtml<br \/>\n1231542033 .\/mapsql<br \/>\n1231542033 .\/makedir.sh<br \/>\n1231542033 .\/lit2html<br \/>\n1231542033 .\/export_xterm<br \/>\n1231542033 .\/EditPlus<br \/>\n1231542033 .\/delay.pl<br \/>\n1231542033 .\/compare_entire_dir.sh<br \/>\n1231542033 .\/change_wan.sh<br \/>\n1231542033 .\/cardDesigner.sh<br \/>\n1231542033 .\/bookfixer.sh<br \/>\n<\/code><\/p>\n<p>As you can see, the &#8216;sync_website.sh&#8217; script is the latest script in that directory. <\/p>\n<p>Hope you find it useful. <\/p>\n<p>&#8211; Suramya<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use Amarok to play music and I really like it, however my Playlist was created in Winamp\/XMMS which I initially thought that Amarok couldn&#8217;t read. So all the new music I got since I switched to Amarok was no longer being added to a playlist, I just added the new songs manually then used [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"","activitypub_status":"","footnotes":""},"categories":[9,13,2],"tags":[],"class_list":["post-985","post","type-post","status-publish","format-standard","hentry","category-computer-hintstips","category-linuxunix-related","category-techie-stuff"],"_links":{"self":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/comments?post=985"}],"version-history":[{"count":4,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/985\/revisions"}],"predecessor-version":[{"id":5099,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/985\/revisions\/5099"}],"wp:attachment":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/media?parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/categories?post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/tags?post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}