Hints and tips on unix

These are some helpful hints on various topics.


Prefix each output line with timestamp

Imagine the case: you were writing a program or a script that outputs some information in the console. Just when everything is almost complete you realize that you actually need timestamp on each message output. For example you need to know how long it takes to backup each of your users' home dirs. The solution is to go through all output commands and wrap them in a function that appends timestamps. Believe it or not, there's a lazier way to do this - it's a simple bash one-liner:
> ./your_program | while read line; do stamp=`date`; echo "$stamp $line"; done

You can try it with
> du -sc /home | while read line; do stamp=`date`; echo "$stamp $line"; done