简体   繁体   English

符号链接到文件夹中的最新文件

[英]Symbolic link to latest file in a folder

I have a program which requires the path to various files. 我有一个程序,需要各种文件的路径。 The files live in different folders and are constantly updated, at irregular intervals. 这些文件位于不同的文件夹中,并且会不定期地不断更新。

When the files are updated, they change name, so, for instance, in the folder dir1 I have fv01 and fv02 . 更新文件后,它们会更改名称,例如,在文件夹dir1我有fv01fv02 Later on the day someone adds fv02_v1 ; 当天晚些时候有人添加了fv02_v1 ; the day after someone adds fv03 and so on. 有人添加fv03之后的第二天等等。 In other words, I always have an updated file but with different name. 换句话说,我总是有一个更新的文件,但名称不同。

I want to create a symbolic link in my "run" folder to these files, such that said link always points to the latest file created. 我想在我的“运行”文件夹中为这些文件创建一个符号链接,这样所述链接始终指向创建的最新文件。

I can do this in Python or Bash, but I was wondering what is out there, as this is hardly an uncommon problem. 我可以用Python或Bash做到这一点,但我想知道那里有什么,因为这不是一个罕见的问题。

How would you go about it? 你会怎么做?

Thank you. 谢谢。

Juan 胡安

PS. PS。 My operating system is Linux. 我的操作系统是Linux。 I currently have a simple daemon (Python) that looks every once in a while (refreshes every minute) for the latest file. 我目前有一个简单的守护进程(Python),每隔一段时间(每分钟刷新一次)查看最新文件。 Seems kind of an overkill to me. 对我来说似乎有点矫枉过正。

Unless there is some compelling reason that you have left unstated (eg thousands of files in the directory) just do it the way you suggest with a script sorting the files by modification time. 除非有一些令人信服的理由说你没有说明(例如目录中的数千个文件),否则就像你建议用脚本按修改时间排序文件一样。 There is no secret method that I am aware of. 我没有秘密的方法。

You could write a daemon using inotify to monitor your directories and immediately set your links but that seems like overkill. 您可以使用inotify编写一个守护进程来监视您的目录并立即设置您的链接,但这似乎有点矫枉过正。

Edit : I just saw your edit. 编辑 :我刚看到你的编辑。 Since you have the daemon already, inotify might not be such a bad idea. 由于你已经拥有守护进程,因此inotify可能不是一个坏主意。 It would be somewhat more efficient than constantly querying since the OS will tell you when something in your directories has changed. 它会比不断查询更有效,因为操作系统会告诉您目录中的某些内容何时发生了变化。

I don't know python well enough to point you to anything specific but there must exist a wrapper for inotify. 我不太了解python足以指向任何特定的东西,但必须存在inotify的包装器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM