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.

Related Resources

More in This Category
Linux systemctl Command: Manage systemd Services

Linux systemctl Command: Manage systemd Services

Learn how to manage systemd Services with the Linux systemctl command, including practical examples, key options, and important precautions.

Linux id Command: Show User and Group IDs

Linux id Command: Show User and Group IDs

Learn how to show User and Group IDs with the Linux id command, including practical examples, key options, and important precautions.

Linux ls Command: List Files and Directories

Linux ls Command: List Files and Directories

Learn how to use the Linux ls command to list files and directories with permissions, sizes, timestamps, and sorting options, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux more Command: View Text One Screen at a Time

Linux more Command: View Text One Screen at a Time

Learn how to use Linux more to read text one screen at a time, move and search interactively, start at a line or pattern, and choose between more and less.

Linux time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Learn the difference between a terminal and a shell, read command syntax and output, and use Bash help tools through a short hands-on exercise.

Linux sudo Command: Run Commands with Elevated or Another User's Privileges

Linux sudo Command: Run Commands with Elevated or Another User's Privileges

Learn how to use the Linux sudo command to run approved commands with elevated privileges or as another user, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux dirname Command: Extract the Directory Part of a Path

Linux dirname Command: Extract the Directory Part of a Path

Learn how to use the Linux dirname command to remove the final component of a path and return its directory portion, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux mkfs Command: Create a New Filesystem

Linux mkfs Command: Create a New Filesystem

Learn how to create a New Filesystem with the Linux mkfs command, including practical examples, key options, and important precautions.

Linux df Command: Check Filesystem Free Space

Linux df Command: Check Filesystem Free Space

Learn how to check Filesystem Free Space with the Linux df command, including practical examples, key options, and important precautions.