When monitoring network connectivity, long-running compilations, or streaming debug sessions in the Linux terminal, standard command outputs frequently omit timestamps. Running commands such as ping, tail -f, or custom background scripts produces lines of text without chronological reference points, making it exceedingly difficult to correlate intermittent network drops or service crashes with system log events.
The standard Linux command-line utility ts (Timestamp), part of the popular moreutils package, solves this problem. It acts as an inline stream filter that reads standard input line by line and prepends a timestamp to each line before passing it to standard output.
Installing moreutils in Linux
The ts command is distributed as part of the moreutils collection of Unix tools:
- Debian / Ubuntu / Linux Mint:
sudo apt update && sudo apt install moreutils - Fedora / RHEL / Rocky Linux:
sudo dnf install moreutils - Arch Linux:
sudo pacman -S moreutils
Basic Usage: Adding Timestamps to Terminal Streams
To use ts, simply pipe any command’s standard output through the utility:
ping 1.1.1.1 | ts
Output:
Nov 07 04:45:10 64 bytes from 1.1.1.1: icmp_seq=1 ttl=58 time=12.4 ms
Nov 07 04:45:11 64 bytes from 1.1.1.1: icmp_seq=2 ttl=58 time=11.8 ms
Nov 07 04:45:12 64 bytes from 1.1.1.1: icmp_seq=3 ttl=58 time=12.1 ms
By default, ts formats timestamps in standard month, day, hour, minute, and second format (%b %d %H:%M:%S).
Customising Date and Time Formats
The ts utility accepts standard strftime conversion specifiers, allowing you to format timestamps in ISO 8601 or millisecond precision:
- ISO 8601 Standard Format:
ping 8.8.8.8 | ts '%Y-%m-%dT%H:%M:%S' - Sub-Second Millisecond Precision:
Outputs fractional seconds (e.g.ping 8.8.8.8 | ts '%H:%M:%.S'04:45:10.829142), which is essential for diagnosing microsecond latency spikes during high-throughput network auditing.
Displaying Incremental Elapsed Time (-i Flag)
When benchmarking performance or identifying slow-running operations in a script, you often need to measure the duration between subsequent output lines rather than wall-clock time. The -i option instructs ts to display elapsed time since the previous line:
./build_pipeline.sh | ts -i '%.S'
Output:
0.000124 Compiling core library...
4.218501 Core compilation completed.
0.812405 Linking shared objects...
12.491200 Optimising binary artifacts...
This output instantly reveals that binary optimisation consumed 12.49 seconds, pinpointing the exact bottleneck in your build chain.
Measuring Cumulative Elapsed Time (-s Flag)
To track total stopwatch time elapsed since the command began executing, pass the -s flag:
curl -s https://example.com/large-archive.tar.gz | ts -s '%.S'
Each line will reflect total seconds and microseconds accumulated from the moment the pipeline was initiated.
Preserving Timestamps in Log Files
You can combine ts with tee to display timestamped console output while permanently appending records to an audit file:
dmesg -w | ts '%Y-%m-%d %H:%M:%S' | tee -a kernel_monitor.log
Using ts provides an instantaneous, zero-overhead method to inject rigorous chronological data into any command line process across all POSIX environments.