List files from time X to time Y

8,398

Most systems don't track files' creation date.

If yours does and has GNU/BSD find, for example if it's OSX, you can use its -newerBt predicate to compare the file's creation (“birth”) time with a specific time.

find -newerBt "29-dec-2014 18:00" ! -newerBt "30-dec-2014 18:00"

This traverses subdirectories recursively. If you only want files in the current directory, make this

find -mindepth 1 -maxdepth 1 -newerBt "29-dec-2014 18:00" ! -newerBt "30-dec-2014 18:00"

If your system doesn't track creation times, you might use the modification time instead. Replace -newerBt by -newermt.

Neither ls nor stat offer a way to filter files by time. All they can do is list files' timestamps, and filtering the output for a time range isn't easy. find is the right tool for this job.

POSIX find can only compare the timestamp of a file with that of another file, so the portable way to filter files in a time interval is to create boundary files. You can only filter on the modification time, POSIX doesn't define a creation time.

touch -t 201412291800 /tmp/start
touch -t 201412301800 /tmp/stop
find . -newer /tmp/start ! -newer /tmp/stop

or if you don't want to recurse

find . ! -name . -prune -newer /tmp/start ! -newer /tmp/stop
Share:
8,398

Related videos on Youtube

Vish
Author by

Vish

Updated on September 18, 2022

Comments

  • Vish
    Vish almost 2 years

    How to list out all files created between from 'x' to 'y' time? I want to list out files created between "29-dec-2014 18:00" to "30-dec-2014 18:00". Using ls or stat?

    • vembutech
      vembutech over 9 years
      Hi, Please find the following url which might gives you a solution for your requirement, "superuser.com/questions/442490/…"
    • MariusMatutiae
      MariusMatutiae over 9 years
      @vembutech That concerns either access or modification times, not creation times.
    • MariusMatutiae
      MariusMatutiae over 9 years
      @Gilles: an incorrect remark on your part. The OP asked for something that is impossible; a proper answer is to explain why that is impossible, not explain how to do something different. But at any rate, suit yourself.
    • Barmar
      Barmar over 9 years
      Do you mean modified between those times? Unix filesystems normally only record 3 timestamps: last access (atime), last modification (mtime), and last inode change (ctime). Creation time is not kept (OS X is a notable exception, HFS+ has creation times).
    • Barmar
      Barmar over 9 years
      If modification times are sufficient, see the -mmin option to find. You'll need to calculate the number of minutes difference from now to the ends of the time range.
    • Mark Wagner
      Mark Wagner over 9 years
      Not enough information is given. For example, the ext4 filesystem records creation time. Is that what you are using?