When creating backups or log files, I like to name the files with a timestamp, ie. the date plus the time.
I use the date command to produce timestamps in the appropriate format, but I find the format specifier a bit long-winded and difficult to remember – is %m minutes or month?
There is a better way… date -I
To make the strings sort in a sane manner, I find it best to use a format with the timestamp elements in descending order of size, eg. YYYYMMDDhhmmss which is produced with a format specifier like this:
# date +%Y%m%d%H%M%S
But as I've mentioned, I find that to be a pain to remember. Wouldn't it be great if there was a single switch that produced a suitable timestamp format?
Well, it turns out that there is: --iso-8601 or -I, with the seconds modifier:
# date -Iseconds
According to NEWS in the coreutils distribution:
date accepts the new option --rfc-3339=TIMESPEC. The old --iso-8602 (-I)
option is deprecated; it still works, but new applications should avoid it.
date, du, ls, and pr's time formats now support new %:z, %::z, %:::z
specifiers for numeric time zone offsets like -07:00, -07:00:00, and -07.
The –rfc-3339 option is similar, but it includes a space in the output, which I prefer to avoid:
# date --rfc-3339 seconds
So, I'll be continuing to use -Iseconds