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?
1 Answers
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

- 544,893

- 829,060
-mmin
option tofind
. You'll need to calculate the number of minutes difference from now to the ends of the time range. – Barmar Dec 30 '14 at 22:32