How to Tail a File and Add Timestamps in Real Time on Linux and macOS

When monitoring or debugging log files, it’s often helpful to tail a file and add timestamps to each new line as it appears. This guide…

How to Tail a File and Add Timestamps in Real Time on Linux and macOS
Photo by Markus Spiske on Unsplash

When monitoring or debugging log files, it’s often helpful to tail a file and add timestamps to each new line as it appears. This guide provides a solution for both Linux and macOS (using gdate) and includes an example of generating random data for demonstration.

1. Generating Example Data for Testing

To test tailing a file in real time, we can generate random lines of alphanumeric data and append them to a file (random_data.txt) every 5 seconds.

Run the following command in a terminal:

while true; do cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 16 >> random_data.txt; echo >> random_data.txt; sleep 5; done

How It Works:

  1. cat /dev/urandom: Reads random binary data.
  2. LC_ALL=C tr -dc 'a-zA-Z0-9': Filters the data to include only alphanumeric characters.
  3. head -c 16: Limits the output to 16 characters.
  4. >> random_data.txt: Appends the random string to random_data.txt.
  5. echo: Adds a newline after the string.
  6. sleep 5: Waits 5 seconds before generating the next line.

2. Tail a File and Add Timestamps

Here’s how to follow the random_data.txt file while adding timestamps to each new line.

Linux Command

On Linux, use the native date command:

tail -f random_data.txt | while read line; do echo "$(date +%T.%N) $line"; done
  • %T: Prints time in HH:MM:SS format.
  • %N: Adds nanoseconds.

macOS Command (with GNU date)

On macOS, you need GNU date (gdate) to achieve nanosecond precision. First, install Coreutils:

brew install coreutils

Then run this command:

tail -f random_data.txt | while read line; do echo "$(gdate +%T.%N) $line"; done

gdate: The GNU version of date, supporting nanosecond timestamps.

3. Putting It Together

Here’s how to run everything step-by-step:

  1. Generate the Data: Open a terminal and start the random data generator:
while true; do cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 16 >> random_data.txt; echo >> random_data.txt; sleep 5; done

2. Tail the File with Timestamps: In another terminal, run:

  • For Linux:
tail -f random_data.txt | while read line; do echo "$(date +%T.%N) $line"; done
  • For MacOS:
tail -f random_data.txt | while read line; do echo "$(gdate +%T.%N) $line"; done

4. Sample Output

After running both commands, you will see output similar to this:

12:00:01.123456789 M1XFCzv4TZZjPx2k 
12:00:06.987654321 u5f6TrWE3K04mp3Y 
12:00:11.765432123 b3XDSe3bfEcsyxV6 
12:00:16.123456789 G63NHmwLzstpa8af 
12:00:21.987654321 EKUeUlfp1LEmHPoe

5. Creating a Script for Reuse

To simplify the process, create a reusable script tail_with_timestamp.sh:

#!/bin/bash 
file=$1 
 
if [[ -z "$file" ]]; then 
    echo "Usage: $0 <filename>" 
    exit 1 
fi 
 
# Use gdate if available (macOS), otherwise use date (Linux) 
date_cmd=$(command -v gdate || echo "date") 
 
tail -f "$file" | while read line; do 
    echo "$($date_cmd +%T.%N) $line" 
done

Steps to Use:

  1. Save this script as tail_with_timestamp.sh.
  2. Make it executable:
chmod +x tail_with_timestamp.sh

3. Run the script:

./tail_with_timestamp.sh random_data.txt

With this guide, you can easily generate random data and follow it in real time with timestamps on both Linux and macOS.