Terminal start Lesson 10 of 12
Searching
10 minutes
Learning goals
- Find lines in files with grep and adjust the search with -i, -n, -v and -c.
- Search whole folders with grep -r.
- Find files by name and type with find, with the pattern in quotes.
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:
Example:
grep 404 projects/website/server.log
08:05 GET /about.html 404
08:12 GET /contact.html 404
08:27 GET /favicon.ico 404Three 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
9Whole 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 garlicFiles 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:
Example:
find . -name '*.txt'
./Documents/Holiday 2027.txt
./Documents/letter.txt
./Documents/notes.txt
./Documents/recipes.txt
./notes.txt-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/websiteAlways 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.
Task complete
Solution
grep -n 404 projects/website/server.log Why: With the line number you can jump straight to the spot later, for example in less or an editor.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
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.
Task complete
Solution
grep -ic garlic Documents/recipes.txt Why: Without -i the line "Garlic bread" would be missing. Small letters and capitals are different characters for grep.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
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.
Task complete
Solution
grep -v GET projects/website/server.log Why: Hiding what you already know leaves the unusual lines. In a big log those are often the interesting ones.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
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.
Task complete
Solution
find . -name '*.jpg' Why: You do not need to know where a file is. find looks in every folder below the start for you.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
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.logBefore the command, in your home folder ~
- ~/projects/website/server.log
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
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works when exactly 2 are chosen. Chosen: 0.
Cards
-
Card 1 of 4
Tap the card or press Space to turn it over.
-
Card 2 of 4
Tap the card or press Space to turn it over.
-
Card 3 of 4
Tap the card or press Space to turn it over.
-
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
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
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: done
- Lesson 11: open
- Lesson 12: open
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
-
grep <word> <file>lines with the word -
grep -i / -n / -v / -cignore case, numbers, inverted, count -
grep -r <word> <folder>search a whole folder -
find . -name '<pattern>'files by name, pattern in quotes -
find . -type f / -type donly files / only folders