Reading and searching · Linux and the Command Line
How do you read and search files from the terminal?
Course contentsWhy you need it
Logs are the first place you look when something breaks, and they can be millions of lines long. You won't open them in an editor; you'll ask for the last lines, count the errors, and search for one user. A handful of commands does all of that.
The intuition
cat file: print the whole filehead -n 3 file,tail -n 3 file: the first or last 3 lineswc -l file: count linesgrep word file: only the lines containing word
grep has useful switches: -i ignores case, -c counts matches, -n shows line numbers, -v shows lines that do not match, -r searches a whole folder.
Definition
grep
Prints the lines of its input that match a pattern. If nothing matches it prints nothing and reports failure, which scripts can test.
Option (flag)
A word starting with - that changes what a command does, like `-n 3` or `-i`. Several single-letter options can be joined: `-in`.
Examples
First look at a log
Run it in the lessonHow long is it, how does it start, and what happened last?
wc -l logs/app.log
head -n 2 logs/app.log
tail -n 1 logs/app.log- L18 lines.
8 logs/app.log - L2The first two lines.
09:00 INFO server started - L3The last line: the most recent event.
09:20 INFO user ada logged out
Search with grep
Run it in the lessonFind errors, count them, and find everything that is not INFO.
grep ERROR logs/app.log
grep -c ERROR logs/app.log
grep -v INFO logs/app.log
grep -in "ADA" logs/app.log- L1Three lines contain ERROR.
- L2-c prints the count instead of the lines.
3 - L3-v: WARN and ERROR lines, everything that isn't INFO.
- L4-i ignores case, -n adds line numbers.
2:09:01 INFO user ada logged in
Common mistakes
cat on a huge file
cat on a 2 GB log floods your screen for minutes. Start with wc -l, head or tail, or grep for what you need.
grep is case-sensitive
grep error misses ERROR. Add -i when you don't know how the text is written.
Quick quiz
1. What does this print?
grep -c payment logs/app.logShow the answer
Answer: A
Two lines mention payment timeout.
2. What does this print?
grep -v -c ERROR logs/app.logShow the answer
Answer: A
8 lines, 3 contain ERROR, so 5 don't.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictRead the users
Type exactly what these commands print.
- ModifyOnly the problems
This prints every line of the log. Change it so it prints only the lines with WARN or ERROR, with line numbers. (Hint: they are the lines that are not INFO.)
- WriteWho logged in?
Print every line of
logs/app.logthat containslogged in, then print how many lines containERROR.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Log file | 日志文件 | A file a program appends a line to for each thing that happens. |
| Option (flag) | 选项 | A word starting with - that changes what a command does. |