Symbolic link to latest file in a folder
Asked Answered
C

1

6

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. Later on the day someone adds fv02_v1; the day after someone adds fv03 and so on. 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.

How would you go about it?

Thank you.

Juan

PS. My operating system is Linux. I currently have a simple daemon (Python) that looks every once in a while (refreshes every minute) for the latest file. Seems kind of an overkill to me.

Clavicle answered 22/1, 2010 at 1:16 Comment(5)
I'm wondering if this shouldn't be more of a server fault (serverfault.com) question.Owlet
The OP is not looking for the command syntax. The OP is looking for a simpler or more elegant way to solve the problem than writing a Python or Bash script that automatically does the ln periodically.Owlet
He wants to do it in bash or python. Seems like a programming question to me.Predikant
I deleted my earlier comment as I mis-understood...apologies...Mussolini
I am willing to continue with my Python daemon, but there must be another way!Clavicle
P
3

Unless there is some compelling reason that you have left unstated (e.g. 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.

Edit: I just saw your edit. Since you have the daemon already, inotify might not be such a bad idea. 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.

Predikant answered 22/1, 2010 at 1:52 Comment(1)
Thank you, Duck. I was finally unable to use inotify for libc version reasons, but you put me on a good way to provide a much more general solution when we finally upgrade. Just for the record, there is a Python inotify module. Thanks again! (+1 and accepted)Clavicle

© 2022 - 2024 — McMap. All rights reserved.