terminal.cab

Terminal start Lesson 10 of 12

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Searching

10 minutes

Learning goals

Lines with a word: grep

grep gets a word (the pattern) and one or more files. It prints every line that contains the word. In the website log, the code 404 means "not found":

grep 404 projects/website/server.log

Purpose: Shows every line of a file that contains a word.

Syntax: grep [OPTION]... PATTERN FILE...

Options:

  • -i ignore upper and lower case
  • -n show the line number in front
  • -v show the lines that do not match

Example:

grep 404 projects/website/server.log
08:05 GET /about.html 404
08:12 GET /contact.html 404
08:27 GET /favicon.ico 404

in the glossary: grep

Three options change the search: -i ignores upper and lower case, -n puts the line number in front, and -v turns the search around and shows the lines without the word.

grep -n 404 projects/website/server.log
4:08:05 GET /about.html 404
7:08:12 GET /contact.html 404
12:08:27 GET /favicon.ico 404
grep -v GET projects/website/server.log
08:00 server started
08:30 server stopped

If you only need the number, -c counts the matching lines:

grep -c 200 projects/website/server.log

Purpose: Counts the matching lines instead of showing them.

Syntax: grep -c PATTERN FILE

Example:

grep -c 200 projects/website/server.log
9

in the glossary: grep

Whole folders: grep -r

With -r (recursive), grep searches every file in a folder and its subfolders. In front of each hit stands the file it comes from. Together with -i, "Garlic bread" counts as well:

grep -ri garlic Documents

Purpose: Searches every file in a folder and its subfolders.

Syntax: grep -r PATTERN FOLDER

Example:

grep -ri garlic Documents
Documents/recipes.txt:- 2 cloves of garlic
Documents/recipes.txt:Garlic bread
Documents/recipes.txt:- 3 cloves of garlic

in the glossary: grep

Files by name: find

find walks through a folder and all folders below it. With -name it only shows names that match a pattern. The dot means "start here":

find . -name '*.txt'

Purpose: Finds files by name, in this folder and all folders below.

Syntax: find FOLDER -name 'PATTERN'

Options:

  • -iname like -name, upper and lower case do not matter
  • -type only files (f) or only folders (d)

Example:

find . -name '*.txt'
./Documents/Holiday 2027.txt
./Documents/letter.txt
./Documents/notes.txt
./Documents/recipes.txt
./notes.txt

in the glossary: find

-iname does the same without caring about upper and lower case. -type f only shows files, -type d only folders:

find . -type d

Purpose: Lists all folders below the current one.

Syntax: find FOLDER -type d

Example:

find . -type d
.
./.config
./Documents
./Downloads
./Pictures
./projects
./projects/website

in the glossary: find

Always put the pattern for find in quotes. Without them, the shell replaces *.txt with the matching names in the current folder before find even starts, and you get a different search than you typed:

find . -name *.txt
./Documents/notes.txt
./notes.txt

Only the files called exactly notes.txt turn up, because the shell turned *.txt into notes.txt.

Practice

Terminal task 1/4

Which lines of projects/website/server.log contain 404? Show them with their line numbers.

Task 1/4: Which lines of projects/website/server.log contain 404? Show them with their line numbers.

Which lines of projects/website/server.log contain 404? Show them with their line numbers.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

Terminal task 2/4

In how many lines of Documents/recipes.txt does garlic appear, written small or with a capital letter? Let grep count.

Task 2/4: In how many lines of Documents/recipes.txt does garlic appear, written small or with a capital letter? Let grep count.

In how many lines of Documents/recipes.txt does garlic appear, written small or with a capital letter? Let grep count.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

Terminal task 3/4

Show all lines of projects/website/server.log that are not a GET request.

Task 3/4: Show all lines of projects/website/server.log that are not a GET request.

Show all lines of projects/website/server.log that are not a GET request.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

Terminal task 4/4

Find every .jpg file in your home folder and all folders below it, with find.

Task 4/4: Find every .jpg file in your home folder and all folders below it, with find.

Find every .jpg file in your home folder and all folders below it, with find.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

Find the error

This command should find all .txt files, but it only finds the ones called notes.txt. Tap the part that is wrong.

Parts of the command

Check works once you have tapped a part.

Predict the output

Command

grep -c 404 projects/website/server.log

Before the command, in your home folder ~

  • ~/projects/website/server.log
What does grep -c 404 projects/website/server.log print?

Check works once you have chosen an answer.

Next up: chaining commands, so the hits of grep go straight into the next command or into a file.

Quiz

  1. Question 1 of 4You are looking for a file whose name you know, but not its folder. Which command?

    Check works once you have chosen an answer.

  2. Question 2 of 4What does -i do for grep?

    Check works once you have chosen an answer.

  3. Question 3 of 4Which command lists only the folders below the current folder?

    Check works once you have chosen an answer.

  4. Question 4 of 4You want the lines with ERROR in a log, with line numbers, no matter how ERROR is written. Which options? Choose TWO.

    Check works when exactly 2 are chosen. Chosen: 0.

Cards

  1. Card 1 of 4

    Tap the card or press Space to turn it over.

  2. Card 2 of 4

    Tap the card or press Space to turn it over.

  3. Card 3 of 4

    Tap the card or press Space to turn it over.

  4. Card 4 of 4

    Tap the card or press Space to turn it over.

End of the lesson

Still open: 6 exercises, 4 quiz questions

Next: lesson 11, Chaining commands (10 min)

What you can do now: grep 404 projects/website/server.log, grep -c 200 projects/website/server.log, grep -ri garlic Documents, find . -name '*.txt', find . -type d

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: done
  11. Lesson 11: open
  12. Lesson 12: open
1 of 12

Next: lesson 11, Chaining commands (10 min) That's enough for today

Path complete

You finished all 12 lessons of Terminal start.

On to Linux Basics: lesson 1, Finding your way around a strange server (8 min) Back to the path

Cheat sheet