Viewing files
tac
Print files with their lines in reverse order, last line first.
tac [OPTION...] [FILE...]
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
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
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
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
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
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
/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
tacprints after it.printf 'a\nb' | tacprintsbafollowed by a newline. Make sure files end with a newline, or add one first.tacreverses lines, not characters;revreverses 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 GNUtail) reverses a file there, or install GNU coreutils, where it may be calledgtac.
Options worth knowing
A selection, described in our own words. For the full list, see tac(1) or tac --help.
| Option | What it does |
|---|---|
-s, --separator STRING | Use STRING instead of newline as the record separator. |
-b, --before | Treat the separator as the start of each record instead of its end. |
-r, --regex | Interpret the separator as a regular expression. |
Related
catConcatenate files and print them to standard output; also the quickest way to see a short file.revReverse the characters of every line.tailPrint the last lines of files, or follow a file as it grows with -f.sortSort lines of text alphabetically, numerically, by version or by chosen fields; also merge and de-duplicate.