Everyone Can Learn Program
中文 Start learning
Core · LinuxLesson 2 of 6Free to read

Reading and searching · Linux and the Command Line

How do you read and search files from the terminal?

Course contents

Why 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 file
  • head -n 3 file, tail -n 3 file: the first or last 3 lines
  • wc -l file: count lines
  • grep 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 lesson

How 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
Step by step
  1. L18 lines.8 logs/app.log
  2. L2The first two lines.09:00 INFO server started
  3. L3The last line: the most recent event.09:20 INFO user ada logged out

Search with grep

Run it in the lesson

Find 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
Step by step
  1. L1Three lines contain ERROR.
  2. L2-c prints the count instead of the lines.3
  3. L3-v: WARN and ERROR lines, everything that isn't INFO.
  4. 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.log
  1. A
    2
  2. B
    3
  3. C
    1
Show the answer

Answer: A

Two lines mention payment timeout.

2. What does this print?

grep -v -c ERROR logs/app.log
  1. A
    5
  2. B
    3
  3. C
    8
Show 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.

  1. PredictRead the users

    Type exactly what these commands print.

  2. 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.)

  3. WriteWho logged in?

    Print every line of logs/app.log that contains logged in, then print how many lines contain ERROR.

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

TermChineseDefinition
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.