
$FILE_LIST=$(find . -type f -cmin -5 -maxdepth 1)
for file in ${FILE_LIST} do
ln -f ${file} ~/tmp/${file}
done
##其中find命令是核心
##-cmin -5表示只查找5分钟之内创建的文件
##-maxdepth 1表示查找的目录深度,1表示只查找当前目录,如果不指定-maxdepth将递归查找
##ln -f ${file} ~/tmp/${file}是将查找到的文件硬链接到~/tmp目录
##你可以根据自己的需要再做相应的修改
JDK 7 的nio2 WatchService可以监听文件系统。
Oracle官方教程链接 http://docs.oracle.com/javase/tutorial/essential/io/notification.html
样例代码:
import static java.nio.file.StandardWatchEventKinds.*Path path = Paths.get("/home")
WatchService watchService = FileSystems.getDefault().newWatchService()
WatchKey watchKey = path.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY)
/*
private boolean notDone = true
while(notDone){
try{
WatchKey watchKey = watchService.poll(60,TimeUnit.SECONDS)
List<WatchEvent.Kind<?>> events = watchKey.pollEvents()
for(WatchEvent event : events){
//WatchKey watchable returns the calling Path object of Path.register
Path watchedPath = (Path) watchKey.watchable()
//returns the event type
StandardWatchEventKinds eventKind = event.kind()
//returns the context of the event
Path target = (Path)event.context()
}
if(!watchKey.reset()){
...handle situation no longer valid
}
}catch(InterruptedException e){
Thread.currentThread().interrupt()
}
}
*/
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)