Linux tac command

Updated: 05/04/2019 by Computer Hope
tac command

On Unix-like operating systems, the tac command catenates and prints the contents of files, in reverse, line by line.

This page covers the GNU/Linux version of tac.

Description

tac (which is "cat" backwards) concatenates each FILE to standard output like the cat command, but in reverse: line by line, printing the last line first. This is useful (for instance) for examining a chronological log file where the last line of the file contains the most recent information.

If no FILE is specified, or if the FILE is specified as "-", tac reverses the contents of standard input.

Syntax

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

Options

-b, --before Attach the line separator before each line of output instead of after.
-r, --regex Interpret the line separator as a regular expression (useful with the -s option, see below).
-s,
--separator=STRING
Use STRING as the line separator instead of a newline.
--help Display command help and exit.
--version Output version information and exit.

Examples

tac file1.txt

Prints the lines of file1.txt in reverse, from last line to first.

cat — Output the contents of a file.
tail — Print the last lines of a text file.