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
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
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
tacchanges. - Characters in a line: the characters that make up one line. That is the only thing
revchanges. - 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 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.cat first.$ cat fruits.txt
apple banana cherry
$ tac fruits.txt
cherry banana apple
tac only moves the order of the lines.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
tacreorders them to read the newest events first.
tac shines. Reverse it and the newest line comes first.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:
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.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 |
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 | revreverses both the order of lines and the characters in each line. Just connect them with a pipe.
tac's output straight into rev.$ tac fruits.txt | rev
yrrehc ananab elppa
tac reversed the lines, then rev reversed the characters.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
revwhen 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
$ rev mini.log
trats 10-60-6202 rorre 20-60-6202 enod 30-60-6202
rev, so the characters inside each line are flipped left to right.2026-06-01 became 10-60-6202. Read it from the right and the original comes back.tac. That gives you newest-first.$ tac mini.log
2026-06-03 done 2026-06-02 error 2026-06-01 start
tac and rev.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:
tacreverses the order of lines;revreverses 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
tacandrev. Usingrevwhen you wanted lines reversed makes the output look garbled - Confusing
tailwithtac.tailkeeps the order,tacreverses it - Running on a huge file blindly. Test with
heador 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.
$ 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
tac changes the line order, and rev changes the characters inside a line.tac. tail keeps the original order.Today's 3-Line Summary
tacreverses the order of the lines and leaves the characters alonerevreverses the characters in each line and leaves the line order alone- To read a log newest-first, use
tac.tailprints the tail in its original order