How to: Add time/date stamp in Linux – Linux Tutorial

Sometimes you need to add a handy time-stamp to your bash output. For back-up purposes, archiving-purposes or just for experimenting. Doing so is simple, you just need to add the $date command to your output.

First, some basics (All the variables are case-sensitive):

  • date – Display the current date
  • %y – Add current year
  • %m – Add current month
  • %d – Add current day
  • %H – Add current hour
  • %M – Add current minute
  • %S – Add current second

Now, some examples:

  • echo $(date +%H:%M:%S) > Current\ Time.txt – Echo current time to a txt file
  • cp -r $USER/minecraft_server/world $USER/minecraft_server/world-backup-$(date +%y-%m-%d) – Make a backup of a folder with the current date
  • date ‘+DATE: %m/%d/%y%nTIME:%H:%M:%S’ – Print the current date and time to the terminal

Leave a Reply

Your email address will not be published. Required fields are marked *