Linux Commands

Bash Tac Command

On Linux, you are probably used to the cat command, which, followed by a file, concatenates the file’s contents to the standard output. With the cat command, the concatenated output gets arranged exactly as it appears on the file.

However, if you need to reverse the arrangement of the file’s content, that’s when you use the tac command. The tac command concatenates a file, printing its contents in reverse. This feature of tac makes it popular when working with log files; if you need to monitor the log files starting with the latest activities, you must concatenate the log file in reverse using the cat command. Let’s get into the details of how to use the tac command.

tac Syntax

$ tac OPTIONS FILE-NAME

Like the cat command, you can use tac followed by the file you want to concatenate its content in reverse. In the example below, we have the same file with contents displayed using the tac and cat command to highlight the difference between the two commands.

Note how in the first output using cat, we have the linuxhint printed as the first line, but when we use tac, it comes last due to reversing the output.

tac Options

There are three main options that you can use with the tac command:

  • -b: It is used when you want the separator to come before and not after.
  • -r: It is used when the separator added should be interpreted as a regex.
  • -s: It is used when you need to use a string in the file as a separator.

Let’s have some examples to see how tac works.

Separating Files When Using tac

With tac, you can concatenate the output of more than one file, but if you don’t add a separator, the output will mix up. For instance, in the example below, we’ve used tac to concatenate two files.

Notice how there is no separator to distinguish the output of each file. To add a separator before the next string or file, use the -b option.

If we needed to use the STRING as the separator, we could use the -s flag.

Note that with the -s flag, you don’t get a new line as the separator. Instead, the STRING separator makes the second file’s output printed out.

You can use the -r instead of the -s to treat the separator string as a regular expression. Let’s see how using -r differs from using -s.

Moreover, if you need to use a given string as the separator, you only need to specify and enclose it in quotation marks. For instance, to separate the file’s content using a string contained in the file, we could run the command as shown.

$ tac --separator=" STRING" FILE-NAME

Note how our file was not separated in the first output, but in the second output, we’ve created the separation using the –separator=”” flag.

That’s how you can work with the tac command. You can open the help page to understand how the various options blend with the command.

$ tac –help

Conclusion

The bash tac command works the same way as cat in concatenating the contents of a file, only that, in this case, it works in reverse. This guide covered the various options you can use with tac and offered examples of each option to help you feel at home with the command. Try it out, especially with files that you want to view their content, starting with those at the bottom, depending on their arrangement.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.