LinuxCommandLibrary

ts

Timestamp standard input to standard output

TLDR

Add a timestamp to the beginning of each line

$ [command] | ts
copy

Add timestamps with microsecond precision
$ [command] | ts "[%b %d %H:%M:%.S]"
copy

Add [i]ncremental timestamps with microsecond precision, starting from zero
$ [command] | ts -i "[%H:%M:%.S]"
copy

Convert existing timestamps in a text file (eg. a log file) into [r]elative format
$ cat [path/to/file] | ts -r
copy

SYNOPSIS

ts [-r] [-s] [-i] [-m] [-u] [-H] [-p] [-h] [-v] [format]

PARAMETERS

-r, --relative
    Outputs timestamps relative to the first timestamp received. If input pauses, it outputs the time since the last timestamp.

-s, --absolute
    Outputs timestamps as seconds since the Unix epoch. This is the default behavior if no format options are specified.

-i, --iso
    Outputs timestamps in ISO 8601 format (e.g., YYYY-MM-DDTHH:MM:SS, with milliseconds implied).

-m, --milliseconds
    Includes milliseconds in the output timestamp. This option is implicitly enabled when using the -i (ISO) format.

-u, --utc
    Uses Coordinated Universal Time (UTC) for timestamps instead of local time.

-H, --human
    Outputs timestamps in a human-readable relative format, such as "1 hour ago" or "2 minutes ago".

-p, --pretty
    Outputs timestamps in a pretty duration format, e.g., "1h 2m 3s".

format
    Specifies a custom output format using strftime(3) syntax. When this is used, other format options (-r, -s, -i, -m, -u, -H, -p) are ignored.

-h, --help
    Displays a brief help message and exits.

-v, --version
    Displays version information for the ts command and exits.

DESCRIPTION

The ts command, part of the moreutils package, reads lines from standard input and prepends them with a timestamp before writing to standard output. It is a highly versatile tool for logging and debugging, allowing users to easily add time information to any stream of data. The timestamps can be configured in various formats, including seconds since epoch, ISO 8601, relative to the first input, or a human-readable duration.

CAVEATS

The ts command is not part of the standard core utilities on all Linux distributions; it is typically found in the moreutils package, which may need to be installed separately. It processes input line by line, so it's best suited for text streams rather than binary data. In relative mode (-r), if there's a significant pause in input, the time difference calculated will be from the last received line, not necessarily the overall start of the process.

COMMON USAGE PATTERNS

ts is frequently piped with other commands to add timestamps to their output. For example:

tail -f /var/log/syslog | ts
Adds default timestamps to live syslog output.

ping -i 3 localhost | ts -r
Adds timestamps showing time elapsed between each ping reply.

echo "Hello World" | ts '%H:%M:%.S'
Adds a timestamp with hours, minutes, and seconds including milliseconds using a custom strftime format string.

HISTORY

The ts command is part of the moreutils collection, a set of small, useful Unix utilities that operate on common data streams. This collection was created by Joey Hess, a Debian developer, to provide tools that complement the standard Unix toolkit without being overly complex or having many dependencies. ts addresses a common need for timestamping output from other commands, especially in scripts and during debugging, becoming a de facto standard for this specific task within the Unix-like ecosystem.

SEE ALSO

date(1), strftime(3), logger(1)

Copied to clipboard