How to set (find) atime in seconds?
With GNU find, you can use -amin
instead of -atime
. As you might guess, it is "File was last accessed n minutes ago."
That said, be aware that most modern systems default to using the relatime
option for filesystems, which saves metadata writes by only updating if the file was actually modified since last access or if a threshold (usually 24 hours) has passed.
So, you will probably either want to change that for the filesystem in question, or else look for another approach. incrond is a handy way to set up scripts to fire on filesystem activity without needing to write your own daemon.
Note that when you do -mtime <timespec>
, the <timespec>
checks the age of the file at the time find
was started.
Unless you run it in a very small directory tree, find
will take several milliseconds (if not seconds or hours) to crawl the directory tree and do a lstat()
on every file. So having a precision of shorter than a second doesn't necessarily make a lot of sense.
Also note that not all file systems support time stamps with subsecond granularity.
Having said that, there are a few options.
With the find
of many BSDs and the one from schily-tools
, you can do:
find . -atime -1s
To find files that have been last accessed less than one second ago (compared to when find
was started).
With zsh
:
ls -ld -- **/*(Dms-1)
For subsecond granularity, with GNU tools, you can use a reference file whose atime you set with touch
:
touch -ad '0.5 seconds ago' ../reference
find . -anewer ../reference
Or with recent versions of perl
:
perl -MTime::HiRes=lstat,clock_gettime -MFile::Find -le '
$start = clock_gettime(CLOCK_REALTIME) - 0.5;
find(
sub {
my @s = lstat $_;
print $File::Find::name if @s and $s[8] > $start
}, ".")'