LinuxCommandLibrary

ts.1s

Add timestamp to input lines

SYNOPSIS

ts [options] [format]

PARAMETERS

-i, --infile format
    Specify a strftime(3) format to parse the input time. This allows ts to parse timestamps from the input itself, instead of prepending timestamps to each line. Must be used in conjunction with --pre. Multiple -i arguments will be tried in the order given. In this mode, input lines with timestamps that don't match any of the formats will be printed without modification.

-s, --skip n
    Skip first n lines.

-f, --force-color
    Force color output even when stdout is not a TTY.

-r, --relative
    Use relative timestamps, displaying the time elapsed since the previous line.

-m, --monotonic
    Use monotonic timestamps, displaying the time elapsed since the start of the command. Needs the CLOCK_MONOTONIC clock.

-o, --outfile format
    Specify a strftime(3) format to use for the output time. (default: %b %d %H:%M:%S)

-p, --pre
    Preserve any timestamps found on the input, instead of adding a new timestamp to the beginning of the line.

-u, --undate
    Treat the date as the start of the day, instead of the current time.

-z, --use-systemd
    Use systemd's Journal Export format. This affects time output (always UTC and iso-8601) and adds the _TRANSPORT=stdout field.

--help
    Display help message and exit.

--version
    Display version information and exit.

DESCRIPTION

The ts command adds timestamps to each line of input it receives from standard input. It can format the timestamp in various ways using strftime-style formatting. This is useful for logging and debugging purposes, allowing you to easily see when each line of output was generated. The command is commonly used in pipes where output from other commands needs to be time-stamped for better analysis or recording of events as they occur. The timestamp can be customized to include various date and time components, such as seconds, milliseconds, timezones, etc., offering flexibility in how the timestamp is presented. The format string is provided as an argument; if not specified, a default format is applied.
It is part of the moreutils package on most distributions.

FORMAT STRING

The format string used by ts follows the same syntax as the strftime(3) function. It allows for precise control over the output format of the timestamps. See the strftime(3) man page for a complete list of formatting options. Common examples include %Y-%m-%d %H:%M:%S for a full date and time or %s for the number of seconds since the epoch.

EXAMPLES

  • Basic Usage: command | ts
    Prepends a default timestamp to each line of output from command.
  • Custom Format: command | ts '%Y-%m-%d %H:%M:%S'
    Prepends a timestamp with the specified format to each line.
  • Relative Timestamps: command | ts -r
    Displays the time elapsed since the previous line of output.
  • Systemd Journal Format: command | ts -z
    Outputs in the systemd journal export format with UTC timestamps.

SEE ALSO

date(1), strftime(3)

Copied to clipboard