
tac Command in Linux
The tac command in Linux concatenates and prints files in reverse. It displays the contents of a file line-by-line in reverse order, from the last line to the first. It is also called the reverse version of the cat command.
Table of Contents
Here is a comprehensive guide to the options available with the tac command −
Syntax of tac Command
The syntax of the tac command in Linux is as follows −
tac [optionsâ¦] [fileâ¦]
The [options] field in the above syntax is used to specify one or more options to modify the command's output. The [file] field is used to specify one or more files to process.
tac Command Options
The options of the tac command are listed below −
Short Option | Long Option | Description |
---|---|---|
-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. |
--help | Display help and exit. | |
--version | Output version information and exit. |
Examples of tac Command in Linux
This section discusses how to use the tac command in Linux with examples −
- Reversing the File Line Order
- Reversing Standard Input
- Using Custom Separator
- Using a Separator before Each Section
- Displaying Usage Help
Reversing the File Line Order
To reverse the file line order, use the tac command with the file name −
tac file.txt
The above command displays the contents of file.txt with lines in reverse order.

Reversing Standard Input
To reverse the standard output, echo lines and pipe them to the tac command −
echo -e 'Welcome\nto\nTutorialsPoint' | tac

Using Custom Separator
To use a custom separator for lines instead of newline, use the -s or --separator option with the separator string −
tac -s "-" file.txt
The above command splits the input at each -, turning Hello-TutorialsPoint into: Welcome, TutorialsPoint. It then reverses the segments: TutorialsPoint, Hello. The - is kept after each segment (except the last), so the output becomes −

Using a Separator before Each Section
By default, the separator is placed after the line. To put it before the line, use the -b or --before option with the tac command −
tac -b -s "-" file.txt
The output of the above command is shown in the image below −

Displaying Usage Help
To display the usage help, use the --help option −
tac --help
Conclusion
The tac command in Linux is used to display the contents of a file in reverse line order, starting from the last line to the first. It works like a reversed version of the cat command. Various options can customize its behavior, such as using a custom separator, placing the separator before lines, or using regular expressions for splitting.
In this tutorial, we covered the tac command, its syntax, options, and usage in Linux with examples.