大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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 wit…

问题原文

原帖链接 · Moritz · CC BY-SA 4.0

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

}

已采纳解答

原帖链接 · asktyagi · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-07-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。