🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

tac Command

Beginner Text Processing man(1)

Concatenate and print files in reverse (last line first)

👁 8 views 📅 Updated: Mar 15, 2026
SYNTAX
tac [OPTION]... [FILE]...

What Does tac Do?

tac (cat reversed) displays files with lines in reverse order — the last line becomes the first and the first becomes the last. It is the line-level complement to rev (which reverses characters within a line).

tac is useful for viewing log files (most recent entries first), reversing sorted output, processing files from bottom to top, and building reverse chronological views.

tac reads the entire file into memory, so it may not be suitable for very large files. For large log files, consider tail for viewing recent entries.

Options & Flags

OptionDescriptionExample
-b Attach separator before instead of after tac -b file.txt
-r Use regex as separator tac -r -s "^Chapter" book.txt
-s Use STRING as separator instead of newline tac -s '---' file.txt

Practical Examples

#1 Reverse file lines

Displays the file with lines in reverse order.
$ tac file.txt

#2 View recent log entries first

Shows the 20 most recent log lines first.
$ tac /var/log/syslog | head -20

#3 Reverse sort output

Reverses sorted output (equivalent to sort -r but works for any input).
$ sort file.txt | tac

#4 Reverse numbered list

Counts from 10 down to 1.
$ seq 1 10 | tac
Output: 10 9 8 7 6 5 4 3 2 1

#5 Process file bottom-up

Shows the last 5 lines of a file (like tail but in reversed order).
$ tac script.sh | head -5

#6 Reverse paragraphs

Reverses the order of paragraphs (separated by empty lines).
$ tac -s '' document.txt

Tips & Best Practices

tac vs tail: tail -n 20 shows last 20 lines in order. tac file | head -20 shows last 20 lines in reverse. Use tac for reverse chronological views.
tac vs rev: tac reverses line ORDER (last line first). rev reverses characters WITHIN each line. They are different operations.
Memory usage: tac reads the entire file into memory. For very large files (multi-GB), use tail or sed for viewing the end.

Frequently Asked Questions

How do I reverse the lines of a file?
Use tac filename. The last line becomes the first and vice versa.
What is the difference between tac and rev?
tac reverses line order (line 5, line 4, line 3...). rev reverses characters within each line (hello → olleh).
How do I view a log file in reverse?
tac /var/log/syslog | head -50 shows the 50 most recent entries with the newest first.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →