tac and rev - Reversing Lines and Characters
What You'll Learn
- How
tacreverses the order of lines (last line first) - How
revreverses the characters in each line (abc → cba) - A clear answer to "which one do I use?" —
tacvsrev - Practical tricks: reading logs newest-first and combining both
Quick Summary (the core difference)
- Flip the order of lines →
tac("cat" spelled backwards) - Flip the characters inside a line →
rev(short for reverse) - Both the name and the target are reversed. When confused: tac = lines / rev = characters
Prerequisites (environment)
- OS: Ubuntu / any common Linux distro
tacships with GNU coreutils,revships with util-linux. Both are installed by default
1. What Is tac and What Can It Do?
Conclusion:
tacprints a file's lines from last to first, line by line. Its name iscatspelled backwards.
cat prints a file from top to bottom. What if I want it the other way — from the bottom up?tac. It's cat spelled backwards, and the job matches the name: "print lines in reverse order."cat first.$ cat fruits.txt apple banana cherry
$ tac fruits.txt
cherry banana apple
tac only moves the order of the lines. It never touches the characters inside a word like apple.The separator is a newline by default, but -s changes it. tac -s ',' file reverses comma-separated fields instead of lines.
2. Why Is Reversing Lines Useful?
Conclusion: Logs append old lines at the top and new lines at the bottom, so
tacreorders them to read the newest events first.
tac shines. Reverse it and the newest line comes first. Pair it with head and you get "the last 5 events, newest-first" instantly.$ tac access.log | head -n 5
192.168.0.9 - GET /login 200 192.168.0.4 - GET /api 500 192.168.0.4 - GET /api 500 192.168.0.7 - GET / 200 192.168.0.2 - GET /about 200
tail shows the last lines in their original order. If you want the tail reordered newest-first, that's a job for tac. Different goals, different tools.
3. What Is rev and How Is It Different from tac?
Conclusion:
revflips the characters within each line left-to-right. It never changes line order — a completely different target fromtac(which reverses lines).
tac reverses lines... so what does rev reverse?rev flips the characters inside a single line. It's short for reverse. Not the order — the contents of each line get turned around.abc becomes cba?$ rev fruits.txt
elppa ananab yrrehc
tac vs rev (the heart of it)
| Command | What it reverses | apple → | Line order |
|---|---|---|---|
tac |
the order of lines | apple | changes |
rev |
characters per line | elppa | unchanged |
rev even comes from a different package — util-linux, not coreutils. Both are available out of the box, though.4. What Happens When You Combine tac and rev?
Conclusion:
tac file | revreverses both the order of lines and the characters in each line. Just connect them with a pipe.
tac's output into rev.$ tac fruits.txt | rev
yrrehc ananab elppa
rev | tac gives the same result — order doesn't matter here, which is the beauty of pipes.rev also reads standard input. echo "Linux" | rev returns xuniL. No file needed — try it through a pipe anytime.