terminal.cab

Linux Basics Lesson 29 of 38

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

Packing and mirroring

10 minutes

Learning goals

tar: many files in one

tar packs a folder into one file, the archive. -c creates, -z compresses with gzip, and -f names the file:

tar -czf shop.tar.gz projects/shop

Purpose: Packs files and folders into one archive, and gets them back out.

Syntax: tar -czf ARCHIVE FILE... | tar -tzf ARCHIVE | tar -xzf ARCHIVE [-C DIR]

Options:

  • -c, -t, -x create, list, extract
  • -z, -f with gzip, and the name of the archive follows
  • -C change to this folder first

Example:

tar -czf shop.tar.gz projects/shop
ls -lh shop.tar.gz
-rw-r--r-- 1 lerner lerner 423 Oct 17 09:42 shop.tar.gz

in the glossary: tar

To get something out, swap the letter: -t lists, -x extracts. -C picks the folder that it goes into, and the folder has to exist already:

tar -czf shop.tar.gz projects/shop
mkdir restore
tar -xzf shop.tar.gz -C restore
ls restore/projects/shop
README.md  app.conf  build.log  releases  test.log

Without -C the files land in the current folder and overwrite whatever has the same name there.

gzip and zcat: one file, smaller

For a single file, tar is more than you need. gzip compresses it in place. The result is notes.txt.gz, and the original is gone, unless you keep it with -k. zcat shows the contents without unpacking anything:

gzip -k notes.txt
ls -l notes.txt notes.txt.gz
-rw-r--r-- 1 lerner lerner 76 Oct 17 08:15 notes.txt
-rw-r--r-- 1 lerner lerner 97 Oct 17 08:15 notes.txt.gz
zcat notes.txt.gz
Renew the certificate
Ask about the backup window
Clean up the old releases

gunzip turns the file back again.

rsync: keeping a copy current

cp -r copies everything each time. rsync looks at what is already there and only transfers the differences. -a keeps rights and times, -v names each file:

rsync -av projects/shop/ mirror/

Purpose: Makes a copy match its source, and only transfers what differs.

Syntax: rsync -av [-n] [--delete] SOURCE/ TARGET/

Options:

  • -a, -v keep rights and times, and name each file
  • -n dry run, only show what would happen
  • --delete also delete in the target what is not in the source

Example:

rsync -av projects/shop/ mirror/
sending incremental file list
created directory mirror
./
README.md
app.conf
build.log
test.log
releases/
releases/v2.4.0/
releases/v2.4.0/VERSION
releases/v2.4.1/
releases/v2.4.1/VERSION

sent 550 bytes  received 253 bytes  1606.00 bytes/sec
total size is 90  speedup is 0.11

in the glossary: rsync

Mind the slash at the end of the source. projects/shop/ means "the contents of shop", and they land directly in mirror/. Without the slash, projects/shop means "the folder shop itself", and you get mirror/shop/. It is the most common rsync mistake, and it is only one character.

rsync is often not preinstalled, on Debian (apt install rsync) as well as on RHEL and Rocky Linux (dnf install rsync). This simulator has it, for local folders only.

Practice

Terminal task 1/2

Pack the folder projects/shop into a compressed archive called shop.tar.gz in your home folder. Then make sure of what is in it.

Task 1/2: Pack the folder projects/shop into a compressed archive called shop.tar.gz in your home folder. Then make sure of what is in it.

Pack the folder projects/shop into a compressed archive called shop.tar.gz in your home folder. Then make sure of what is in it.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

Fill the gap

You want to see what is in backup.tar.gz without unpacking it: tar -f backup.tar.gz

You want to see what is in backup.tar.gz without unpacking it: tar -…f backup.tar.gz

Check works once you have typed an answer.

Terminal task 2/2

Mirror the contents of projects/shop to a folder mirror in your home folder, so that app.conf ends up directly in mirror.

Task 2/2: Mirror the contents of projects/shop to a folder mirror in your home folder, so that app.conf ends up directly in mirror.

Mirror the contents of projects/shop to a folder mirror in your home folder, so that app.conf ends up directly in mirror.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

tar -czf empty.tar.gz

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
What does tar -czf empty.tar.gz print?

Check works once you have chosen an answer.

Next up: the disk is full, and someone also needs yesterday's configuration back.

Quiz

  1. Question 1 of 3You receive an archive from someone. What do you do first?

    Check works once you have chosen an answer.

  2. Question 2 of 3What is left after gzip notes.txt?

    Check works once you have chosen an answer.

  3. Question 3 of 3rsync -av shop mirror/ puts app.conf where?

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

Next: lesson 30, Disk full, but of what? (10 min)

What you can do now: tar -czf shop.tar.gz projects/shop, rsync -av projects/shop/ mirror/

+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: 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: done
  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 30, Disk full, but of what? (10 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.