1

I was looking at this other question:

Directory last modified date

which tells me how stuff works, but doesn't really give any solution. How would I get the time which a directory was last modified? I would be using this time to determine whether i need to clear a cache, whose contents are calculated from the contents of the directory.

One alternative would be to recurse through every file and subfolder, but that would probably completely negate any benefits of caching in the first place. As far as I can tell, the "mtime" and other metadata for files/folders are cached somewhere and do not actually require a file-read every time you want to check (which is the reason why checking mtime on every memcached file is still much faster than loading the files themselves, even when the files are tiny).

So... is there any nice, fast way of getting the last-modified timestamp of an entire directory?

Community
  • 1
  • 1
Li Haoyi
  • 15,330
  • 17
  • 80
  • 137

1 Answers1

0

Well you can always do ls -la to get the timestamp of files of directories, but I don't think that's what you're asking.

You didn't include what language or environment you wanted this solved in, but it's rather easy in php:

$filename = 'includes/';
if (file_exists($filename)) {
    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
//echos includes/ was last modified: September 06 2011 17:51:10.

That information does get cached by the operating system, but you can wipe that cache with:

http://www.php.net/manual/en/function.clearstatcache.php

Landon
  • 4,088
  • 3
  • 28
  • 42
  • I'm working in Python, but i figured if it was doable there would be an analog in every language. That only gets the last time the "directory" was modified, and won't pick up changes in individual files, right? – Li Haoyi Sep 08 '11 at 10:22
  • Well, yes and no, the filemtime method will work on directories or files. My example has it set up to check one directory. However, it would be very easy to check the files. Just use opendir() to read all the files in a directory and you can easily check those files as well. – Landon Sep 08 '11 at 18:20
  • The problem is the directory is pretty deep (5-6 levels?) with quite a lot of files inside. Unless the file list from opendir() is also cached, it checking-for-cache-invalidation may cost more than caching is giving me in the first place. I'll run some benchmarks. – Li Haoyi Sep 08 '11 at 18:40
  • The depth of the directories doesn't matter, just call clearstatcache() before you check the stats and you'll be fine. I've done this in the past, and there isn't much of a time cost to these functions. – Landon Sep 08 '11 at 20:56