Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a constantly running script that I output to a log file: >> /var/log/logfile

I'd like to add a timestamp before each line that is appended to the log. Like:

Sat Sep 10 21:33:06 UTC 2011 The server has booted up.  Hmmph.

Is there any jujitsu I can use?

share|improve this question
See this questin.…. A couple answer would apply here. – Zoredache Sep 11 '11 at 0:43
For an awk/gawk solution see:… – User Nov 2 '12 at 17:31
up vote 40 down vote accepted

You can pipe the script's output through a loop that prefixes the current date and time:

./ | while IFS= read -r line; do echo "$(date) $line"; done >>/var/log/logfile

If you'll be using this a lot, it's easy to make a bash function to handle the loop:

adddate() {
    while IFS= read -r line; do
        echo "$(date) $line"

./ | adddate >>/var/log/logfile
./ | adddate >>/var/log/logfile
./ | adddate >>/var/log/logfile
share|improve this answer
What is "IFS= " for? – Nils Sep 15 '11 at 21:15
@Nils it's a trick to prevent read from trimming whitespace at the beginning and and of the line. It sets IFS (bash's Internal Field Separator, basically a list of whitespace characters) to empty for the read command. – Gordon Davisson Sep 15 '11 at 21:43
...and -r ignores the escape-character "\". This should really work in all cases - great pice of scripting. – Nils Sep 16 '11 at 20:47
@Nils it's not completely bulletproof, since some implementations of echo interpret escape sequences. If you really want it not to mess with the content (other than adding dates), replace the echo command with printf "%s %s\n" "$(date)" "$line" – Gordon Davisson Sep 17 '11 at 2:31

See "ts" from the Ubuntu "moreutils" pkg:

command | ts

Or, if $command does automatic buffering (requires expect-dev pkg):

unbuffer command | ts
share|improve this answer

You can simply echo the command outputs to the logfile. ie,

echo "`date -u` `./`" >> /var/log/logfile

It really works :)


[sparx@E1]$ ./ 
Hello Worldy
[sparx@E1]$ echo "`date -u` `./`" >> logfile.txt
[sparx@E1]$ cat logfile.txt 
Mon Sep 12 20:18:28 UTC 2011 Hello Worldy
share|improve this answer
Hmm isn't working for me. – Antonius Bloch Sep 12 '11 at 19:11
What are you getting when you execute the command ? – SparX Sep 12 '11 at 20:15
That puts a timestamp before the entire output of ''./'', not before each line. – clacke Mar 9 '15 at 15:01

The date command will provide that information

date -u
Sat Sep 10 22:39:24 UTC 2011

so you can

echo $(date -u) "Some message or other"

is that what you wanted ?

share|improve this answer
Using the date command was kind of what I had in mind, but I can't really add that to the script itself, so what I'm looking for is a way to change this line: " >> /var/log/logfile" to append the date. – Antonius Bloch Sep 10 '11 at 21:53
In that case redirect the output of your script to a named pipe and have a daemon listening for output which takes the script output and adds a date before writing it to a log file. You can probably modify the script I wrote here to do this. I would do it because it interests me but it's late in the UK and I have an early start tomorrow. – Iain Sep 10 '11 at 22:12

You mean like:

(date && >> /var/log/logfile
share|improve this answer
My god, people, everyone is doing back tick replacement, named pipes, etc. Just enclose both the date command and the script in parens! The guy who has the function has a legit case if there's multi-line output and the log needs to look pretty with the date on every line, but most of these solutions are overkill that doesn't use shell semantics. – cjc Sep 12 '11 at 21:11
That will only add the timestamp once per execution of The OP needs a timestamp per line. – Dave Forgac Oct 10 '12 at 14:10
although this doesn't answer the OP question I still found it useful info. – User Nov 2 '12 at 20:14

Make a file

#!/usr/bin/env bash
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`

When you need to send to log file use

#!/usr/bin/env bash
source /path/to/

echo "$TIMESTAMP Say what you are doing" >> $LOGFILE

do_what_you_want >> $LOGFILE

Log file will looks like

2013-02-03 18:22:30 Say what you are doing

So it will be easy to sort by date

share|improve this answer
Your '''' will run ''date'' exactly once, on ''source .../''. – clacke Mar 9 '15 at 15:03

Pipe a "sed": | sed "s|^|$('date') :: |" >> /var/log/logfile
share|improve this answer

Try this

 date +"%Y-%m-%d %T"

Call this timestamp function in every echo command:

echo "$(timestamp): write your log here" >> /var/log/<logfile>.log
share|improve this answer
@ shazbot:Thanks for editing, that was a typo error, i didnt notice. – Sanjay Yadav Jun 15 '15 at 7:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.