Image for post
Image for post

Generating date and time stamps from the command line

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`

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store