The tac
command in Bash is used to concatenate and display files in reverse order, specifically reversing the order of lines. It is essentially the reverse of the cat
command, which displays file contents in the standard order.
The basic syntax of the tac
command is as follows:
tac [options] [arguments]
-b
, --before
: Place a delimiter before the line instead of after it.-r
, --regex
: Treat the delimiter as a regular expression.-s
, --separator
: Specify a custom separator instead of the default newline.To reverse the contents of a file named example.txt
:
tac example.txt
To reverse the contents of multiple files, file1.txt
and file2.txt
, and display them one after the other:
tac file1.txt file2.txt
To reverse the contents of a file and use a custom separator, such as a comma:
tac -s ',' example.txt
To reverse lines and place a delimiter before each line:
tac -b example.txt
tac
in combination with other commands like grep
or sort
to manipulate data more effectively.Redirect the output of tac
to a new file if you want to save the reversed content:
tac example.txt > reversed_example.txt
tac
reads the entire file into memory, so it may not be suitable for very large files.