Linux Basics Lesson 14 of 38
Steering the streams
10 minutes
Learning goals
- Tell stdin, stdout and stderr apart and steer them with |, >, >>, < and 2>.
- Collect errors and output in one place with 2>&1 and explain why its position matters.
- Throw away what you do not need with /dev/null, and protect files with set -o noclobber.
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.logInto 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:
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
3wc -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:
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.12>&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.
Task complete
Solution
grep ERROR logs/app.log > errors.txt Why: The check looks at the file and not at what you typed. If the file has exactly these lines, it does not matter how they got there.
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 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.
Task complete
Solution
grep WARN logs/app.log >> notes.txt Why: One character decides between "add to it" and "throw it away". That is why noclobber exists, and why it is worth a second look before you press Enter.
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
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.
Task complete
Solution
ls logs nope > all.txt 2>&1 Why: &> is the short form in bash, but a plain sh does not know it. In scripts for other shells, > file 2>&1 is the safe form.
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.
Predict the output
Command
ls logs nope 2>&1 > /dev/nullBefore the command, in your home folder ~
- ~/logs/
- ~/logs/app.log
- ~/logs/app.log.1
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
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
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
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
- 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: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: done
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
A | Boutput of A into the input of B -
> file, >> fileoverwrite, append -
> file 2>&1output and errors into the file -
2>/dev/nullthrow away the errors -
set -o noclobber> no longer overwrites
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 17 KB)
- View as image (PNG, 41 KB)