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: date -r prints the modification time of a file in any format you like

Linux
~/tips/tip-linux-date-r-file-time

mv app.log "app-$(date -r app.log +%F).log"

Shows when a file was last changed, formatted the way your script needs it.

Most people reach for date to get the current time. Give it -r and a file name and it shows when that file was last modified, through the same format strings you already know. I find that a lot handier in scripts than picking the time out of ls -l.

-r (--reference) swaps “now” for the modification time of the file. Everything after the plus sign works as usual, so you can rename an old log after its own date in one line:

mv app.log "app-$(date -r app.log +%F).log"

For a log last written on 2 November 2026 that gives app-2026-11-02.log. The same trick works for a quick freshness check of a backup: %s prints seconds since 1970, so subtracting two of them gives the age in days:

echo $(( ($(date +%s) - $(date -r /mnt/user/backups/latest.tar +%s)) / 86400 )) days old

Put a check like that in a cron script and let it complain when the number gets bigger than you’d like. Add -u to get the time in UTC, or -Iseconds for an ISO 8601 stamp you can compare between machines.

The caveat is which time you get. It’s the modification time, not when the file was created, and a copy made without cp -p or cp -a gets a fresh one, so a restored backup can look brand new. Without -u it also follows the server’s time zone, which in Leiden means the hour shifts with summer time. And -r can’t be combined with -d: the manual says the options that pick the date to display are mutually exclusive, and date refuses the pair with an error.

Further reading: date(1) manual page, GNU coreutils: date invocation

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link