CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File Viewing & Editing / tac

tac

Print a file with the lines in reverse order

Beginnerviewinglogspipes

tac is cat backwards: it prints the last line first. On logs that means newest first, so tac app.log | grep -m1 ERROR finds the most recent error instantly, even in a multi-gigabyte file, without reading it from the top.

Syntaxtac [OPTION]... [FILE]...

Watch it run

You want to see a list or a log from the bottom up.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
cat steps.txt

The normal order, top to bottom.

Anatomy of a command

$
commandtac

Print lines in reverse order, starting from the end of the file.

How it works

For a regular file, tac seeks to the end and reads backwards in chunks, looking for the line separator (newline by default) and printing each line once it has found where it starts. It never has to read the beginning of the file unless it gets there, which is why tac big.log | head is fast on a 10 GB log. When the input is a pipe or terminal, which can't be read backwards, tac first copies everything into a temporary file and then reverses that, so it has to wait for the input to end. The separator is treated as a line terminator that comes after each record, so a file whose last line has no trailing newline produces a joined line. -s sets a different separator and -r makes it a regular expression.

Options you'll actually use

-s SEPUse SEP instead of newline as the record separator
-rTreat the separator as a regular expression
-bAttach the separator before each record instead of after
| grep -m1Common pairing: newest matching line only
| head -n NCommon pairing: last N lines, newest first

Common mistakes

cat big.log | tac | head

Through a pipe tac can't seek, so it must buffer the entire input to a temp file before printing anything.

tac big.log | head (give tac the file directly)
Using tac to reverse the characters of a line

tac reverses the order of lines, not the characters in them.

rev for characters: echo abc | rev
tac app.log.2.gz | grep -m1 ERROR

tac reverses the compressed bytes line by line, producing binary garbage; grep finds nothing or prints 'binary file matches'.

zcat app.log.2.gz | tac | grep -m1 ERROR

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show the last five lines of ~/logs/app.log, newest first.

  2. 2

    Find the most recent ERROR line in ~/logs/app.log.

  3. 3

    Show the last three deploys from ~/logs/deploy.log in chronological order.

Related commands

catPrint and concatenate filestailShow the last lines of a file and follow it livegrepSearch text for lines that match a patternlessBrowse files and output: scroll, search, filter, follow
PreviouscatNexthead
Linux / File Viewing & Editing / tac

tac

Print a file with the lines in reverse order

Beginnerviewinglogspipes

tac is cat backwards: it prints the last line first. On logs that means newest first, so tac app.log | grep -m1 ERROR finds the most recent error instantly, even in a multi-gigabyte file, without reading it from the top.

Syntaxtac [OPTION]... [FILE]...

Watch it run

You want to see a list or a log from the bottom up.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
cat steps.txt

The normal order, top to bottom.

Anatomy of a command

$
commandtac

Print lines in reverse order, starting from the end of the file.

How it works

For a regular file, tac seeks to the end and reads backwards in chunks, looking for the line separator (newline by default) and printing each line once it has found where it starts. It never has to read the beginning of the file unless it gets there, which is why tac big.log | head is fast on a 10 GB log. When the input is a pipe or terminal, which can't be read backwards, tac first copies everything into a temporary file and then reverses that, so it has to wait for the input to end. The separator is treated as a line terminator that comes after each record, so a file whose last line has no trailing newline produces a joined line. -s sets a different separator and -r makes it a regular expression.

Options you'll actually use

-s SEPUse SEP instead of newline as the record separator
-rTreat the separator as a regular expression
-bAttach the separator before each record instead of after
| grep -m1Common pairing: newest matching line only
| head -n NCommon pairing: last N lines, newest first

Common mistakes

cat big.log | tac | head

Through a pipe tac can't seek, so it must buffer the entire input to a temp file before printing anything.

tac big.log | head (give tac the file directly)
Using tac to reverse the characters of a line

tac reverses the order of lines, not the characters in them.

rev for characters: echo abc | rev
tac app.log.2.gz | grep -m1 ERROR

tac reverses the compressed bytes line by line, producing binary garbage; grep finds nothing or prints 'binary file matches'.

zcat app.log.2.gz | tac | grep -m1 ERROR

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show the last five lines of ~/logs/app.log, newest first.

  2. 2

    Find the most recent ERROR line in ~/logs/app.log.

  3. 3

    Show the last three deploys from ~/logs/deploy.log in chronological order.

Related commands

catPrint and concatenate filestailShow the last lines of a file and follow it livegrepSearch text for lines that match a patternlessBrowse files and output: scroll, search, filter, follow
PreviouscatNexthead