Locating a file by date on your Linux machine

published Oct 03, 2011 12:12   by admin ( last modified Oct 03, 2011 12:12 )

Summary

To find an AVI file that was last modified 31st of July 2010:

locate -0 *.AVI|xargs -0 stat -c "%y%n"|grep 2010-07-31

A friend just popped up in a chat window and asked if I still had a video from his birthday that I gave him on a memory stick that he had since mislaid.

I asked when the event was and he said 31st of July 2010, over a year ago.

Well, the video was gone from the camera's memory card but it might me on my 1.5 TB hard disk, but where, and what would it be called? Now I could use find to go through the entire hard disk and use its flags for creation and modification date, but that might be a bit slow. The locate command however is super fast since it has a premade index, but alas only searches for file names.

Luckily the number of video files, specifically those suffixed with .AVI from the camera, are a small subset of the files on the disk. So if I could just filter those out and then run stat to get some file info I could thereafter run grep with the date string. Running this:

locate *.AVI|xargs stat -c "%y%n"|grep 2010-07-31

...did not work since stat started stat'ing fragments of path names, so a couple of -0 switches were needed to make file names NULL terminated:

locate -0 *.AVI|xargs -0 stat -c "%y%n"|grep 2010-07-31
It returned one hit, the file I was looking for :-)