tac Command in Linux

This tutorial explains Linux “tac” command, options and its usage with examples.

tac – Concatenate and print files in reverse.

DESCRIPTION

tac (which is “cat” backwards) concatenates each FILE to standard output just 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 in which 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.

SYNOPSIS

advertisement
advertisement

tac [OPTION]… [FILE]…

OPTIONS :

-b, –before
attach the separator before instead of after
-r, –regex
interpret the separator as a regular expression
-s, –separator=STRING
use STRING as the separator instead of newline

EXAMPLES

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

$ tac file1.txt

The word tac is reverse of the word cat. The tac command functionality is also reverse of the cat command. cat command prints the file. tac command prints the file in reverse order with the last line first as shown below.

advertisement
$ cat file1.txt
01. Advanced C Programming Lab
02. Linux & C Debugging Techniques Lab
03. SAN Technology Training
04. SAN Administration Training
05. Linux Device Drivers
06. Linux Administration Training
07. Linux Systems Programming I
08. Linux Systems Programming II – IPCs
09. Linux Network Programming Lab
10. Linux Multi-threaded Programming Lab
11. Linux Kernel Internals Programming
12. Linux Kernel Debugging
13. Linux Virtualization Internals Programming
 
$ tac file1.txt
13. Linux Virtualization Internals Programming
12. Linux Kernel Debugging
11. Linux Kernel Internals Programming
10. Linux Multi-threaded Programming Lab
09. Linux Network Programming Lab
08. Linux Systems Programming II – IPCs
07. Linux Systems Programming I
06. Linux Administration Training
05. Linux Device Drivers
04. SAN Administration Training
03. SAN Technology Training
02. Linux & C Debugging Techniques Lab
01. Advanced C Programming Lab

2. Using the other options

$ cat records 
---1---
1
2
3
---2
A
B
C
---3--
a
b
c
$ tac --before --regex --separator=^---[0-9]+-*$ records
---3--
a
b
c
---2
A
B
C
---1---
1
2
3

In this example the file records contains multiline records, each started with a line (^…$) that starts with —, followed by a number ([0-9]+) and an optional sequence of minus signs (-*). One can see the order of lines in each record and its header line are preserved.

advertisement

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.