Terminal start Lesson 11 of 12
Chaining commands
10 minutes
Learning goals
- Pass the output of one command to the next with the pipe |.
- Write output to a file with > and add to a file with >>.
- Send error messages elsewhere with 2> and throw them away with /dev/null.
- Count, sort and summarize lines with wc -l, sort and uniq -c.
The pipe: |
The pipe takes the output of the command on the left and hands it to the command on the right as input. wc -l counts
lines, so together they count the hits of grep:
grep 404 projects/website/server.log | wc -l
Purpose: Counts the hits of grep. The pipe passes them to wc.
Syntax: COMMAND1 | COMMAND2
Example:
grep 404 projects/website/server.log | wc -l
3You can chain as many commands as you like. Each one gets what the one before it produced.
Into a file: > and >>
> sends the output into a file instead of onto the screen. The file is created if it does not exist:
grep 404 projects/website/server.log > errors.txt
Purpose: Writes the output into a file instead of onto the screen.
Syntax: COMMAND > FILE
Example:
grep 404 projects/website/server.log > errors.txt
cat errors.txt
08:05 GET /about.html 404
08:12 GET /contact.html 404
08:27 GET /favicon.ico 404>> adds to the end of a file. What was in it stays:
echo "Pay the rent" >> notes.txt
Purpose: Adds the output at the end of a file. What was there stays.
Syntax: COMMAND >> FILE
Example:
echo "Pay the rent" >> notes.txt
cat notes.txt
Buy milk
Call the dentist
Water the plants
Pay the rentError messages: 2> and /dev/null
Error messages travel on their own channel. That is why they still show up on the screen when you redirect with >.
2> redirects only the error messages. /dev/null is a bin with no bottom: whatever goes in is gone.
cat notes.txt missing.txt 2>/dev/null
Purpose: Throws the error messages away and keeps the normal output.
Syntax: COMMAND 2>/dev/null
Example:
cat notes.txt missing.txt
Buy milk
Call the dentist
Water the plants
cat: missing.txt: No such file or directory
cat notes.txt missing.txt 2>/dev/null
Buy milk
Call the dentist
Water the plantsSort and count: sort and uniq -c
sort sorts lines. uniq -c puts lines that are the same and next to each other into one line, with the count in
front. That is why sort comes first. Together they show which notes are in both files:
sort notes.txt Documents/notes.txt | uniq -c
Purpose: Sorts lines and counts how often each one appears.
Syntax: sort FILE... | uniq -c
Example:
sort notes.txt Documents/notes.txt | uniq -c
2 Buy milk
1 Call the dentist
1 Water the plants"Buy milk" appears twice. uniq compares lines of text, not whole files.
Looking ahead: why sort file > file empties the file
One trap catches everyone once. You want to sort a file in place and type sort notes.txt > notes.txt:
sort notes.txt > notes.txt
cat notes.txt
The file is empty. The shell sets up > before sort starts, and > empties the file first. When sort reads it, there
is nothing left. Write into a new file instead, and rename it afterwards if you want.
Practice
Terminal task 1/3
How many requests in projects/website/server.log ended in 404? Find them with grep and count them with wc -l, joined by a pipe.
Task 1/3: How many requests in projects/website/server.log ended in 404? Find them with grep and count them with wc -l, joined by a pipe.
How many requests in projects/website/server.log ended in 404? Find them with grep and count them with wc -l, joined by a pipe.
Task complete
Solution
grep 404 projects/website/server.log | wc -l Why: wc counts anything you hand it. With a pipe it works for every command that prints lines, not only 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 2/3
Add the line "Pay the rent" at the end of notes.txt. The three lines that are already there stay.
Task 2/3: Add the line "Pay the rent" at the end of notes.txt. The three lines that are already there stay.
Add the line "Pay the rent" at the end of notes.txt. The three lines that are already there stay.
Task complete
Solution
echo "Pay the rent" >> notes.txt Why: With a single > the file would only contain "Pay the rent". The second > is the difference between adding and replacing.
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/3
Merge notes.txt and Documents/notes.txt into one sorted list without duplicate lines. Save it as all-notes.txt in your home folder. Both note files stay as they are.
Task 3/3: Merge notes.txt and Documents/notes.txt into one sorted list without duplicate lines. Save it as all-notes.txt in your home folder. Both note files stay as they are.
Merge notes.txt and Documents/notes.txt into one sorted list without duplicate lines. Save it as all-notes.txt in your home folder. Both note files stay as they are.
Task complete
Solution
ls all-notes.txt
sort notes.txt Documents/notes.txt | uniq > all-notes.txt Why: The result goes into a new file, so the two originals stay untouched. If something is wrong, you just run it again.
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.
Build the command
Build the command that shows how often each line appears in the two note files together.
Your command
Building blocks
Check works once your command has at least one block.
Predict the output
Command
sort notes.txt > notes.txt; cat notes.txtBefore the command, in your home folder ~
- ~/notes.txt
Check works once you have chosen an answer.
Fill the gap
To throw error messages away, you send them with 2> to .
To throw error messages away, you send them with 2> to ….
Check works once you have typed an answer.
Next up: the final scenario. A messy Downloads folder is waiting, and you have all the tools to tidy it up.
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 5
Tap the card or press Space to turn it over.
-
Card 2 of 5
Tap the card or press Space to turn it over.
-
Card 3 of 5
Tap the card or press Space to turn it over.
-
Card 4 of 5
Tap the card or press Space to turn it over.
-
Card 5 of 5
Tap the card or press Space to turn it over.
End of the lesson
Still open: 6 exercises, 4 quiz questions
Next: lesson 12, Final scenario: tidying up Downloads (15 min)
What you can do now: grep 404 projects/website/server.log | wc -l, grep 404 projects/website/server.log > errors.txt, echo "Pay the rent" >> notes.txt, cat notes.txt missing.txt 2>/dev/null, sort notes.txt Documents/notes.txt | uniq -c
+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: open
- Lesson 11: done
- 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
-
a | boutput of a into b -
> file, >> filewrite (replace), add at the end -
2>/dev/nullthrow error messages away -
wc -lcount lines -
sort | uniq -ccount equal lines