Linux Basics Lesson 15 of 38
Branching off and passing on
9 minutes
Learning goals
- See output and save it at the same time with tee and tee -a.
- Turn lines from a pipe into arguments with xargs and xargs -n.
- Delete files with spaces in their names safely with find -print0 and xargs -0.
Seeing and saving: tee
tee FILE copies its input to the file and on to its output. -a appends instead of overwriting. Behind it the pipe can continue:
ls cache | tee inventory.txt
Purpose: The T-piece in the pipe. The data flows on and lands in a file as well.
Syntax: COMMAND | tee [-a] FILE
Options:
Example:
ls cache | tee inventory.txt
a.tmp
b.tmp
keep.log
old report.tmp
echo "checked 09:45" | tee -a inventory.txt
checked 09:45
cat inventory.txt
a.tmp
b.tmp
keep.log
old report.tmp
checked 09:45A famous side job is echo 'text' | sudo tee /etc/some.conf. sudo command > file fails on files that belong to root, because your own shell
opens the file. With sudo tee, a process with root rights does the writing.
Lines become arguments: xargs
A pipe feeds the input of a command, but many commands want arguments. rm reads no list of files from stdin. xargs translates: it reads the
pipe and hangs what it finds behind the command. -n sets how many at a time.
echo a b c | xargs echo
a b c
echo a b c | xargs -n 2 echo
a b
c
Try it first, with echo
xargs can do a lot of damage quickly. So put echo in front first, and you see the commands it would run. Then you take echo away.
Look what happens with a name that has a space in it, though:
find cache -name "*.tmp" | xargs echo rm
rm cache/a.tmp cache/b.tmp cache/old report.tmp
find cache -name "*.tmp" | xargs -n 1 echo rm
rm cache/a.tmp
rm cache/b.tmp
rm cache/old
rm report.tmp
old report.tmp became two arguments, cache/old and report.tmp. A real rm would complain about both and leave the file where it is. Or
worse, if a file called report.tmp happens to exist.
Names with spaces: -print0 and -0
The fix is for find to end each name with a zero byte, the one character that cannot occur in a file name, and for xargs to split at exactly
that:
find cache -name "*.tmp" -print0 | xargs -0 -n 1 echo rm
Purpose: Turns lines from a pipe into arguments of a command.
Syntax: ... | xargs [-0] [-n COUNT] COMMAND
Options:
Example:
find cache -name "*.tmp" -print0 | xargs -0 -n 1 echo rm
rm cache/a.tmp
rm cache/b.tmp
rm cache/old report.tmp
find cache -name "*.tmp" -print0 | xargs -0 rm
ls cache
keep.logFor a deletion, find cache -name "*.tmp" -delete would also do, without any pipe. But xargs works with every command, not just with rm.
Practice
Terminal task 1/3
Show the content of the folder cache and save the same list in inventory.txt, in one line.
Task 1/3: Show the content of the folder cache and save the same list in inventory.txt, in one line.
Show the content of the folder cache and save the same list in inventory.txt, in one line.
Task complete
Solution
ls cache | tee inventory.txt Why: With ls > file you would see nothing, with ls and then ls > file you would run it twice. tee gives you both from one run.
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 "checked" to the end of notes.txt with tee, and see it on the screen as well. What was in the file must stay.
Task 2/3: Add the line "checked" to the end of notes.txt with tee, and see it on the screen as well. What was in the file must stay.
Add the line "checked" to the end of notes.txt with tee, and see it on the screen as well. What was in the file must stay.
Task complete
Solution
echo checked | tee -a notes.txt Why: This is the tee form you will see most often on real servers, with sudo in front of tee, to add a line to a config that belongs to root.
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
Delete all .tmp files in the folder cache, including the one with a space in its name, with find and xargs. keep.log stays.
Task 3/3: Delete all .tmp files in the folder cache, including the one with a space in its name, with find and xargs. keep.log stays.
Delete all .tmp files in the folder cache, including the one with a space in its name, with find and xargs. keep.log stays.
Task complete
Solution
find cache -name "*.tmp" -print0 | xargs -0 rm Why: A zero byte is the one character that cannot occur in a file name, so it separates reliably. The check looks at the folder afterwards and not at the words you typed.
Another way
find cache -name "*.tmp" -delete Shorter, and without the pipe there is nothing to split. It only works for deleting, while -print0 | xargs -0 works with every command.
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
echo a b c | xargs -n 2 echoBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
- ~/cache/
Check works once you have chosen an answer.
Fill the gap
The partner of find -print0: find . -name '*.tmp' -print0 | xargs rm
The partner of find -print0: find . -name '*.tmp' -print0 | xargs … rm
Check works once you have typed an answer.
Next up: Quitting time, and the team lead wants a report.
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: 5 exercises, 3 quiz questions
What you can do now: ls cache | tee inventory.txt, find cache -name "*.tmp" -print0 | xargs -0 -n 1 echo rm
+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: open
- Lesson 15: done
- 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
-
... | tee <file>see and save -
... | tee -a <file>see and append -
... | xargs echo rmtry it out first -
find ... -print0 | xargs -0 rmsafe with spaces in names
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)