How to Use the ts Command to Add Timestamps to Output

Zachary Lee
Level Up Coding
Published in
5 min readMay 4, 2023

--

Learn how to easily add timestamps to your output with the ts command and streamline your workflow.

Photo by Lon Christensen on Unsplash

The ts command is a versatile and powerful utility for Linux programmers, providing an easy way to add timestamps to any output, such as logs or command outputs. This command is part of the moreutils package and comes with a few options that allow users to customize the timestamps according to their needs. In this guide, we will delve into the ts command, its features, and how to use it efficiently.

Installing the moreutils Package

Before using the ts command, you must install the moreutils package. It may come with some Linux distributions, but if not you need to install it manually. For instance, in Debian and Ubuntu-based systems, run the following command:

sudo apt-get install moreutils

Once the package is installed, you can start using the ts command.

Understanding Standard Streams, Pipes, and Redirection

To use the ts command effectively, it’s essential to have a solid understanding of standard streams (stdin, stdout, and stderr), pipes, and redirection in Linux.

Standard Streams

Linux processes have three primary communication channels: stdin (standard input), stdout (standard output), and stderr (standard error). These streams help processes communicate with the user, files, or other processes.

  • stdin: This is the default input channel for a process. It typically receives data from the keyboard but can be redirected from files or other processes.
  • stdout: This is the default output channel for a process. It displays the process’s output on the terminal but can be redirected to files or other processes.
  • stderr: This is the default error output channel for a process. It displays error messages on the terminal but can also be redirected to files or other processes.

Pipes and Redirection

Pipes and redirection are mechanisms used to send data from one process to another or to a file.

  • Pipes (|): Pipes are used to connect the stdout of one process to the stdin of another. This way, the output of the first process serves as input for the second process.
  • Redirection: Redirection is used to send the output (stdout or stderr) of a process to a file or to merge two streams.

Basic Usage of the ts Command

The primary function of the ts command is to add timestamps to each line of input. When ts is called without any input, it waits for input from the keyboard, adding a timestamp to the input line and printing it to stdout (standard output).

Here’s a simple example of using the ts command to add timestamps to ping output:

$ ping google.com | ts   
May 03 11:40:32 PING google.com (172.217.163.46): 56 data bytes
May 03 11:40:32 Request timeout for icmp_seq 0
May 03 11:40:33 Request timeout for icmp_seq 1
May 03 11:40:34 Request timeout for icmp_seq 2
May 03 11:40:35 Request timeout for icmp_seq 3
May 03 11:40:36 Request timeout for icmp_seq 4

The pipe takes the output from the ping command and sends it to ts as input, allowing ts to add a timestamp to each line of input.

Exploring ts Command Options

There are several options available for the ts command that allow users to customize its behavior.

Incremental Timestamps

The -s and -i options provide incremental timestamps instead of absolute timestamps. However, their incrementation start points differ.

  • -s: This option shows incremental timestamps from the start of the command execution. To use this option, run the following command:
$ ping google.com | ts -s
00:00:01 PING google.com (172.217.163.46): 56 data bytes
00:00:01 Request timeout for icmp_seq 0
00:00:02 Request timeout for icmp_seq 1
00:00:03 Request timeout for icmp_seq 2
00:00:04 Request timeout for icmp_seq 3
00:00:05 Request timeout for icmp_seq 4
  • -i: This option displays incremental timestamps from the previous line of output. Here is an example using the ping command with incremental timestamps between each line:
$ ping google.com | ts -i
00:00:01 PING google.com (172.217.163.46): 56 data bytes
00:00:00 Request timeout for icmp_seq 0
00:00:01 Request timeout for icmp_seq 1
00:00:01 Request timeout for icmp_seq 2
00:00:01 Request timeout for icmp_seq 3
00:00:01 Request timeout for icmp_seq 4

Formatting Timestamps

Here are some common format specifiers:

  • %b: Abbreviated month name
  • %d: Day of the month
  • %H: Hour (00-23)
  • %M: Minute (00-59)
  • %S: Second (00-60)
  • %Y: Year with century
  • %Z: Time zone

For instance:

$ ping google.com | ts "%Y-%m-%d %H:%M:%S"
2023-05-03 11:45:37 PING google.com (172.217.163.46): 56 data bytes
2023-05-03 11:45:37 Request timeout for icmp_seq 0
2023-05-03 11:45:38 Request timeout for icmp_seq 1
2023-05-03 11:45:39 Request timeout for icmp_seq 2
2023-05-03 11:45:40 Request timeout for icmp_seq 3

Advanced Use Cases and Tips

Beyond its basic usage, the ts command can be used for more advanced purposes, such as analyzing logs, monitoring, etc.

Analyzing Logs with the ts Command

To analyze logs effectively, it is essential to have accurate timestamps. Combining the ts command with other utilities such as grep, awk, or sed, allows for powerful log analysis.

For example, suppose you have a log file application.log and want to filter out all ERROR messages with a timestamp:

$ cat application.log | grep 'ERROR' | ts
May 03 11:46:05 ERROR: Failed to connect to database
May 03 11:46:05 ERROR: Invalid user credentials
May 03 11:46:05 ERROR: Timeout while processing request

Monitoring with the ts Command

The ts command can be used to monitor the output of a long-running process, such as a server or application, by adding timestamps to the output. This can be beneficial when trying to identify patterns or anomalies in the process’s performance. For example, you can use the ts command to monitor a server’s CPU usage:

$ vmstat 1 | ts

Conclusion

The ts command is a powerful utility for Linux programmers that allows them to add timestamps to any output, making it a valuable tool for debugging and monitoring. Have fun!

Thanks for reading. If you like such stories and want to support me, please consider becoming a Medium member. It costs $5 per month and gives unlimited access to Medium content. I’ll get a little commission if you sign up via my link.

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

--

--