I have a script that checks a directory using
mdls* (to sort by added time) and them performs some operations. However,
mdls is a bit slow so I’d like to run it only when absolutely necessary (i.e. when directory contents have changed).
My idea is to run
mdls once and cache the results, the perform some operation to keep track of the state of the directory. On subsequent runs, I’d run the check operation again and compare the result to what I had. If different, rerun
mdls; if not, use the cache.
I’m thinking of either
du '/dir/path' | tail -1 (if the directory size remains equal to the byte, it’s likely the contents haven’t changed) or
ls '/dir/path' | md5'.
I’m currently more inclined to the
md5 solution, since it’s resilient to file renames. However, both of these solutions are hacky, and I wonder if there’s something more efficient (that is still fast).
Ultimately the solution will be used in a ruby script, if that helps.
* I’m not posting this to Ask Different because
mdls is not the point. It could be any other slow command.