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.
tac [OPTION]... [FILE]...
Try it
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.
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
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 istail -r. Portable scripts can usesed '1!G;h;$!d'orawk '{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,
tacprints it without one, and then the next record follows directly. Useprintf '...\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 filethen reads from the end without loading everything. - Not a sort.
tacreverses; it does not order. To sort in descending order, usesort -r. - Reverse characters within a line with
rev, nottac.tac | revreverses both. - Newest-first logs:
tac /var/log/app.log | head -20shows the latest 20 entries newest first;tail -n 20gives the same lines oldest first. -band-rare for non-line records such as paragraph markers or tagged blocks. With-r,tacmatches 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.
Related
cat,head,tail,sort- Lessons: Viewing text, Text pipelines