brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: olddir in logrotate moves rotated logs into a folder of their own

Logs
~/tips/tip-logs-logrotate-olddir

rotate 4

Keeps all rotated copies of a log in one separate directory instead of next to the live file.

A log folder with app.log, app.log.1, app.log.2.gz and so on gets crowded fast. I’d rather have the live file alone and the old copies somewhere else. logrotate has an option for that.

/var/log/myapp/*.log {
    weekly
    rotate 4
    compress
    missingok
    olddir /var/log/myapp/old
    createolddir 755 root root
}

With olddir, logs are moved into that directory for rotation, and all old versions end up there. A relative path is taken relative to the directory that holds the log file, and an absolute path is used as is.

The createolddir line creates the directory when it does not exist. It takes a mode and optionally an owner and group, and it assumes 0755 when you give no mode. If you leave that line out, create the directory yourself. nocreateolddir turns the creation off again.

Before it goes live, I’d test the file. The debug flag changes nothing and does not update the state file:

sudo logrotate -d /etc/logrotate.d/myapp

The caveat comes from the man page. The old directory must be on the same physical device as the log file being rotated, unless you use copy, copytruncate or renamecopy. If you want rotated logs on a bigger disk, add one of those. The noolddir option overrides olddir and rotates the logs in the directory where they normally live.

Further reading: logrotate(8) man page

More Logs tips sort -m app.log worker.log | less sed -E 's/[0-9]+/N/g' app.log | sort | uniq -c | sort -rn | head -n 15 awk '$1 >= "2026-11-30T02:00" && $1 < "2026-11-30T03:00"' app.log rotate 14 logger: All Logs tips (9) All topics

Link