Get a formatted date in UNIX

Here are a set of quick commands on getting a formatted Date to be used in timestamping some files:

Simple Date

date +%Y-%m-%d

Results in:
2017-03-31

Simple Date and Time

date +%Y-%m-%d_%H-%M-%S

Results in:
2017-03-31_18-14-04

Timestamping a file

So if you like to timestamp a file you can just do the following:

mv someFile someFile_`date +%Y-%m-%d_%H-%M-%S`

If you ls afterwards, you have a nicely timestamped file:
someFile_2017-03-31_18-18-25

Nice!

Leave a comment