Updated April 18, 2023
Introduction to Linux tac
In the Linux operating system, the tac command is used to concatenate and print the files in reverse format. When the input or specified file is not provided to the command then the tac command will read the standard input. The tac command is exactly opposite to the “cat” command. The tac command was written by Jay Lepreau and David MacKenzie.
Syntax:
tac [ OPTION ] ... [ FILE ] ...
- tac: Using the tac keyword in the command | syntax. It will take the two different sets of arguments such as an option and file name then revert the output result.
- OPTION: We can provide the different flags as options that are compatible with tac command.
- FILE: we need to provide the input file to tac command.
How does Linux Tac Command Work?
In the Linux operating system, there are different ways to read files like cat, tail, head, more, less, etc. The cat command is useful to read the file as it i.e. the command is reading the file from start to end. But tac command is inversely proportional or opposite to the cat command. It will print the output in reverse format.
The tac command will take the two different sets of arguments as an input to the command. As per the give input option, the tac command will revert the input file result and display it. The tac is a complete reverse version of the cat command and also spelled the backward. It will print every line of file from start to end of the file but in reverse format.
Examples to Implement Linux tac Command
Following are the examples are given below:
Example #1 – Tac Command
In Linux, the tac command will print the input file in reverse format. It is a very basic Linux tac command to get the command output.
Command:
tac data_1.txt
Explanation: We are having a sample directory, in the same directory we have “data_1.txt” file. There are few words of data present in it (refer Screenshot 1 (a)). Now we need to use the “tac” command and read the same file “data_1.txt”. While reading the data via “tac” command the compete output will be in the reverse format (refer Screenshot 1 (b)).
Output:
Example #2 – Tac Command with “-b” option
In tac command, we are having the facility to add the separator while reading the multiple files. We need to use the “-b” option in the Tac command.
Command:
tac -b data_1.txt data_2.txt
Explanation: In the sample directory, there are two different files i.e. “data_1.txt” and “data_2.txt”. Both the files are having few data in it (refer Screenshot 2 (a)). Now we need to read both the files at the same time. If we will read the files normal then the files come together and we are not able to find the starting and ending point of the file. To overcome this condition, we are using the “-b” option in tac command. It will separate the files while displaying the multiple files (refer Screenshot 2 (b)).
Output:
Example #3 – Tac Command with “-r” option
In tac command, the command option will interpret the separator as a give regular expression. We need to use the “-r” option in the tac command.
Command:
tac -r data_2.txt data_1.txt
Explanation: In the Linux ecosystem via tac command, we can read the multiple files in the same time and get the output in reverse format. We are having the functionality to get the output as compare to a separator (as a regular expression). To get the same output, we need to use the “-r” option in the command option will interpret the separator as a give regular expression.
Output:
Example #4 – Tac Command with “-s” option
The tac command uses the string to separate out the output. It will not use the new line to separate out the end result. We need to use the “-s” option in the tac command.
Command:
tac -s data_1.txt data_2.txt
Explanation: As per the above tac command, we are using the two files as an input to the command. The command is to use the string as a separator. It will not consider the new line to separate the output result. Hence we are getting the tac command is printing the second file output (data_2.txt) and skip the first line output of the file (data_1.txt). To get a similar tac command output, we need to use the “-s” option in the tac command.
Output:
Example #5 – Tac “–help” command
In tac command, if we will need any help to get the reference in tac command, check the syntax of the command, check the different variable option available in the tac command. We need to use the “–help” option in the tac command.
Command:
tac --help
Explanation: As per the above option, we are getting instant help on the tac command. We are able to get detail information on tac command including the command usage, different options available in the command, etc. To get such information we need to use the “–help” option in the tac command.
Output:
Example #6- Tac “–version” command
We are able to track the tac version of the current operating system. We need to use the “–version” option in the tac command.
Command:
tac --version
Explanation: As per the Linux operating version, the tac version will also vary. As per the current operating system, we are having 8.22 version of the tac command. The version may vary from the different operating systems. The tac command feature will also vary from version to version.
Output:
Conclusion
We have seen the uncut concept of “Linux Tac Command” with the proper example, explanation, and command with different outputs. The tac command is used to get the end result in reverse format. The tac command is mainly useful in data manipulation language.
Recommended Articles
We hope that this EDUCBA information on “Linux tac” was beneficial to you. You can view EDUCBA’s recommended articles for more information.