Bash tips

From DarkWiki
Jump to: navigation, search

Prepending lines of text with a timestamp

To put a date/time against each line (perhaps as part of logging), you can use the ts command.

$ echo -e "foo\nbar\nbaz" | ts '[%Y-%m-%d %H:%M:%S]'
[2011-12-13 22:07:03] foo
[2011-12-13 22:07:03] bar
[2011-12-13 22:07:03] baz

If it is not already present on your system, it can be added using apt:

sudo apt install moreutils