If(Condition) Rename a file with (Date+Time) Stamp

Hi!

Please see our current script:

#!/usr/bin/ksh
if (egrep "This string is found in the log" /a01/bpm.log)
then
  mailx -s "Error from log" me@email.com, him@email.com </a01/bpm.log
fi

To the above existing script, we need to add the following change:
1) After finding the string, the bpm.log file must be renamed to bpm<date><time>.log so that the log is saved with a new filename

Can you help me with this ?

Thank you!!!

See man mv

I think a simple mv would do the work, isn't it?

mv /a01/bpm.log /a01/bpm$(date +%m%d%y_%T).log

Yes. I knew it should be mv for sure. But i did not know about adding the Date+Time stamp.

Will try this.

Thank you!

Maybe something like this ?

#!/usr/bin/ksh
if ( egrep "This string is found in the log" /a01/bpm.log 2>/dev/null )
then
mailx -s "Error from log" me@email.com, him@email.com </a01/bpm.log
mv bpm.log bpm$(date +'%Y%m%d%H%M%S').log
fi

The file must be renamed like this:
bpm021520110130.log

Nope, you should then change the date formatting to something like :

$(date +'%m%d%Y%H%M')

use man date to check the correct format you need

1 Like

The previous format you gave fit much better.

Thank you!