Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux tac command

Reference for the Linux tac command: print files with their lines in reverse order, change the separator with -s, use -b and -r, and read logs newest first.

tac is cat spelled backwards, and it does what the name suggests: it prints the lines of its input in reverse order, last line first. Typical uses are reading a log with the newest entry at the top, processing a list from the end, and undoing a sort.

Unlike cat, tac has to read the whole input before it can print the first line. With a regular file it can seek to the end and work backwards efficiently; with a pipe it must buffer everything in memory or a temporary file.

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

Try it

Bash Reversing lines and files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Separators and regular expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
tac [-brs] [-s SEPARATOR] [FILE]...
Option Description
-s SEP, --separator=SEP Use SEP instead of newline as the record separator.
-b, --before Attach the separator before each record instead of after it.
-r, --regex Interpret the separator as a regular expression.

With no FILE, or -, tac reads standard input.

Returns: 0 on success; 1 if a file could not be read.

Usage notes

  • Part of GNU coreutils. BSD and macOS systems do not have tac; the equivalent there is tail -r. Portable scripts can use sed '1!G;h;$!d' or awk '{a[NR]=$0} END{for(i=NR;i>0;i--)print a[i]}'.
  • A missing final newline is handled by joining: if the last line has no newline, tac prints it without one, and then the next record follows directly. Use printf '...\n' to make sure the data ends cleanly.
  • Memory: from a pipe, the whole input is held until the end. For very large streams, write to a file first; tac file then reads from the end without loading everything.
  • Not a sort. tac reverses; it does not order. To sort in descending order, use sort -r.
  • Reverse characters within a line with rev, not tac. tac | rev reverses both.
  • Newest-first logs: tac /var/log/app.log | head -20 shows the latest 20 entries newest first; tail -n 20 gives the same lines oldest first.
  • -b and -r are for non-line records such as paragraph markers or tagged blocks. With -r, tac matches the regular expression while scanning backwards, so patterns that can match more than one character (such as [0-9]\+) may split records differently than you expect. Single-character classes like [;,] are reliable; test anything else on a small sample.
  • Several files: each file is reversed on its own. To reverse the whole combination, use cat a b | tac.