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 -d understands phrases like 'next friday' and '2 days ago'

Linux
~/tips/tip-shell-date-d-relative-dates

date -d '2 days ago' +%F

Turns plain-English dates such as yesterday or next friday into a real date on the command line.

Working out what date it was three days ago is a job for a computer, and GNU date will do the sum if you ask in English.

date -d '2 days ago' +%F
date -d 'next friday' +%F
date -d '2026-12-24 +1 week' +%F

The -d option takes a date string. The manual says last stands for -1, this for 0, and first and next both for 1. A trailing ago flips the sign of the unit before it, so 2 days ago is minus two days. The result is formatted with +%F, which gives YYYY-MM-DD.

It is handy for backup scripts that need a file name or a cutoff, like deleting everything older than a given day, because you avoid doing date maths in the shell.

There is one caveat worth knowing: months and years are fuzzy units. The manual’s own example is that 2022-12-31 -1 month might come out as 2022-12-01, because 2022-11-31 does not exist. If the day of the month matters, step back by days instead.

This is GNU date. The BSD date on macOS uses different options, so on a Mac use gdate from coreutils, or write the same thing with date -v-2d.

Further reading: GNU Coreutils: relative items in date strings

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