terminal.cab

Terminal start Lesson 11 of 12

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

Chaining commands

10 minutes

Learning goals

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
3

in the glossary: grep

You 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

in the glossary: grep

>> 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 rent

in the glossary: echo

Error 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 plants

in the glossary: cat

Sort 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

in the glossary: sort

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

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

Build the command

Build the command that shows how often each line appears in the two note files together.

Your command

    Tap the blocks below to build the command.

    Building blocks

    Check works once your command has at least one block.

    Predict the output

    Command

    sort notes.txt > notes.txt; cat notes.txt

    Before the command, in your home folder ~

    • ~/notes.txt
    What does sort notes.txt > notes.txt; cat notes.txt print?

    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

    1. Question 1 of 4What does the | in grep 404 server.log | wc -l do?

      Check works once you have chosen an answer.

    2. Question 2 of 4notes.txt has three lines. What is in it after echo hi > notes.txt?

      Check works once you have chosen an answer.

    3. Question 3 of 4You run cat notes.txt missing.txt > all.txt. What still appears on the screen?

      Check works once you have chosen an answer.

    4. Question 4 of 4You want notes.txt sorted. Which ways keep your notes safe? Choose TWO.

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

    Cards

    1. Card 1 of 5

      Tap the card or press Space to turn it over.

    2. Card 2 of 5

      Tap the card or press Space to turn it over.

    3. Card 3 of 5

      Tap the card or press Space to turn it over.

    4. Card 4 of 5

      Tap the card or press Space to turn it over.

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

    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: open
    11. Lesson 11: done
    12. Lesson 12: open
    1 of 12

    Next: lesson 12, Final scenario: tidying up Downloads (15 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