Linux - Software This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
04-19-2009, 07:19 PM
|
#1
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Rep:
|
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.
For example, given these subdirectories:
2009.03.21_00.50.02
2009.03.21_11.31.42
2009.03.22_07.55.47
2009.03.22_10.59.20
2009.04.03_23.21.38
2009.04.15_11.06.08
2009.04.16_00.50.02
2009.04.18_20.35.26
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.
Last edited by Mountain; 04-19-2009 at 07:33 PM.
|
|
|
04-19-2009, 08:03 PM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Code:
new=$(ls -tr|tail -n 1)
rm current
ln -s ${new} current
How do the new dirs get created? Wouldn't it be cleaner to have
the process creating them update the link?
Cheers,
Tink
|
|
|
04-19-2009, 08:38 PM
|
#3
|
Moderator
Registered: May 2001
Posts: 29,415
|
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.
|
|
|
04-19-2009, 09:15 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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 ;}
|
|
|
04-19-2009, 10:38 PM
|
#5
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
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.
|
|
|
04-19-2009, 10:44 PM
|
#6
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
Quote:
Originally Posted by unSpawn
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:
find ./ -maxdepth 1 -type d -printf "%A@ %p\n" | \sort -r|head -1
1240101326.0000000000 ./2009.04.18_20.35.26
Is that what you expected?
Is the $2 selecting the second value ("./2009.04.18_20.35.26")?
Thanks.
|
|
|
04-19-2009, 10:46 PM
|
#7
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
Quote:
Originally Posted by Tinkster
Wouldn't it be cleaner to have
the process creating them update the link?
|
Yes, unfortunately, I do not have that option. Another (sophisticated) program creates the directories.
|
|
|
04-19-2009, 10:49 PM
|
#8
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
Quote:
Originally Posted by Mountain
I tried this out from the parent directory. Here's what I did:
find ./ -maxdepth 1 -type d -printf "%A@ %p\n" | \sort -r|head -1
1240101326.0000000000 ./2009.04.18_20.35.26
Is that what you expected?
Is the $2 selecting the second value ("./2009.04.18_20.35.26")?
Thanks.
|
A little more experimenting got me this:
Code:
# find ./ -maxdepth 1 -type d -printf "%A@ %p\n" | \sort -r|head -1 | awk '{print $2}'
./2009.04.18_20.35.26
I hope you guys don't consider this spam. But I do understand what it is doing now. It is very cool!
|
|
|
04-19-2009, 10:57 PM
|
#9
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by Mountain
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).
Cheers,
Tink
Last edited by Tinkster; 04-19-2009 at 11:07 PM.
|
|
|
All times are GMT -5. The time now is 07:40 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|