Console9

How to add timestamps to rotated log filenames with Logrotate

Configure the Logrotate dateext and dateformat directives to append date stamps to rotated log file names instead of numeric suffixes.

Configure Logrotate to append date stamps to rotated log file names for easier identification and chronological sorting.

Prerequisites

  • Linux system with Logrotate installed.
  • Root or sudo access to edit files in /etc/logrotate.d/.
  • An existing Logrotate configuration file for the target application.

Step-by-Step: Add Timestamps to Rotated Filenames with Logrotate

  1. Open the Logrotate configuration file for the target application. This example uses Nginx:

    sudo nano /etc/logrotate.d/nginx
  2. Add the dateext and dateformat directives inside the log file block. Logrotate appends a date suffix instead of a numeric counter ( .1, .2, .3) to rotated filenames:

    /var/log/nginx/*.log {
        daily
        rotate 14
        compress
        dateext
        dateformat -%Y%m%d
        extension .log
        missingok
        notifempty
    }

    Logrotate renames rotated files using the date format. With this configuration, access.log becomes access-20260308.log after rotation.

    Logrotate with Nginx log file updated for timestamps

    The dateformat directive uses strftime format specifiers:

    SpecifierMeaningExample
    %YFour-digit year2026
    %mTwo-digit month03
    %dTwo-digit day08
    %sUnix timestamp1773187200

    The extension directive preserves the .log file extension. Logrotate places the date stamp before the extension: access-20260308.log.

  3. Save the configuration file and exit the editor.

  4. Test the updated Logrotate configuration in debug mode:

    sudo logrotate -d /etc/logrotate.d/nginx

    Logrotate displays the renamed filenames with date stamps in the debug output.

How to Verify Timestamps Appear in Rotated Filenames

Force a rotation and list the log directory:

sudo logrotate -f /etc/logrotate.d/nginx
ls -la /var/log/nginx/

Logrotate creates rotated files with date-stamped names such as access-20260308.log.gz.

Common Issues When Adding Timestamps to Log Filenames with Logrotate

Logrotate reports "destination already exists."The dateext directive generates one filename per day. If Logrotate runs multiple times in the same day, the second run fails because the target filename already exists. Use dateformat -%Y%m%d-%s to include a Unix timestamp for uniqueness.

The date format produces unexpected filenames.Verify the dateformat string uses valid strftime specifiers. Note that %m is months and %M is minutes. Using %M instead of %m produces minute-based suffixes instead of monthly ones.

The file extension appears in the wrong position.Without the extension directive, Logrotate appends the date stamp after the full filename: access.log-20260308. Add extension .log to place the date before the extension: access-20260308.log.

For a full reference of all Logrotate directives, see the Logrotate directives reference.