Linux tac Command: Print Lines in Reverse Order
The tac command prints records from the last record to the first. Because a newline is the default record separator, it is useful for reviewing a text log from its newest line backward or reversing the current line order of a file.
This guide explains the basic syntax, key options, practical examples, related commands, common cautions, and frequently asked questions for tac.
What Is the tac Command?
Its name is cat written backward. It reverses records rather than individual bytes or characters. When several files are provided, it processes the files in operand order while reversing the records within each file.
Basic Syntax
tac [OPTION]... [FILE]...
Options and edge cases can vary by implementation and version. Check tac --help and man tac on the target system before relying on implementation-specific behavior.
Basic tac Usage
Reverse the lines of a file
The final line becomes the first line of output.
tac events.log
Process multiple files
Each input is reversed independently while the file operand order remains unchanged.
tac january.log february.log
Reverse standard input
A pipeline can provide the input, but tac must reach the end before it can emit the first reversed record.
printf 'one\ntwo\nthree\n' | tac
Result of reversing the line order
tac does not reverse characters, but outputs the lines in reverse order. The order of characters within each line remains the same.
printf 'first\nsecond\nthird\n' | tac
Example output:
third second first
It is useful when reading logs that accumulate in chronological order from the most recent line, but it is not the same as sorting based on timestamps.
Key Options
| Option | Purpose |
|---|---|
-s STRING |
Use STRING instead of newline as the record separator. |
-b |
Attach the separator before the record instead of after it. |
-r |
Interpret the separator as a regular expression. |
--help |
Show options supported by the installed implementation. |
Practical Examples
Browse a log from newest to oldest
Combine tac with a pager so the reversed output remains searchable.
tac application.log | less
Use a custom record separator
GNU tac can reverse records delimited by a string other than newline.
tac -s '---' records.txt
Find the most recent matching line
Reverse the log and stop grep after its first match.
tac access.log | grep -m 1 'ERROR'
tac and Related Commands
| Command | Primary role | Best fit |
|---|---|---|
tac |
Reverse existing record order | Reading a file from the bottom |
cat |
Preserve original order | Displaying and concatenating files |
tail |
Select the final portion | Showing only recent lines |
sort -r |
Sort values in descending order | Ordering by content |
Important Cautions
It is not a sorting command
The command does not interpret timestamps or values; it only reverses the current record sequence.
Unbounded streams cannot be reversed promptly
The end of input must be known, so tac is not suitable for a stream that never ends.
Check separator portability
Custom separator and regular-expression behavior described here follows GNU tac and may differ elsewhere.
FAQ
Are tac and sort -r equivalent?
No. tac reverses the current order, while sort -r compares values and sorts them.
How do I reverse only the last ten lines?
Use tail -n 10 FILE | tac.
Can tac follow a live log?
Not usefully, because it needs the end of input. Use tail -f for live monitoring.
Does tac merge all input files before reversing?
Normally it processes each file in operand order and reverses records within that file.









