Date Formatting in Linux Shell Scripts with the date Command

A flat vector illustration of a minimal terminal window displaying a date command, overlaid with a simple geometric calendar page. Text: LINUX SHELL DATE FORMAT.

We may earn a referral fee at no additional cost to you when you shop through the links on this page.

Whether you are stamping a backup filename, writing a log line, or just printing today’s date in a script, the Linux date command formats it however you need. This DevAnswers guide covers the format specifiers, the practical recipes (timestamps, ISO 8601, epoch), date arithmetic, and the one portability gotcha that bites people moving scripts to a Mac.

The date Command and Format Specifiers 

Run date with no arguments and you get the current date and time in a default, human-readable form. To control the output, add a + followed by a format string of % specifiers:

Terminal
date +"%Y-%m-%d %H:%M:%S"
Output
2026-06-05 14:23:45

Each specifier expands to one piece of the date. The ones you will reach for most:

Specifier Meaning Example
%Y Year, four digits 2026
%m Month (01-12) 06
%d Day of month (01-31) 05
%H Hour, 24-hour (00-23) 14
%M Minute (00-59) 23
%S Second (00-60) 45
%A / %a Weekday, full / short Friday / Fri
%B / %b Month name, full / short June / Jun
%F Full date, same as %Y-%m-%d 2026-06-05
%T Time, same as %H:%M:%S 14:23:45
%s Seconds since the Unix epoch 1780669425
%z / %Z Timezone offset / name +0000 / UTC
%j Day of the year (001-366) 156

As the table shows, %F and %T are handy shorthands, so the command above is just date +"%F %T". Because date reads the system clock, its output reflects your configured timezone.

One thing to watch: %A and %B follow your system’s locale, so on a non-English server they print translated names. When a script needs stable, predictable output (for English names, or for anything you will sort or parse), force the C locale: LC_ALL=C date +"%A" returns the weekday in English (Friday for the date above), never a localized variant.

Timestamps, ISO 8601, and the Unix Epoch 

The most common scripting use is a timestamped filename, built with command substitution so the date is filled in at runtime:

Terminal
tar -czf "backup-$(date +%F).tar.gz" /var/www
# -> backup-2026-06-05.tar.gz

That ISO-style %F date sorts correctly in a directory listing, which is exactly why it suits scheduled backups and database dumps. For a full ISO 8601 timestamp, use the -Iseconds option instead of spelling it out:

Terminal
date -Iseconds
Output
2026-06-05T14:23:45+00:00

And when you need a single sortable number, for instance to compare two moments or stamp a record, %s prints the Unix epoch: the number of seconds since 1 January 1970 UTC. (Two events in the same second share a value, so it is not a uniqueness guarantee.)

Terminal
date +%s
Output
1780669425

The same trick cleans up log output: prefixing a message with $(date +%T) or a full -Iseconds stamp turns an unreadable stream into something you can actually scan later, the way cron job logs record when each run happened.

Date Arithmetic and Reformatting with -d 

The -d (--date) option is the GNU date superpower: instead of the current time, it shows the time described by a string. That string can be relative, which makes “yesterday” or “a week from now” trivial:

Terminal
# (run on 2026-06-05)
date -d "yesterday" +%F
date -d "2 days ago" +%F
date -d "+7 days" +%F
Output
2026-06-04
2026-06-03
2026-06-12

It also reformats any date you hand it into whatever layout you want, which is handy for turning a machine date into a human one:

Terminal
LC_ALL=C date -d "2024-03-07" +"%A, %d %B %Y"
Output
Thursday, 07 March 2024

And it accepts @N to mean N seconds since the epoch, which is how you turn a stored timestamp back into something readable:

Terminal
date -d "@1709805825" -u +"%Y-%m-%d %H:%M:%S"
Output
2024-03-07 10:03:45

The -u flag works with or without -d and prints (or interprets) the time in UTC, which is the safer choice for logs and anything shared across timezones. One caution: -d only displays a time. Changing the system clock is a different, privileged operation that uses -s, so you can experiment with -d freely without touching anything.

macOS Differences and the bash printf Built-in 

This is the gotcha. Several of the flags above are GNU (Linux) extensions: -d arithmetic, -Iseconds, and -s are all GNU-specific. The date on macOS and the BSDs does not understand them; macOS uses adjustment flags instead, for example date -v-1d for “yesterday”. If a script must run on both, install GNU coreutils on the Mac (the commands then become gdate) or stick to plain +FORMAT strings, which are portable everywhere.

There is also a faster option for the common case. Bash has a built-in printf with a %(FORMAT)T specifier, where -1 means “now”. Because it is built into the shell, it formats the time without launching the external date program, which matters in a loop that runs thousands of times (note it needs bash 4.2+, so it will not work on macOS’s default bash 3.2):

Terminal
printf '%(%Y-%m-%d %H:%M:%S)T' -1; echo
# or assign straight to a variable (no echo needed):
printf -v now '%(%F)T' -1
Output
2026-06-05 14:23:45

A handful of recipes come up constantly. For a filename-safe stamp that includes the time, date +%Y%m%d_%H%M%S gives something like 20260605_142345 with no spaces or colons to escape. date -R prints an RFC 5322 stamp suitable for email headers. Combining -d with a format answers questions like “what weekday was a given date?” in one line: date -d 2024-12-25 +%A prints Wednesday. And since the format string is just text with specifiers mixed in, you can write literal words too, so date -d "2024-12-25" +"Week %V of %G" reads naturally as Week 52 of 2024 (%G is the ISO week-numbering year, which is the correct partner for %V).

Summary 

The core pattern is date +FORMAT, with a few standalone shortcuts like -Iseconds: reach for %F and %T for everyday dates and times, -Iseconds for ISO 8601, and %s for the epoch. Use $(date +%F) to timestamp filenames, -d "STRING" for arithmetic and reformatting, and -u to stay in UTC. Just remember that -d is GNU-only, and that bash’s printf '%(...)T' is the fork-free shortcut when speed matters. With those few pieces in hand, almost any date-handling task in a shell script comes down to picking the right format string for the job.

Support DevAnswers
Written, tested and maintained by one developer. Your support keeps these guides accurate and ad-light.
Buy me a coffee

Comments

Leave a reply