Unix Shell: find files by a date range

I needed to restore some files from an archive on UNIX, but only the files of a particular date-range were needed.ร‚ย  It took a few moments to find and figure out how I could easily extract files older than a particular date, or files from a particular date-range. This is how:

  1. Create a perimeter file, like so:
    touch -t yyyymmddHHMM marker_date

  2. List files older than the marker_date:
    find . -type f ! -newer marker_date -ls
    Of course, instead of `-ls’ parameter (to list), you can use `-print’ and a pipe to xargs to, for example, delete the selected files, etc.

Likewise, for a range of dates:

  1. Create the perimeter files:
    touch -t yyyymmddHHMM range_start
    touch -t yyyymmddHHMM range_end

  2. List the files between the range dates:
    find . -type f -newer range_start ! -newer range_end -ls

NOTE: For an even easier way to accomplish the same, see comment by Hampus below!

14 thoughts on “Unix Shell: find files by a date range”

  1. Hi,
    That’s very helpful ๐Ÿ™‚
    I would like to know if I could translate your post in French (with a backlink for sure)
    Thanks by advance
    Best Regards
    Pierre-Yves

  2. Hi Pierre! And sorry for the slow response… I need to tend to my blog more often! ๐Ÿ™‚

    Yes, absolutely! And a backlink would be great! ๐Ÿ™‚

  3. There is a more obvious/simple/straight-forward (but seldom mentioned/understood) method using newerXY where Y=t (I overlooked this option numerous times myself):

    Find all files last modified between 1nd of May and 30th of June 2012 (00:00 probably implicit)
    > find . -newermt 2012-05-01 ! -newermt 2012-07-01

    or, being more specific on the time:
    > find . -newermt “2012-06-15 08:13” ! -newermt “2012-06-15 18:20”

    References:
    * See “man find”, search for -newerXY
    * Tom hanses reply at http://www.cyberciti.biz/faq/linux-unix-osxfind-files-by-date/

  4. @Hampus, thanks for that piece of shell-fu! That’s definitely an easier way รขโ‚ฌโ€œ no need to mess with date files. I added a pointer to your comment in my original post.

  5. @hb You will still probably want to define some kind of a time range. There are very few files that have been created exactly two hours ago. Perhaps you would like to find files created, say, between two and three hours ago, or between two and 2:15 hours ago, or even all files from two hours and earlier (in a specific directory). Using the format suggested by Hampus, here are couple of examples (assuming it would be 14:00 at the time these commands are run):

    find . -newermt "2013-12-16 11:00" ! -newermt "2013-12-16 12:00"
    find . -newermt "2013-12-16 11:45" ! -newermt "2013-12-16 12:00"
    find . -newermt "1970-01-01 00:00" ! -newermt "2013-12-16 12:00"
    

    To count the results, just add “| wc -l” in the end, like so:

    find . -newermt "2013-12-16 11:00" ! -newermt "2013-12-16 12:00" | wc -l
    
  6. I found out that the solution using -newermt does not available on solaris.

    find: bad option -newermt

    find: [-H | -L] path-list predicate-list

    I think I still have to use the perimeter temporary file (:

  7. @Ville and @Hampus–Thanks, very useful stuff!

    @Dale–This is two years after you asked your question, but maybe the answer will be useful for someone else. Sort the returned list by date using the much under-appreciated xargs command. e.g.,

    find . -type f -iname \*.txt -newermt “2014-04-14 17:44” ! -newermt “2014-07-08 17:11” | xargs ls -ltrd

    Note a bit of a gotcha here: if you don’t specify “-type f”, then find may return some directories. When xargs pipes these directories to ls, it will list the contents of those directories, with the result that you may end up with a bunch of files in your output that are not from your specified time range. The way around this problem is to always use “-type f” in your find command and/or always specify the -d flag to ls (this forces ls to treat directories as ordinary files, rather than listing their contents).

Leave a Reply to Chandu Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.