tac and rev - Reversing Lines and Characters

tac and rev - Reversing Lines and Characters

What You'll Learn

  • How tac reverses the order of lines (last line first)
  • How rev reverses the characters in each line (abc → cba)
  • A clear answer to "which one do I use?" — tac vs rev
  • Practical tricks: reading logs newest-first and combining both

Quick Summary (the core difference)

  • Flip the order of linestac ("cat" spelled backwards)
  • Flip the characters inside a linerev (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
  • tac ships with GNU coreutils, rev ships with util-linux. Both are installed by default

1. What Is tac and What Can It Do?

Conclusion: tac prints a file's lines from last to first, line by line. Its name is cat spelled backwards.

Lina: Senpai, cat prints a file from top to bottom. What if I want it the other way — from the bottom up?
Linny-senpai: That's exactly when you reach for tac. It's cat spelled backwards, and the job matches the name: "print lines in reverse order."
Lina: Even the name is reversed!
Linny-senpai: Right. Let's look at a normal cat first.
$ cat fruits.txt
apple
banana
cherry
$ tac fruits.txt
cherry
banana
apple
Lina: The whole lines flipped from the bottom up! But the words themselves are unchanged.
Linny-senpai: That's the key point. 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 tac reorders them to read the newest events first.

Lina: What's the point of reversing lines, really?
Linny-senpai: The biggest use is log files. New records get appended at the bottom, so the "latest" entry is always last.
Lina: But I want the latest, and scrolling to the end every time is annoying...
Linny-senpai: That's where 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: rev flips the characters within each line left-to-right. It never changes line order — a completely different target from tac (which reverses lines).

Lina: tac reverses lines... so what does rev reverse?
Linny-senpai: rev flips the characters inside a single line. It's short for reverse. Not the order — the contents of each line get turned around.
Lina: So abc becomes cba?
Linny-senpai: Exactly. Let's compare on the same file.
$ 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
Lina: The names are both "reverse," but what they reverse is different. tac = lines, rev = characters.
Linny-senpai: Memorize that and you're set. By the way, 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 | rev reverses both the order of lines and the characters in each line. Just connect them with a pipe.

Lina: What if I want to flip both the lines and the characters?
Linny-senpai: Connect them with a pipe. Send tac's output into rev.
$ tac fruits.txt | rev
yrrehc
ananab
elppa
Lina: cherry comes first (lines reversed), and yrrehc shows the characters reversed too!
Linny-senpai: Right, both stages apply. 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.

5. Common Pitfalls and When to Use Which

Conclusion: tac reverses the order of lines; rev reverses characters within a line. Mixing them up is the main mistake — when unsure, test on a tiny file.

What you want Command
Reorder lines bottom-to-top tac file
Read a log newest-first from the top tac log
Flip characters in each line rev file
Reverse both lines and characters tac file | rev

Easy mistakes to avoid

  • Swapping tac and rev (using rev when you wanted lines reversed, then seeing "garbled" text)
  • Confusing tail with tac (tail keeps order, tac reverses it)
  • Running on a huge file blindly — test with head or a small example first