opscape

Viewing files

tac

Print files with their lines in reverse order, last line first.

tac [OPTION...] [FILE...]

6 of 6 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: tac(1)

tac is cat spelled backwards and does what the name suggests: it prints the last line first and the first line last. The characters inside each line stay in order; to reverse those, use rev.

The everyday use is logs and other append-only files, where the newest entry is at the bottom and you want it at the top. tac reads a regular file from the end, so tac big.log | head is fast even on a large file. It is part of GNU coreutils and is not installed on macOS by default.

Examples

Reverse the order of lines

tac todo.txt
explain
output
deploy v2.4
fix logout bug
write tests
fix login bug
buy milk

deploy v2.4, the last line of the file, comes out first and buy milk last.

Show the newest log entries first

tac logs/app.log | head -n 3
explain
output
2026-03-14 08:30:05 INFO  cleanup job removed 42 temp files
2026-03-14 08:30:00 ERROR disk usage on /var at 91%
2026-03-14 08:25:00 INFO  user id=2 updated by api

The log is appended to in time order, so reversing it puts the most recent entries on top. head -n 3 keeps the three newest.

Find the most recent matching line

tac logs/app.log | grep -m 1 WARN
explain
output
2026-03-14 08:20:12 WARN  blocked request for /.env from 198.51.100.23

grep -m 1 stops at the first match, which after tac is the last WARN in the file: the blocked /.env request at 08:20:12.

Reverse several files

tac logs/app.log.1 logs/error.log
explain
output
2026-03-13 23:59:59 INFO  log rotated
2026-03-13 23:59:40 INFO  nightly backup finished: 1.2G in 88s
2026-03-13 23:58:12 INFO  nightly backup started
[2026-03-14 08:30:00] disk usage on /var at 91%
[2026-03-14 08:16:40] database timeout after 5000ms (host=db01)

Each file is reversed on its own and the results are printed in the order the files were given; the files themselves are not swapped.

Reverse records split by another separator

printf 'one,two,three,' | tac -s ,
echo
output
three,two,one,

-s , makes the comma the record separator instead of the newline. Each record keeps its trailing comma, so the output reads three,two,one,. The echo just ends the line.

Attach the separator to the front of each record

printf '/usr/local/bin' | tac -b -s /
echo
output
/bin/local/usr

With -b the separator belongs to the start of the record that follows it, so the path components /usr, /local and /bin come out in reverse order as /bin/local/usr.

Common pitfalls

  • A last line without a trailing newline gets glued to the line tac prints after it. printf 'a\nb' | tac prints ba followed by a newline. Make sure files end with a newline, or add one first.

  • tac reverses lines, not characters; rev reverses characters within each line. They are often confused.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • macOS has no tac. tail -r (a BSD option, not in GNU tail) reverses a file there, or install GNU coreutils, where it may be called gtac.

Options worth knowing

A selection, described in our own words. For the full list, see tac(1) or tac --help.

OptionWhat it does
-s, --separator STRINGUse STRING instead of newline as the record separator.
-b, --beforeTreat the separator as the start of each record instead of its end.
-r, --regexInterpret the separator as a regular expression.

Related