tacPrint a file with the lines in reverse order
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.
tac [OPTION]... [FILE]...You want to see a list or a log from the bottom up.
tacPrint lines in reverse order, starting from the end of the file.
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.
-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 firstcat big.log | tac | headThrough 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 linetac reverses the order of lines, not the characters in them.
rev for characters: echo abc | revtac app.log.2.gz | grep -m1 ERRORtac 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 ERRORTry these on a Linux machine of your own. Each task has a hint and a solution.
Show the last five lines of ~/logs/app.log, newest first.
Find the most recent ERROR line in ~/logs/app.log.
Show the last three deploys from ~/logs/deploy.log in chronological order.
tacPrint a file with the lines in reverse order
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.
tac [OPTION]... [FILE]...You want to see a list or a log from the bottom up.
tacPrint lines in reverse order, starting from the end of the file.
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.
-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 firstcat big.log | tac | headThrough 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 linetac reverses the order of lines, not the characters in them.
rev for characters: echo abc | revtac app.log.2.gz | grep -m1 ERRORtac 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 ERRORTry these on a Linux machine of your own. Each task has a hint and a solution.
Show the last five lines of ~/logs/app.log, newest first.
Find the most recent ERROR line in ~/logs/app.log.
Show the last three deploys from ~/logs/deploy.log in chronological order.