Let me start by saying I'm very new to linux and bash scripting, only been at this for 2 weeks so assume I know nothing!

For development reasons, I need to copy a log file to another location and name, here's my logrotate file:

/pub/share/logs/results.txt {
    rotate 30
        sh /pub/share/scripts/result_rotate.sh $1 > /dev/null

And the bash script is simply:


date=$(date +%Y%m%d)
cp $1-$date /pub/share/reports/data.log

I don't know if I have to put this in a script file or if I can just type that inside the postrotate block, but for now this works.

It passes the name of the file to be rotated but does so after it's already renamed it with the date extension, hence the date code above. Is this a safe way to do this? Could there ever be an instance where it might fail to match up properly?

1 Answer 1


Yes, why not, you can directly write in postrotate block:

cp $1-$(date +%Y%m%d) /pub/share/reports/data.log >/dev/null

in a worse situation where logrotation happened for example with today's date and postrotate posted to run in next day's date, so our cp command will not get that file to copy. for that you can get one day older file always instead of today's date rotated file:

cp $1-$(date -d'-1day' +%Y%m%d) /pub/share/reports/data.log >/dev/null

or of course you can copy those both files, meaning today's date and yesterday's date files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .