Wednesday, 15 June 2011

Aide Memoire - Adding the date/time stamp to a filename in a script

I'll be making further use of this later this week, as I look to automate an IBM Tivoli Directory Integrator Assembly Line via a Unix cron job.

I've still got a lot of learning about cron to do, including the use of the crontab command ( for user-specific jobs ) and the /etc/crontab file, and it's associated pals: -


However, as part of my uber cool bash script, I wanted to be able to automatically backup and then copy out a log file. Here we go: -

#! /bin/sh

cd /opt/IBM/TDI/V7.0/tdisol/TDI 
mv logs/ibmdi.log logs/ibmdi.log-$(date +%F-%T) 
/opt/IBM/TDI/V7.0/ibmdisrv -c /opt/IBM/TDI/V7.0/tdisol/TDI/PwdExpire.xml -r pwdExpiry 
cp logs/ibmdi.log /tmp/ibmdi_execution-$(date +%F-%T)

The use of the -$(date +%F-%T) element of the filename appends the date and time, which is just what I wanted.

Sweet, cool and neat

No comments: