When analysing system logs, debugging failed deployment pipelines, or parsing chronological data in Linux, the newest events are almost always appended to the very bottom of the file. While the standard cat command displays content from top to bottom, reading large logs this way requires extensive scrolling. While tail displays only the final few lines, examining multi-thousand-line logs in reverse chronological order requires a dedicated utility.
The GNU tac command—humorously named as cat spelled backwards—is a core Unix/Linux utility designed specifically to concatenate and print files in reverse order, line by line. In this guide, we explore how to use tac in the Linux terminal to read log files backwards, configure custom regex separators, and integrate it into shell pipelines.
In This Guide
How tac Differs from cat and rev
To avoid confusion when manipulating text in Linux, it is important to distinguish between three core utilities that handle reversal or concatenation:
- cat: Reads files forward, printing from line 1 to line N, maintaining normal character and line ordering.
- rev: Reverses the order of characters within each individual line, but prints the lines themselves from top to bottom.
- tac: Reverses the order of entire lines, printing line N first down to line 1, while keeping the character sequence within each individual line completely intact.
Basic Syntax and Simple Inversion
The fundamental syntax for tac is straightforward:
tac [options] [file...]
To view any file in reverse line order, run tac followed by the file path:
tac deployment_steps.txt
If the original file contained:
Step 1: Build containers
Step 2: Run unit tests
Step 3: Deploy to production
The tac output will render as:
Step 3: Deploy to production
Step 2: Run unit tests
Step 1: Build containers
You can also pass multiple files at once. tac processes each file individually, outputting the inverted lines of the first file, followed by the inverted lines of the second:
tac part1.log part2.log > combined_reversed.log
Reading Chronological Log Files in Reverse
In production troubleshooting, errors frequently cause subsequent cascading failures. By reversing log output with tac, you can inspect the most recent crash incidents first without waiting for a massive file to scroll through your terminal pager:
tac /var/log/syslog | less
Inside less, you can navigate top-down to see timestamps starting from the present second and moving backward through history.
Using Custom Record Separators (-s Option)
By default, tac uses the standard newline character (\n) as its record delimiter. However, structured text formats—such as database dumps, multi-line XML blocks, or custom server logs—often group multi-line entries separated by specific delimiters (such as semicolons, blank rows, or divider strings).
Use the -s (or --separator) option to define an alternative record delimiter:
To reverse records separated by a semicolon:
tac -s ';' database_dump.sql
To reverse multi-line paragraphs separated by a double newline:
tac -s $'\n\n' essay.txt
Applying Regular Expression Separators (-r Option)
When record boundaries vary—such as application logs where each entry begins with a dynamic ISO timestamp (e.g. [2026-11-03])—a static string separator is insufficient. Combine -r (or --regex) with -s to interpret the separator as a regular expression:
tac -r -s '^\[[0-9]{4}-[0-9]{2}-[0-9]{2}\]' application.log
In this example, tac treats each multi-line log block beginning with a date stamp as a single record, outputting the entire log entry intact while inverting the order of the entries.
Controlling Separator Attachment (-b Option)
By default, tac attaches the separator to the end of the record it separates. When dealing with logs or scripts where delimiters mark the beginning of a block (such as leading headers or markdown section dividers), use the -b (or --before) flag:
tac -b -s "=== START RECORD ===" export.txt
This ensures the header tag remains anchored to the top of its corresponding record after inversion.
Combining tac with grep, head, and awk
Because tac reads from standard input when no file is specified, it serves as an indispensable pipeline filter:
- Find the Last Occurrence of an Error Fast:
Becausetac /var/log/nginx/error.log | grep -m 1 "502 Bad Gateway"tacfeeds lines in reverse,grep -m 1terminates immediately upon discovering the most recent matching error, avoiding a full file scan. - Extract the Last 20 Lines in Chronological Order:
Piping totac logfile.txt | head -n 20 | tacheadgrabs the newest 20 lines, and runningtaca second time restores them to standard chronological order. - Process Reverse Command History:
Instantly browse your terminal history starting from your most recent command.history | tac | less