terminal.cab

Linux Basics Lesson 15 of 38

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

Branching off and passing on

9 minutes

Learning goals

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:

  • -a append instead of overwriting

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:45

in the glossary: ls

A 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:

  • -n 2 at most two arguments per call
  • -0 names end with a zero byte, the partner of find -print0

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

in the glossary: find

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

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

echo a b c | xargs -n 2 echo

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
  • ~/cache/
What does echo a b c | xargs -n 2 echo print?

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

  1. Question 1 of 3What does tee do without -a if the file already exists?

    Check works once you have chosen an answer.

  2. Question 2 of 3Why does find . | rm not delete anything?

    Check works once you have chosen an answer.

  3. Question 3 of 3Why does sudo echo x > /etc/app.conf fail, while echo x | sudo tee /etc/app.conf works?

    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: 5 exercises, 3 quiz questions

Next: lesson 16, The 17:45 report (9 min)

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

  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: open
  15. Lesson 15: done
  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 16, The 17:45 report (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.