15
Nov

Prefix command output in a shell with timestamp

   Posted by: Kristian Rønningen in linux

So I was writing this script that logged certain things to a file using a regular redirect. My script was running various commands that outputted a random amount of lines, all in random formats. I wanted to have a timestamp prefixing all of that, similar to what you get in logs written by syslog. I searched around a bit, and ended with this:

1
ls -l / | awk '{now=strftime("%Y-%m-%d %T %Z"); print now" "$0}'

Of course if you want a different timestamp, you just change the arguments you pass to strftime. Quite handy!

Tags: , , ,

This entry was posted on Tuesday, November 15th, 2011 at 15:57 and is filed under linux. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

I'd love to hear from you, so please leave a comment below!

Name (*)
Mail (will not be published) (*)
URI
Comment