Linux Basics Lesson 16 of 38
The 17:45 report
9 minutes
Learning goals
- Show a statistic on the screen and save it in the same run with tee.
- Collect the errors of a command in a file along with its output.
- Clean up files with spaces in their names, and nothing else.
The log has today's date, the time and the level, each separated by a single space. For the check over both logs, keep in mind that logs/old.log does not
exist. On this machine it also does not need sudo; on most real servers, logs under /var/log do.
Steps
Step 1/3: Count the INFO, WARN and ERROR lines of logs/app.log. Show the result, biggest first, and save it in ~/report.txt in the same run. Step 2/3: The ticket also wants the check over both logs, grep -c ERROR logs/app.log logs/old.log. Collect its output and its error message in ~/check.log. Step 3/3: Now clean up. Delete all .tmp files in ~/tmp, also the one with a space in its name, and nothing else. Scenario solved
17:45, shortly before quitting time. The team lead writes: "How many INFO, WARN and ERROR lines did we have today in logs/app.log? Put the evaluation in the ticket as a file, but tell me the numbers here right now. And clear out your tmp folder before you go." Three small jobs, and a pipe does most of it.
-
Step 1 done solved before
Count the INFO, WARN and ERROR lines of logs/app.log. Show the result, biggest first, and save it in ~/report.txt in the same run.
Hint 1: grep -o prints only the matching part of a line, here the level. Then comes the counting chain from module 3, and at the very end the T-piece.Hint 2: Type grep -oE 'INFO|WARN|ERROR' logs/app.log | sort | uniq -c | sort -rn | tee ~/report.txtSolution
grep -oE 'INFO|WARN|ERROR' logs/app.log | sort | uniq -c | sort -rn | tee ~/report.txtWhy: From the left: grep takes out the level, sort groups, uniq -c counts, sort -rn puts the biggest on top, and tee saves it while the numbers still appear for the chat. On a server with cut you could also write cut -d' ' -f3 logs/app.log. It is not loaded here.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 2 done solved before
The ticket also wants the check over both logs, grep -c ERROR logs/app.log logs/old.log. Collect its output and its error message in ~/check.log.
Hint 1: old.log does not exist, grep says so on stderr. A > alone only catches stdout.Hint 2: Type grep -c ERROR logs/app.log logs/old.log > ~/check.log 2>&1Solution
grep -c ERROR logs/app.log logs/old.log > ~/check.log 2>&1Why: The command ends with exit code 2 because of the missing log, and that is fine here. The error is part of the finding. Whoever reads the ticket now sees that the old log was missing, instead of believing it had no errors.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 3 done solved before
Now clean up. Delete all .tmp files in ~/tmp, also the one with a space in its name, and nothing else.
Hint 1: Without a special separator, xargs splits names at spaces. You know the pair of options from the last lesson. Try it with echo first.Hint 2: Type find ~/tmp -name "*.tmp" -print0 | xargs -0 rmSolution
find ~/tmp -name "*.tmp" -print0 | xargs -0 rmWhy: Quitting time, and nothing important is gone. The check looks at the folder and at whether notes.txt and keep.log were touched, not at the words you typed. -delete would have done it too, the -print0 form works with every command.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Scenario solved
Why each step mattered
- Step 1: From the left: grep takes out the level, sort groups, uniq -c counts, sort -rn puts the biggest on top, and tee saves it while the numbers still appear for the chat. On a server with cut you could also write cut -d' ' -f3 logs/app.log. It is not loaded here.
- Step 2: The command ends with exit code 2 because of the missing log, and that is fine here. The error is part of the finding. Whoever reads the ticket now sees that the old log was missing, instead of believing it had no errors.
- Step 3: Quitting time, and nothing important is gone. The check looks at the folder and at whether notes.txt and keep.log were touched, not at the words you typed. -delete would have done it too, the -print0 form works with every command.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Loading terminal …
Esc, then Tab leaves the terminal
Step 1 of 3
Cards
-
Card 1 of 2
Tap the card or press Space to turn it over.
-
Card 2 of 2
Tap the card or press Space to turn it over.
Cheat sheet
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 17 KB)
- View as image (PNG, 41 KB)