开发者

How to find the newest files in directory?

开发者 https://www.devze.com 2023-02-24 05:18 出处:网络
Suppose i have a directory in /tmp/myDir where files are constantly created, modified and destroyed by a daemon.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消