Often while writing shell scripts, we need to create files which can be distinguished from one another, what better way to do it other then times stamps.

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

Command

Format / result Command
YYYY-MM-DD date -I
YYYY-MM-DD_hh:mm:ss date +%F_%T
YYYYMMDD_hhmmss date +%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
YYYYMMSShhmmss date +%Y%m%d%H%M%S
YYYYMMSShhmmssnnnnnnnnn date +%Y%m%d%H%M%S%N
YYMMDD_hhmmss date +%y%m%d_%H%M%S
Seconds since UNIX epoch date +%s

How to Use

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