ts

text processingLinux/Unix
The ts command is one of the most frequently used commands in Linux/Unix-like operating systems. ts Add timestamp to each line of input

Quick Reference

Command Name:

ts

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

ts [options] [arguments]

Common Use Cases

    Syntax

    ts [options] [format]

    Options

    Option Description
    -r, --reference=FILE Use FILE's timestamp as the time of the first line of input
    -s, --set=STRING Set time described by STRING (e.g., '2012-11-01 21:41:34')
    -i, --incremental-only Don't print absolute timestamps, only the delta with previous line
    -m, --milliseconds Include milliseconds in timestamp
    --help Display help and exit
    --version Output version information and exit

    Format String: You can specify a custom format string according to strftime(3) format. For example:

    • %H:%M:%S - Hours:Minutes:Seconds
    • %Y-%m-%d %H:%M:%S - Year-Month-Day Hours:Minutes:Seconds
    • [%Y-%m-%d %H:%M:%S] - Bracketed timestamp

    Examples

    How to Use These Examples

    The examples below show common ways to use the ts command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    Basic Examples:

    Add timestamp to each line of output
    command | ts
    Specify a custom timestamp format (strftime format)
    command | ts '%Y-%m-%d %H:%M:%S'
    Add microsecond precision timestamps
    command | ts -m

    Advanced Examples:

    Add timestamp to ping results
    ping -c 5 google.com | ts
    Log command output with timestamps to a file
    command | ts > timestamped_log.txt
    Include millisecond precision with custom format
    command | ts -m '%H:%M:%S'
    Combine with other commands in pipeline
    tail -f /var/log/syslog | grep ERROR | ts '[%Y-%m-%d %H:%M:%S]'
    Add timestamps to script output
    ./script.sh | ts
    Log system monitoring with timestamps
    vmstat 1 | ts
    Add timestamps to interactive input
    ts > timestamped_input.txt
    # (now type lines and each will get timestamped)
    Monitor log file with timestamps
    tail -f logfile.txt | ts
    Add timestamps to multiple command outputs
    (command1; command2; command3) | ts
    Use with tee to view and log timestamped output
    command | ts | tee timestamped_output.log

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `ts` command is a simple yet powerful utility that adds a timestamp to each line of its input. It's part of the moreutils package and is particularly useful for logging and debugging activities, allowing you to see precisely when each line of output was produced. By default, `ts` adds timestamps in the format "MMM DD HH:MM:SS" (e.g., "Jul 09 15:42:11"), but this can be customized using format strings based on the strftime function. This flexibility allows you to adapt the timestamp format to your specific needs, whether you need millisecond precision for performance analysis or a specific date format for log compatibility. The command is particularly valuable when monitoring system activities, debugging time-sensitive issues, or analyzing the performance of scripts and commands. By piping the output of any command through `ts`, you gain valuable temporal context for each line of output. Some common use cases for `ts` include: 1. **Log Enhancement**: Adding timestamps to log files that don't already include them. 2. **Performance Analysis**: Measuring the time between different stages of a command or script's execution. 3. **Real-time Monitoring**: Adding timestamps to the output of commands like `tail -f` to see when events occur. 4. **Debugging**: Correlating program output with specific times to identify issues. The `-m` option is particularly useful for performance analysis, as it adds millisecond precision to the timestamps. This can help identify subtle timing issues or bottlenecks in scripts or commands. The `-i` (incremental) option changes the behavior to show only the time difference between consecutive lines, which is helpful for measuring intervals between events. Unlike some timestamping methods, `ts` works in real-time, adding timestamps as lines are received, rather than processing the entire input at once. This makes it suitable for monitoring ongoing processes or interactive use. It's worth noting that `ts` is not typically included in the base installation of many Linux distributions and might need to be installed separately as part of the moreutils package.

    Related Commands

    These commands are frequently used alongside ts or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the ts command works in different scenarios.

    $ ts
    View All Commands