Often while writing shell scripts, we need to create files that can be distinguished from one another, what better way to do it than timestamps?

below you will find a the bash variable to add to the name of you file.

Command

FormatResult Command
YYYY-MM-DDdate -I
YYYY-MM-DD_hh:mm:ssdate +%F_%T
YYYYMMDD_hhmmssdate +%Y%m%d_%H%M%S
YYYYMMDD_hhmmss (UTC version)date --utc +%Y%m%d_%H%M%SZ
YYYYMMDD_hhmmss (with local TZ)date +%Y%m%d_%H%M%S%Z
YYYYMMSShhmmssdate +%Y%m%d%H%M%S
YYYYMMSShhmmssnnnnnnnnndate +%Y%m%d%H%M%S%N
YYMMDD_hhmmssdate +%y%m%d_%H%M%S
Seconds since UNIX epochdate +%s

How to Use

now=$(date +%Y%m%d_%H%M%S)
fileName="file-${now}.ext"

If you are writing a new shell script, I suggest this boilerplate to you. Check it out Boilerplate for creating a Shell Script