0

I have a logging system, which writes logfiles on a daily base into subdirectories like the following:

/var/log/xyz/2015/03/10/log.log

In my home dir (or anywhere else), i want a softlink to the latest logfile somhow like this.

ln -s /var/log/xyz/CURRENT_YEAR/CURRENT_MONTH/CURRENT_DAY/log.log ~/log.log

Is it possible to get suche a dynamic link?

2 Answers2

2

I don't know of any way to make a dynamic symbolic link like you're looking for directly, but another option would be to set an environment variable or alias in your .bashrc file (assuming you're using bash).

export currentlog=$(date '+/var/log/xyz/%Y/%m/%d/log.log')

Then to use the environment variable, just say $currentlog rather than ~/log.log .

For example (putting files in my home directory rather than /var/log):

# export currentlog=$(date '+/home/userid/%Y/%m/%d/log.log')
# echo "TEST LOG INFO" > /home/userid/2015/03/11/log.log
# cat $currentlog
TEST LOG INFO
# ls -l $currentlog
-rw-rw-r--. 1 userid userid 0 Mar 11 13:16 /home/userid/2015/03/11/log.log
# grep TEST $currentlog
TEST LOG INFO
0

Yes, you should be able to do that quite easily with command substitution in a cron job, edit your user cron with the following command:

crontab -e

Then add the following entries, which will run at 9am and 9:01 am respectively.

0 9 * * * rm ~/log.log
1 9 * * * ln -s /var/log/xyz/$(date +%Y)/$(date +%m)/$(date +%d)/log.log ~/log.log

Or, alternatively, run them in sequence at the same time:

0 9 * * * rm ~/log.log; ln -s /var/log/xyz/$(date +%Y)/$(date +%m)/$(date +%d)/log.log ~/log.log

This will produce a symlink like:

/var/log/xyz/2015/03/11/log.log => ~/log.log

If the formatting is different to the above (e.g. using the word March rather than the number 03) then take a look at this guide to figure out how to change the formatting:

http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

Hope that helps!

Alex Berry
  • 2,307
  • 13
  • 23
  • the crux of the answer is the syntax for creating the symlink; would probably be clearer to ignore cron in the answer. Any reader aught to be capable of copying and pasting a command into crontab. – AD7six Mar 11 '15 at 19:27
  • I suppose, although the question asked whether it could be dynamic, rather than just the commands for the symlink creation. By dynamic I took that he always wanted the link to be up to date, so I thought the easiest solution was a daily cron to ensure that. – Alex Berry Mar 11 '15 at 19:49
  • Sure, but you've dedicated most of the real-estate of your answer to explaining how to edit cron - that's appropriate for e.g. superuser, not really here though. – AD7six Mar 12 '15 at 10:30