Tac is a command-line utility tool that is used for displaying and concatenating files with lines in reverse order. It is available on Unix-like operating systems, such as Linux and macOS, and is included in the GNU coreutils package.
The tac command is the reverse of cat, which concatenates files and displays their contents in the order they appear. tac instead reads each file in reverse order and displays the content of the file with the lines reversed. This can be useful in many situations, such as when you want to view the last few lines of a log file or when you want to reverse the order of a file.
The tac command can be used with multiple files as well. If more than one file is given, tac concatenates the files in reverse order and displays the result. For example, the command tac file1.txt file2.txt will display the contents of file2.txt followed by file1.txt, with the lines in each file reversed.
Here is an example of how to use the tac command:
$ cat file.txt line 1 line 2 line 3 line 4
$ tac file.txt line 4 line 3 line 2 line 1
As shown in the example above, tac reads the lines of the file in reverse order and displays them in that order.
If you encounter the below error while running the command tac:
tac: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
Debian | apt-get install coreutils |
Ubuntu | apt-get install coreutils |
Alpine | apk add coreutils |
Arch Linux | pacman -S coreutils |
Kali Linux | apt-get install coreutils |
CentOS | yum install coreutils |
Fedora | dnf install coreutils |
OS X | brew install coreutils |
Raspbian | apt-get install coreutils |
tac Command Examples
1. Concatenate specific files in reversed order:
# tac path/to/file1 path/to/file2 ...
2. Display `stdin` in reversed order:
# cat path/to/file | tac
3. Use a specific [s]eparator:
# tac -s separator path/to/file1 path/to/file2 ...
4. Use a specific [r]egex as a [s]eparator:
# tac -r -s separator path/to/file1 path/to/file2 ...
5. Use a separator [b]efore each file:
# tac -b path/to/file1 path/to/file2 ...
Summary
In summary, tac is a useful command-line tool for displaying the contents of files with lines in reverse order. It can be used with multiple files and is a convenient way to quickly view the last few lines of a file or reverse the order of a file. More information on tac and its usage can be found in the GNU coreutils documentation.