Skip to content

tac Linux Command Guide

The tac command in Linux is used to display a file’s contents in reverse order, line by line. It is the reverse of the cat command, displaying the last line first and the first line last. By default, tac reads the file line by line from the end to the beginning. It is commonly used with pipes to reverse the output of other commands or to display logs and files in a more convenient format. The tac command also provides options to modify its behavior, such as ignoring line terminators or using a specified delimiter.

tac Syntax:

Terminal window
tac [option] [file]

Options:

OptionDescription
-bAttach a string to each line
-rReverses the order of characters
-sSpecifies a separator

Parameters:

ParameterDescription
fileThe file to be displayed

tac Command Samples:

Reverse lines in a file

Terminal window
tac file.txt

Prints the lines of a file in reverse order.

Concatenate and reverse multiple files

Terminal window
tac file1.txt file2.txt

Concatenates multiple files and prints the content in reverse order.

Reverse the output of a command

Terminal window
ls -l | tac

Displays the output of the “ls -l” command in reverse order.

Reverse a file and save the output to a new file

Terminal window
tac file.txt > reversed_file.txt

Reverses the lines in a file and saves the output to a new file.

Reverse a file and use a separator between lines

Terminal window
tac -s , file.txt

Prints the lines of a file in reverse order with a comma separator.

Reverse a file and display line numbers

Terminal window
tac -n file.txt

Displays the lines of a file in reverse order with line numbers.

Reverse a file and display specific line range

Terminal window
tac -r 1:10 file.txt

Prints lines 1 to 10 of a file in reverse order.

tac FAQ:

How do I use tac in Linux?

To use the tac command in Linux, execute the following command:

Terminal window
tac file.txt

Can I specify multiple files with tac in Linux?

Yes, you can specify multiple files with the tac command in Linux. Simply provide the file names as arguments.

Terminal window
tac file1.txt file2.txt

How can I concatenate and reverse the content of files using tac in Linux?

To concatenate and reverse the content of multiple files using tac in Linux, you can use it with the cat command as shown below:

Terminal window
cat file1.txt file2.txt | tac

Can I use tac to display specific lines from a file in reverse order in Linux?

Yes, you can use the tac command along with options like sed in Linux to display specific lines from a file in reverse order.

Terminal window
sed -n '5,10p' file.txt | tac

How can I reverse the content of a file without displaying line numbers in Linux using tac?

To reverse the content of a file without displaying line numbers using tac in Linux, you can use the following command:

Terminal window
tac -s '' file.txt

Is it possible to display the last “n” lines of a file in reverse order with tac in Linux?

Yes, you can display the last “n” lines of a file in reverse order using tac in Linux with the head command to specify the number of lines.

Terminal window
tail -n 10 file.txt | tac

Applications of the tac command

  • Reverse the lines of a file
  • Print the contents of a file in reverse order
  • Concatenate and print files in reverse
  • Useful for reading log files in chronological order