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
Related videos on Youtube
Author by
David
Updated on September 17, 2022Comments
-
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 about 13 yearsWhy is this any easier than the solutions we gave? It seems like our solutions are much better, and faster, than this.
-
David about 13 yearsThis 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 about 5 yearsI 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.