LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 04-19-2009, 07:19 PM   #1
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Rep: Reputation: 41
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.
 
Old 04-19-2009, 08:03 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 04-19-2009, 08:38 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 04-19-2009, 09:15 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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 ;}
 
Old 04-19-2009, 10:38 PM   #5
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
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.
 
Old 04-19-2009, 10:44 PM   #6
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by unSpawn View Post
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.
 
Old 04-19-2009, 10:46 PM   #7
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by Tinkster View Post
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.
 
Old 04-19-2009, 10:49 PM   #8
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by Mountain View Post
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!
 
Old 04-19-2009, 10:57 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by Mountain View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Samba: automatically create a domain user's home directory sfoo Linux - Newbie 1 03-04-2008 03:26 PM
How to create a script that will automatically detect a SATA and IDE hdd cyclonous_gt Linux - General 3 05-10-2007 01:02 PM
How to create a bash script to automatically disown a process. jon_k Linux - Software 5 06-19-2005 05:53 AM
need help with a script to automatically create a subdirectory verbatim Linux - Newbie 6 04-26-2005 12:51 AM
How to create a tiny script and make it start automatically tchigi Linux - Newbie 2 11-28-2003 03:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration