LearnLinux FoundationsReading Files

tac

tac - a hands-on Linux lab on a real virtual machine.

Print any file bottom to top with tac, cat spelled backwards. Reverse a log so the newest line is first, see the one-line-file edge case, and flip the output of a pipeline with tac, all in the practice terminal.

You already know cat, the command that prints a whole file to the screen from top to bottom. A folder called intercept is sitting in your home directory, packed with files a listening station captured overnight. Open the boot log with cat and it comes out oldest line first, newest line last, the way it was written.

That is backwards from how you usually want to read a log: a file a program writes one line at a time, newest entry at the bottom. When something just broke, the line you care about is the last one, and cat buries it at the far end of the scroll. This lesson is about one command that flips that around: tac.

The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. It replays the real captured output here. On the real machine at the end of the module, the mission builds this same intercept folder and you run tac for real.

The command is tac, and the name is the whole idea: it is cat spelled backwards, and it does cat's job backwards. It prints a file with the lines in reverse order, last line first. Point it at the boot log, ~/intercept/logs/messages.log, and press Enter:

tac ~/intercept/logs/messages.log

prompt: student@linuxcamp:~$ answer: tac ~/intercept/logs/messages.log output: line 8: ready line 7: running diagnostics line 6: user student logged in line 5: starting services line 4: mounting filesystems line 3: network interface up line 2: loading kernel modules line 1: system boot hint: Type tac, a space, then the path to the file: tac ~/intercept/logs/messages.log

There is the whole file, all eight lines, but upside down. line 8: ready, the last line in the file, printed first, and line 1: system boot, the first line, printed last. That is the entire job of tac: same file cat would show you, with the line order flipped end to end.

It is worth being precise about what tac reverses, because the word "backwards" can mislead. tac reverses the order of the lines. It does not reverse the characters inside a line. Look again at the output you just got: line 8: ready still reads left to right exactly as it always did. Only its position moved, from the bottom of the file to the top.

So the file is turned upside down, not written in a mirror. Every line is intact and readable; it is the stack of lines that got flipped. That is the one sentence to hold on to: cat prints a file top to bottom, tac prints the same file bottom to top. Same lines, opposite order.

The name is your memory aid, and it is exact. tac is cat reversed letter for letter, and its behavior is cat reversed line for line. If you can remember what cat does, you already know what tac does: the same thing, from the other end.

tac works on any text file, not just logs. In the intel/ folder there is haiku.txt, a three-line poem. Reading it with tac makes the reversal small enough to check by eye: three lines, and you can watch them swap ends.

Reverse the poem with tac ~/intercept/intel/haiku.txt:

prompt: student@linuxcamp:~$ answer: tac ~/intercept/intel/haiku.txt output: dawn brings fresh uptime packets cross the dark network silent servers hum hint: Type tac, a space, then the path to the poem: tac ~/intercept/intel/haiku.txt

The poem reads bottom to top now. Its real last line, dawn brings fresh uptime, is on top, and its real first line, silent servers hum, is at the bottom. Three lines make the flip easy to trust: whatever was last is now first, whatever was first is now last, and the middle line stays put. On an eight-line log or a three-line poem, the rule is the same.

Here is the honest gotcha, and it follows straight from "tac reverses lines." If a file has only one line, there is nothing to reorder. tac still runs, but the output looks identical to cat, because a stack of one line is the same upside down as right side up.

The intel/ folder has exactly such a file: notes.md, a single line of text. Run tac on it and watch it come back unchanged:

tac ~/intercept/intel/notes.md

prompt: student@linuxcamp:~$ answer: tac ~/intercept/intel/notes.md output: quick note: rotate the keys at 0600 hint: Same as always, just point tac at the one-line file: tac ~/intercept/intel/notes.md

One line in, the same one line out. This is not a bug, it is the definition working correctly: tac reorders whole lines, and one line has no other line to trade places with. It is a useful reminder that tac never touches the text inside a line. It only ever rearranges the lines as a whole, and with a single line there is simply nothing to rearrange.

So far you have handed tac a file to open. It has a second, quieter talent: it can reverse whatever is piped into it. A pipe, written |, takes the output of the command on its left and feeds it as input to the command on the right. So tac does not need a file of its own; it will happily reverse a stream of lines that another command produced.

Here head -n 3 grabs the first three lines of the log, and the pipe hands those three lines to tac to flip. You do not need to know head in depth, only that it emits the top three lines, which then arrive at tac in order:

head -n 3 ~/intercept/logs/messages.log | tac

prompt: student@linuxcamp:~$ answer: head -n 3 ~/intercept/logs/messages.log | tac output: line 3: network interface up line 2: loading kernel modules line 1: system boot hint: Build the pipe left to right, with tac last: head -n 3 ~/intercept/logs/messages.log | tac

The three lines arrived at tac as 1, 2, 3 and left it as 3, 2, 1. tac reversed them exactly as it reverses a file, because to tac a stream of lines is a stream of lines, whether it came from a file you named or from a pipe. This is why tac shows up at the end of pipelines: whenever some other command produces lines in one order and you want them the other way, you tack | tac on the end.

Scaffolding off. No command is shown this time, and you have everything you need.

You want the boot log, ~/intercept/logs/messages.log, printed with its lines in reverse order, newest event first. Reach for the command whose whole job is that, the one whose name is cat backwards, and point it at the log.

prompt: student@linuxcamp:~$ answer: tac ~/intercept/logs/messages.log output: line 8: ready line 7: running diagnostics line 6: user student logged in line 5: starting services line 4: mounting filesystems line 3: network interface up line 2: loading kernel modules line 1: system boot hint: The command that reverses line order is cat spelled backwards. Point it at ~/intercept/logs/messages.log

Nailed it. tac flipped all eight lines so line 8: ready leads and line 1: system boot trails, the newest event first. You picked the right command from its job alone, with nothing shown, which is exactly the move the real machine will ask of you.

One more, still with no command shown. This time the target is the poem, ~/intercept/intel/haiku.txt.

You want its three lines printed in reverse order, real last line on top. Same tool, different file. Before you press Enter, decide which line you expect to see first: it should be the line that sits at the bottom of the real file, dawn brings fresh uptime.

prompt: student@linuxcamp:~$ answer: tac ~/intercept/intel/haiku.txt output: dawn brings fresh uptime packets cross the dark network silent servers hum hint: Same command as the log challenge, aimed at ~/intercept/intel/haiku.txt

Exactly as you called it: dawn brings fresh uptime came out first, because it is the file's last line and tac always leads with the last line. Two challenges, two files, zero commands shown. The tool is yours now.

You earned this cheat sheet. Every row is a use of tac you just ran:

The one idea under all of it: tac reverses the order of lines, and only their order. It is cat from the bottom up. Reach for it in two cases. First, when the newest lines sit at the end of a file but you want to read them first. Second, when a pipeline hands you lines in the wrong order and you want them flipped.

tac sits next to cat, head, and tail in the reading toolkit: cat prints a whole file, head and tail take an end of it, and tac prints it bottom to top. Each has its own lesson; here you only needed tac.

The practice terminal has shown you every shape of tac: reversing a file, the one-line edge case, and reversing lines that arrive through a pipe. The Reading Files module ends with one real machine, and that is where you run tac for real.

In the Reading Files capstone mission, a Linux VM boots just for you and builds this same intercept folder. One of its objectives uses exactly what you practiced here: point tac at a file, the boot log or the poem, to read it in reverse line order. One difference from here: the lab shows no commands. You read the objective, you recall tac, you type it. That recall is what makes it stick.

Finish the other Reading Files lessons, then go read the traffic bottom up for real.

Practice tac in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.