tac and rev - Reversing Lines and Characters

tac and rev - Reversing Lines and Characters

What you'll be able to do

  • Reverse the order of lines in a file with `tac`
  • Reverse the characters inside each line with `rev`
  • Tell `tac`, `rev`, and `tail` apart and pick the right one

Prerequisites (read these first)

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

Words used here (sorted out first)

  • Line: one line in a file. Everything between one line break and the next.
  • Line order: which line comes first, second, and so on. That is the only thing tac changes.
  • Characters in a line: the characters that make up one line. That is the only thing rev changes.
  • Standard input: the data arriving from the previous command through a pipe (|). It can stand in for a file.

"Reverse the lines" and "reverse the characters" are two separate jobs. Keep them apart and the rest of this article falls into place.

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.
Linny-senpai: The job matches the name too. It prints the 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.
Linny-senpai: It never touches what is inside a word like apple. So the spelling stays as it was.

The separator is a newline by default. Use -s to change it.

Where the separator ends up is less intuitive than you would expect, so there is no need to reach for it while you are starting out. Read man tac when you want the details.

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 is the point of reversing lines?
Linny-senpai: The biggest use is log files. New records get appended at the bottom.
Linny-senpai: So the latest event is always sitting at the very end.
Lina: But I want the latest, and scrolling to the end every time is a chore.
Linny-senpai: That's where tac shines. Reverse it and the newest line comes first.
Linny-senpai: Pair it with head and you get the last 5 events, newest first.
$ 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 is 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.
Linny-senpai: It only changes what is inside each line. The order of the lines stays put.
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. They even ship in different packages.
Linny-senpai: tac comes from coreutils and rev from util-linux. 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 straight into rev.
$ tac fruits.txt | rev
yrrehc
ananab
elppa
Lina: cherry comes first, so the lines are reversed. And yrrehc shows the characters reversed too.
Linny-senpai: Right, both stages apply. tac reversed the lines, then rev reversed the characters.
Linny-senpai: rev | tac gives the same result. The two commands change different things, so the order doesn't matter.

rev also reads standard input. echo "Linux" | rev returns xuniL. No file is needed, so you can try it through a pipe anytime.

5. Lina Gets Stuck: Reaching for rev Instead of tac

Conclusion: Using rev when you wanted lines reversed looks like garbled text. Nothing is broken — the characters are simply flipped.

Let's make a small log to practise on.

$ cat << 'EOF' > mini.log
2026-06-01 start
2026-06-02 error
2026-06-03 done
EOF
Lina: Senpai, I tried to read this log newest-first and the screen turned into a mess.
$ rev mini.log
trats 10-60-6202
rorre 20-60-6202
enod 30-60-6202
Lina: Is this garbled text? I panicked and thought I had broken the file.
Linny-senpai: Relax. Nothing is garbled. You used rev, so the characters inside each line are flipped left to right.
Lina: Wait, this is the correct behaviour?
Linny-senpai: It is. Look closely: 2026-06-01 became 10-60-6202. Read it from the right and the original comes back.
Lina: You're right, I can read it backwards. So the file was never damaged. What a relief.
Linny-senpai: To change the order of the lines, use tac. That gives you newest-first.
$ tac mini.log
2026-06-03 done
2026-06-02 error
2026-06-01 start
Lina: That's the shape I wanted. I'll watch out for mixing up tac and rev.
Linny-senpai: It is the most common slip. Remember that they target different things and you'll be fine.

Neither command changes the original file. Both only print the result to the screen. So even a mix-up leaves your file intact.

Note that tac and rev are not available in the virtual terminalAn interactive program that reads the commands you type and runs them. on this site yet. Try the examples in this article on your own Linux machine or terminal.

6. 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 makes the output look garbled
  • Confusing tail with tac. tail keeps the order, tac reverses it
  • Running on a huge file blindly. Test with head or a small example first

7. Mini Exercises: Try It Yourself

Conclusion: Three drills — reversing lines, reversing characters, and combining both — confirm by hand what each command targets.

Lina: I understand the difference. I want to confirm it hands-on.
Linny-senpai: Good. Here are three tasks. Create the practice file first, then try them.
$ cat << 'EOF' > animals.txt
penguin
tiger
whale
EOF

Task 1: Print the lines of animals.txt from the bottom up.

Show hint 1 (direction)

You want to change the order of the lines. The characters inside each line stay as they are.

Show hint 2 (command name)

The command is tac. No options needed.

Show answer
$ tac animals.txt
whale
tiger
penguin

Only the line order changed. The spelling of each word is untouched.

Task 2: Print each line of animals.txt with its characters flipped left to right.

Show hint 1 (direction)

This time the line order stays put. Only the characters inside each line get reversed.

Show hint 2 (command name)

The command is rev.

Show answer
$ rev animals.txt
niugnep
regit
elahw

The line order is unchanged. Only the characters flipped.

Task 3: Reverse both the line order and the characters inside each line.

Show hint 1 (direction)

Two jobs, one after the other. Feed the result of the first straight into the second.

Show hint 2 (command name)

You need both tac and rev. The symbol that connects them is |.

Show answer
$ tac animals.txt | rev
elahw
regit
niugnep

Writing rev animals.txt | tac gives the same result.

8. Review

Lina: Let me sum up. tac changes the line order, and rev changes the characters inside a line.
Linny-senpai: Exactly. The names look alike, so remember them by what they target.
Lina: To read a log newest-first, use tac. tail keeps the original order.
Linny-senpai: Perfect. Neither one changes the file, so when in doubt, test on a small one.

Today's 3-Line Summary

  1. tac reverses the order of the lines and leaves the characters alone
  2. rev reverses the characters in each line and leaves the line order alone
  3. To read a log newest-first, use tac. tail prints the tail in its original order

What to Read Next

Share this article

Next steps