Generating date and time stamps from the command line

Praj Basnet
1 min readNov 20, 2020

--

Often when working with shell scripts you’ll need to append a date or time stamp to a file name (e.g. archiving logs).

The following will generate the current date in the format YYYYMMDD which is the most suitable format for chronologically sorting files.

date +%Y%m%d
> 20201121

Similarly the following will output the current time in the format HH24MMSS (24 hour time)

date +%H%M%S
> 094457

Combining the two will give you a date-time stamp (this example adds a dash between the date and time to separate them).

date +%Y%m%d-%H%M%S
> 20201121-094457

You can use these commands in your shell scripts. For example, this will create an empty test file called test.txt.20201121–094457:

touch test.txt.`date +%Y%m%d-%H%M%S`

--

--