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 Tutorial / How the Shell Runs Commands: ;, &&, ||, Aliases, and Environment Variables

Linux Tutorial / How the Shell Runs Commands: ;, &&, ||, Aliases, and Environment Variables

Learn how Bash resolves commands and uses exit status with ;, &&, and ||, then distinguish aliases, shell variables, and exported environment variables.

Linux dd Command: Copy and Convert Data by Blocks

Linux dd Command: Copy and Convert Data by Blocks

Learn how to copy and convert files or block devices with Linux dd, choose block sizes and ranges, display progress, verify results, and avoid overwriting the wrong device.

Linux basename Command: Extract the Final Component of a Path

Linux basename Command: Extract the Final Component of a Path

Learn how to use the Linux basename command to remove directory prefixes and optional suffixes from path strings, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux diff Command: Compare Text Files and Directories

Linux diff Command: Compare Text Files and Directories

Learn how Linux diff compares text files and directories, produces unified patches, ignores selected whitespace changes, and reports differences through exit status.

Linux tac Command: Print Lines in Reverse Order

Linux tac Command: Print Lines in Reverse Order

Learn how Linux tac prints records from last to first, handles multiple files and custom separators, and helps inspect recent log entries in reverse order.

Linux help Command: Read Help for Bash Built-in Commands

Linux help Command: Read Help for Bash Built-in Commands

Learn how to use the Linux help command to read usage information for Bash built-in commands, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux zip Command: Create ZIP Archives

Linux zip Command: Create ZIP Archives

Learn how to create ZIP Archives with the Linux zip command, including practical examples, key options, and important precautions.

Linux sed Command: Search and Replace Text Streams

Linux sed Command: Search and Replace Text Streams

Learn how to search and Replace Text Streams with the Linux sed command, including practical examples, key options, and important precautions.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux gzip Command: Compress a File in gzip Format

Linux gzip Command: Compress a File in gzip Format

Learn how to compress a File in gzip Format with the Linux gzip command, including practical examples, key options, and important precautions.