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…
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; doneHow It Works:
cat /dev/urandom: Reads random binary data.LC_ALL=C tr -dc 'a-zA-Z0-9': Filters the data to include only alphanumeric characters.head -c 16: Limits the output to 16 characters.>> random_data.txt: Appends the random string torandom_data.txt.echo: Adds a newline after the string.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 inHH:MM:SSformat.%N: Adds nanoseconds.
macOS Command (with GNU date)
On macOS, you need GNU date (gdate) to achieve nanosecond precision. First, install Coreutils:
brew install coreutilsThen run this command:
tail -f random_data.txt | while read line; do echo "$(gdate +%T.%N) $line"; donegdate: The GNU version of date, supporting nanosecond timestamps.
3. Putting It Together
Here’s how to run everything step-by-step:
- 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; done2. 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"; done4. 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 EKUeUlfp1LEmHPoe5. 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"
doneSteps to Use:
- Save this script as
tail_with_timestamp.sh. - Make it executable:
chmod +x tail_with_timestamp.sh3. Run the script:
./tail_with_timestamp.sh random_data.txtWith this guide, you can easily generate random data and follow it in real time with timestamps on both Linux and macOS.
