This tutorial explains Linux “tac” command, options and its usage with examples.
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.
tac [OPTION]… [FILE]…
attach the separator before instead of after
interpret the separator as a regular expression
use STRING as the separator instead of newline
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.
$ 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.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Buy Linux Books
- Practice Programming MCQs
- Apply for Programming Internship
- Apply for Linux Internship
- Buy Information Technology Books