Linux Date/Timestamp

I often find myself needing to append a date (and sometimes a time stamp) to files. 

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

$ date +%Y%m%d

Example output is “20120528” or Year = 2012, Month=05 (May) and day = 28.

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

$date +%H%M%S

Example output is “193347” or Hour = 19, Minute = 33 and second = 47 (19:33.47). 

The two can be combined:

$date +%Y%m%d-%H%M%S

Example output is 20120528-193347. Note the separation of the date and time with a dash.

You can use these commands in your shell scripts. For example, this will create an empty test file called “Test.txt.20120528-193347”:

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

As you can see, quite handy for organising files.