Suramya's Blog : Welcome to my crazy life…

September 3, 2009

Sort a file list by Date in Linux (Including Subdirectories)

Filed under: Computer Tips,Linux/Unix Related,Techie Stuff — Suramya @ 7:35 PM

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’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.

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: “Why can’t we just use ls -lRrt”? That’s pretty much what I thought. But unfortunately this command doesn’t work very well for what I wanted to do.

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:

find -name "*.mp3" -print0 | xargs -0 stat --format '%Y %n'|sort -r

Now the explanation:

  • find -name “*.mp3” : returns a list of all mp3 files in the current directory and any subdirectory under it.
  • -print0: This tells find to use the ASCII NUL character instead of space to separate the filenames. If we don’t use this then xargs chokes on the spaces in file names.
  • xargs -0: This tells xargs to use the ASCII NUL character instead of space to separate the filenames.
  • stat –format ‘%Y %n’: 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.

    e.g.

    suramya@Wyrm:~$ stat --format '%Y %n' unison.log
    1251802152 unison.log

  • sort -r: sorts the list using the first column (the creation time) and displays the result.

——-

Example execution result in my Scripts directory:

suramya@Wyrm:~/bin$ ls -l
total 2648
-rwx------ 1 suramya suramya      77 2009-01-10 04:30 bookfixer.sh
-rwx------ 1 suramya suramya      76 2009-01-10 04:30 cardDesigner.sh
-rwx------ 1 suramya suramya     548 2009-01-10 04:30 change_wan.sh
-rwx------ 1 suramya suramya     478 2009-01-10 04:30 compare_entire_dir.sh
-rwx------ 1 suramya suramya    3462 2009-01-10 04:30 delay.pl
-rwx------ 1 suramya suramya      66 2009-01-10 04:30 EditPlus
-rwx------ 1 suramya suramya      88 2009-01-10 04:30 export_xterm
-rwx------ 1 suramya suramya     203 2009-01-30 04:29 extract.sh
-rwx------ 1 suramya suramya   82558 2009-01-10 04:30 lit2html
-rwx------ 1 suramya suramya     320 2009-01-10 04:30 makedir.sh
-rwx------ 1 suramya suramya      49 2009-01-10 04:30 mapsql
-rwx------ 1 suramya suramya     369 2009-01-15 02:11 MuteUnmute.sh
-rwx------ 1 suramya suramya 2350194 2009-01-10 04:30 pdftohtml
drwx------ 2 suramya suramya    4096 2009-01-10 04:30 Poet
drwx------ 2 suramya suramya    4096 2009-01-10 04:30 Remote
-rwx------ 1 suramya suramya     109 2009-02-08 19:47 resize.sh
-rwx------ 1 suramya suramya      48 2009-01-10 04:30 rootscan
-rwx------ 1 suramya suramya     397 2009-01-10 04:30 S41firewall
-rwx------ 1 suramya suramya     111 2009-02-21 20:15 sync_website.sh
-rwx------ 1 suramya suramya     116 2009-01-10 04:30 text2img
-rwx------ 1 suramya suramya     122 2009-01-10 04:30 uncompress.sh
-rwx------ 1 suramya suramya  180580 2009-01-10 04:30 vcdgear


suramya@Wyrm:~/bin$ find -name "*" -print0 | xargs -0 stat --format '%Y %n'|sort -r
1235227520 ./sync_website.sh
1235227520 .
1234102660 ./resize.sh
1233269990 ./extract.sh
1231965676 ./MuteUnmute.sh
1231542033 ./vcdgear
1231542033 ./uncompress.sh
1231542033 ./text2img
1231542033 ./S41firewall
1231542033 ./rootscan
1231542033 ./Remote/StarFlight
1231542033 ./Remote/Firestorm
1231542033 ./Remote/CyberNibble
1231542033 ./Remote
1231542033 ./Poet/poet.tar.gz
1231542033 ./Poet
1231542033 ./pdftohtml
1231542033 ./mapsql
1231542033 ./makedir.sh
1231542033 ./lit2html
1231542033 ./export_xterm
1231542033 ./EditPlus
1231542033 ./delay.pl
1231542033 ./compare_entire_dir.sh
1231542033 ./change_wan.sh
1231542033 ./cardDesigner.sh
1231542033 ./bookfixer.sh

As you can see, the ‘sync_website.sh’ script is the latest script in that directory.

Hope you find it useful.

– Suramya

3 Comments »

  1. Clever way of using stat to sort the list, I fiddled a bit with ls and sort and by not getting what I wanted I found your post using google. ๐Ÿ™‚

    Nice share. ๐Ÿ™‚

    Comment by Renaud — January 5, 2012 @ 8:45 PM

  2. Note the sort wont work if the file time stamps cross before/after unix time 9999999999.

    Try zero-padding the unix time with: `stat –format ‘%010Y %n’`

    Also `stat –format ‘%010Y %y %n’` works nicely giving the human-readable data/time also.

    Comment by Rob — January 9, 2012 @ 4:17 PM

  3. Hi Rob,

    Thanks for the tip. Will try it out.

    – Suramya

    Comment by Suramya — January 10, 2012 @ 4:27 PM

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress