Is there a way to insert a timestamp in a file in Ubuntu?

5,826

Solution 1

You can use the date command.

date >> my_file.txt

Where my_file.txt is the file to put the timestamp into.

Look at the manual page for strftime(3) (man 3 strftime) to see some date formatters you can use. For instance:

date +%l:%M >> my_file.txt

Will output something like 9:37 (Hour:Minute) to the text file.

Solution 2

Use this command at your shell prompt (or within your shell script):

  • date >> /var/log/my_log_file.log
Share:
5,826

Related videos on Youtube

David
Author by

David

Updated on September 17, 2022

Comments

  • David
    David over 1 year

    I often find myself needing to type out a timestamp into a text file. Is there a way to quickly insert the current timestamp into a file in Ubuntu?

  • Wuffers
    Wuffers about 13 years
    Why is this any easier than the solutions we gave? It seems like our solutions are much better, and faster, than this.
  • David
    David about 13 years
    This is more flexible since you can insert the current time into any position of the file from the clipboard. This way I'm not limited to having to append the time to the end of the file. Your solutions are indeed easier and faster if you are content with the limits of appending via command line as opposed to insertion via keyboard shortcut. Insert was a keyword for me.
  • ryanjdillon
    ryanjdillon about 5 years
    I don't understand why the down vote. There are many ways to get to Detroit, and sometimes some roads are closed. So long as it is a correct and functional answer that doesn't duplicate others, it is a valuable addition.