I am new to Linux/CentOS and right now I am running into a little problem.

My CentOS7 Server logs all Maillogs on a daily basis and moves them to the folder /var/log/old_maillogs. Right now the logs are saved with a name similar to this:


I would like their names to change, based on how old they are, so that if the Log is one day old it's named:


And one the day after that, the files name changes to:


And so on.

Is this even possible, if so, what would be the smartest way to get it working? I have read a lot about scripts that get executed once per day, but then again, how do I code the script, so that it recognizes how many logs are in the folder?

  • 5
    Do not reinvent the wheel, use logrotate which is written for exactly this purpose (it will even compress your old logs automatically). Read man logrotate, if something is still unclear edit your question to ask for specifics.
    – Panki
    Commented Jul 21, 2023 at 9:39

1 Answer 1


As said by @Panki in the comments, use logrotate.

I haven't tried it, but this configuration (save it in /etc/logrotate.d/maillog) should do what you want:

/var/log/maillog {
    rotate 365
    olddir /var/log/old_maillogs

You must log in to answer this question.

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