1

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?

Vish
  • 11
  • 1
  • 1
  • 2
  • Hi, Please find the following url which might gives you a solution for your requirement, "http://superuser.com/questions/442490/command-to-find-files-for-specific-time-range" – BDRSuite Dec 30 '14 at 08:48
  • @vembutech That concerns either access or modification times, not creation times. – MariusMatutiae Dec 30 '14 at 08:59
  • @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. – MariusMatutiae Dec 30 '14 at 21:46
  • 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 Dec 30 '14 at 22:29
  • 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. – Barmar Dec 30 '14 at 22:32
  • Not enough information is given. For example, the ext4 filesystem records creation time. Is that what you are using? – Mark Wagner Dec 30 '14 at 23:22

1 Answers1

2

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