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
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"
If you are writing a new shell script, I suggest this boilerplate to you. Check it out Boilerplate for creating a Shell Script