Score:3

How to automatically change the name of a file on a daily basis

jo flag

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

My CentOS7 Server logs all Maillogs, via Logrotate, 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:

Maillog-20230721

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:

Maillog-1

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

Maillog-2

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?

This is my current config:

/var/log/maillog{

daily

rotate 365

postrotate

   Systemctl restart rsyslog.service

endscript

mailfirst

olddi /var/log/old_maillog

}
David avatar
cw flag
I just want to check that you realize that the numbers in your filename above are giving the date for the log. 20230721 is 2023, 07, 21, or the 21st of July 2023. Right now you should be getting a unique file name each day. Numbering the logs with 1, 2, 3, ... etc. is fine if it serves some other purpose for you, but you already have an automatic filename that changes on a daily basis. There are other simple commands you could use to, for example, count the number of log files or find the oldest logs in a directory.
Score:5
in flag

Please refer logrotate man page option "start count"

start count

This is the number to use as the base for rotation. For example, if you specify 0, the logs will be created with a .0 extension as they are rotated from the original log files. If you specify 9, log files will be created with a .9, skipping 0-8. Files will still be rotated the number of times specified with the count directive.

Moritz avatar
jo flag
"start count" seems to be the right thing, but I don't really get, how the parameter works. When I add `start 0` to the config and force a rotation, the logged file is still named `maillog - 20230724`. Am I doing something wrong? I added my config to the original question.
Moritz avatar
jo flag
Nevermind, I just made it work. Thank you very much!
Score:3
jo flag

As @asktyagi already stated. The right parameter to use is "start count".

Initially it didn't work for me, that's because I didn't disable the parameter for "use data extension for rotated files", which is called nodateext.

So the working config looks like this:

/var/log/maillog{

daily

rotate 365

postrotate

   Systemctl restart rsyslog.service

endscript

mailfirst

olddi /var/log/old_maillog

start 1

nodateext
}

This config saves logs on a daily basis and renames the extension to the number, that is equivalent to how old the log is.

So on day 1 it's called - maillog.1

And on day 2 it's called - maillog.2

And so on.

Mark Morgan Lloyd avatar
us flag
Kudos for reporting back with your experiences.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.