How to find the newest files in directory?
Suppose i have a directory in /tmp/myDir where files are constantly created, modified and destroyed by a daemon. I need to periodically (every 45 seconds) monitor last created files. To do that i must determine which files were created after the last check. What would be the best way to do that from a c daemon?
If possible, the method should avoid opening every file i开发者_运维百科n the directory during each check.
inotify
is pretty nifty... If you can't use that, I would suggest obtaining the st_mtime
for each file in the directory, and comparing it to (time(0) - 45)
. If it's greater, then it's within your 45 second window. Put your executable on the crontab
if you're rather not create a continually running daemon.
call scandir
and use the filter
callback function to leave only relevant files. you will probably have to use fstat
on each one of the files.
精彩评论