Need a script to automatically create/update a symlink to the most recent directory
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Need a script to automatically create/update a symlink to the most recent directory
I want to make a shell script which will create or update a symlink (named "current") so that it automatically points to the subdirectory with the most recent date.
I want the script to create or update a symlink like this:
current -> 2009.04.18_20.35.26/
the symlink will reside at the same level as the directories 2009.04.18_20.35.26, etc.
When the script is run next, if there is a new directory named e.g., 2009.04.20_16.15.06, (and this is the most recent) the symlink should be updated to:
current -> 2009.04.20_16.15.06/
I have no idea how to do this. Any suggestions? Thanks.
Might 'ls -tr' not fail if the directory doesn't only contain recently created subdirectories? I'm thinking 'find /some/dir -maxdepth 1 -type d -printf "%A@ %p\n" | \sort -r|head -1|awk '{print "ln -sf",$2,"current"}'|sh' though that's prolly too convoluted and not -print0 resisant.
It might if the resolution gets too small, or if the dirs get written
to after after creation.
Thinking about it again: if those dirs are the ONLY content
of that particular directory, and considering that their names
are pretty much ISO normalised (punctuation aside) an
$(ls | tail -n 1 )
should suffice ;}
Thanks for the great suggestions. This helps a lot.
It would be nice if it will work if I happen to stick a readme file or some other misc files in the parent folder. Mostly the directories are the only content, but that might not always be true.
However, the directory names will always conform exactly to the pattern I have shown.
I don't guess ls will show only directories... at least the man page doesn't seem to show such an option.
Might 'ls -tr' not fail if the directory doesn't only contain recently created subdirectories? I'm thinking 'find /some/dir -maxdepth 1 -type d -printf "%A@ %p\n" | \sort -r|head -1|awk '{print "ln -sf",$2,"current"}'|sh' though that's prolly too convoluted and not -print0 resisant.
I tried this out from the parent directory. Here's what I did:
Thanks for the great suggestions. This helps a lot.
It would be nice if it will work if I happen to stick a readme file or some other misc files in the parent folder. Mostly the directories are the only content, but that might not always be true.
However, the directory names will always conform exactly to the pattern I have shown.
I don't guess ls will show only directories... at least the man page doesn't seem to show such an option.
No, there isn't. You could either use find for this (as
unspawn suggested [I wouldn't actually bother with the timestamps]),
or be specific in the criteria for ls ... e.g.
ls 200?.??.??_??.??.??
(of course, this again only works if only dirs follow that naming convention).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.