terminal.cab

Linux Basics Lesson 14 of 38

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

Steering the streams

10 minutes

Learning goals

The pipe

A | B sends what A prints into the input of B. At every |, think "... and with that, then". Build a pipe from left to right and check after every step: only when grep shows the right thing do you hang | wc -l onto it.

grep ERROR logs/app.log | wc -l

Purpose: Sends the output of the left command into the input of the right one.

Syntax: A | B

Example:

grep ERROR logs/app.log | wc -l
2
ls -lt logs | head -n 2
total 8
-rw-r--r-- 1 lerner lerner 621 Oct 17 09:41 app.log

in the glossary: grep

Into a file: > and >>

> writes stdout to a file instead of the screen, and >> appends to it. < is the other direction and feeds a file into the input.

grep ERROR logs/app.log > errors.txt

Purpose: Writes output to a file instead of the screen. > overwrites, >> appends.

Syntax: COMMAND > FILE, COMMAND >> FILE

Options:

  • > overwrite the file without asking
  • >> append to the end
  • < FILE read the input from a file

Example:

grep ERROR logs/app.log > errors.txt
wc -l errors.txt
2 errors.txt
echo "checked 09:45" >> errors.txt
wc -l < errors.txt
3

in the glossary: grep

wc -l < errors.txt prints no file name, because wc only ever sees a stream. That is handy if you want the bare number.

The second channel: 2>

Why does the message stay on the screen? Programs have two output channels: stdout (channel 1) for results and stderr (channel 2) for errors. > only catches channel 1. Try it with a folder that does not exist:

ls logs nope > all.txt 2>&1

Purpose: Catches error messages too. Channel 2 goes where channel 1 is going right now.

Syntax: COMMAND > FILE 2>&1

Options:

  • 2> FILE only the errors into a file
  • 2>/dev/null throw the errors away
  • &> FILE both into one file, bash shorthand

Example:

ls logs nope > all.txt
ls: cannot access 'nope': No such file or directory
cat all.txt
logs:
app.log
app.log.1
ls logs nope > all.txt 2>&1
cat all.txt
ls: cannot access 'nope': No such file or directory
logs:
app.log
app.log.1

in the glossary: ls

2>&1 says: channel 2 goes where channel 1 is going right now. With 2> you send only the errors somewhere, and /dev/null is the place for what you really do not want to see. In bash, &> is the short form for both at once.

ls logs nope 2> problems.txt
logs:
app.log  app.log.1
cat problems.txt
ls: cannot access 'nope': No such file or directory
ls logs nope 2>/dev/null
logs:
app.log  app.log.1
ls logs nope &> both.txt
wc -l both.txt
4 both.txt

A safety catch: noclobber

set -o noclobber protects files from >. As long as it is on, > refuses to overwrite an existing file, and only >| still does:

set -o noclobber
echo x > logs/app.log
bash: logs/app.log: cannot overwrite existing file
wc -l logs/app.log
17 logs/app.log
set +o noclobber

Put the line in your ~/.bashrc if you want it always. On a real server, you are not protected from anything that does not go through >, such as cp or rm.

Practice

Terminal task 1/3

Save the ERROR lines of logs/app.log in the file errors.txt in your home folder.

Task 1/3: Save the ERROR lines of logs/app.log in the file errors.txt in your home folder.

Save the ERROR lines of logs/app.log in the file errors.txt in your home folder.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Terminal task 2/3

Add the WARN lines of logs/app.log to the end of notes.txt. What was in notes.txt must stay.

Task 2/3: Add the WARN lines of logs/app.log to the end of notes.txt. What was in notes.txt must stay.

Add the WARN lines of logs/app.log to the end of notes.txt. What was in notes.txt must stay.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Terminal task 3/3

Run ls logs nope and collect both the list and the error message in the file all.txt. Nothing should remain on the screen.

Task 3/3: Run ls logs nope and collect both the list and the error message in the file all.txt. Nothing should remain on the screen.

Run ls logs nope and collect both the list and the error message in the file all.txt. Nothing should remain on the screen.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

ls logs nope 2>&1 > /dev/null

Before the command, in your home folder ~

  • ~/logs/
  • ~/logs/app.log
  • ~/logs/app.log.1
What does ls logs nope 2>&1 > /dev/null print?

Check works once you have chosen an answer.

Find the error

This line is supposed to put the list and the error message in the file. The error still lands on the screen. Tap the part that is in the wrong place.

Parts of the command

Check works once you have tapped a part.

Fill the gap

Throw away the error messages of find: find /var -name '*.log' 2>

Throw away the error messages of find: find /var -name '*.log' 2>…

Check works once you have typed an answer.

Next up: seeing and saving at the same time, and turning lines into arguments.

Quiz

  1. Question 1 of 3Which channel carries error messages?

    Check works once you have chosen an answer.

  2. Question 2 of 3What does sort data.txt > data.txt leave behind?

    Check works once you have chosen an answer.

  3. Question 3 of 3set -o noclobber is on. What does echo x > existing.txt do?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. Card 3 of 3

    Tap the card or press Space to turn it over.

End of the lesson

Still open: 6 exercises, 3 quiz questions

Next: lesson 15, Branching off and passing on (9 min)

What you can do now: grep ERROR logs/app.log | wc -l, grep ERROR logs/app.log > errors.txt, ls logs nope > all.txt 2>&1

+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: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: done
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 15, Branching off and passing on (9 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.