Skip to content
Latchkey

tac: Reverse Line Order

tac concatenates and prints files with their lines in reverse, last line first.

tac is cat backwards: it reverses line order. Handy for showing newest-last logs newest-first.

What it does

tac reads its input and writes the lines in reverse order, so the last line comes out first. With -s it reverses on a custom separator instead of newline, and -r treats the separator as a regular expression. Unlike tail it reverses the whole input, not just the end.

Common usage

Terminal
tac app.log                      # newest entries first
tac changes.txt | head -n 5      # last 5 lines, newest first
printf '1\n2\n3\n' | tac        # 3 2 1

Options

FlagWhat it does
(default)Reverse line order, last line first
-s <sep>Use <sep> as the record separator
-rTreat the separator as a regular expression
-bAttach the separator before instead of after each record

In CI

tac is useful for presenting append-only logs newest-first or for reversing a generated list before processing. Combine tac | head to take items from the end while keeping a head-based pipeline.

Common errors in CI

tac is GNU coreutils and is not installed on BSD/macOS by default, so scripts fail there with "command not found"; install coreutils (gtac) or use tail -r on macOS, which reverses lines similarly. A final line without a trailing newline is still handled, but mixed line endings (CRLF) can leave stray carriage returns after reversal.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →