Linux Basics Lesson 29 of 38
Packing and mirroring
10 minutes
Learning goals
- Pack a folder with tar -czf, and look inside with tar -tzf before unpacking it.
- Unpack into a folder of your choice with tar -xzf and -C.
- Compress single files with gzip and read them with zcat.
- Mirror a folder with rsync -av, and test --delete with -n first.
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:
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.gzTo 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:
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.11Mind 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.
Task complete
Solution
tar -czf shop.tar.gz projects/shop
tar -tzf shop.tar.gz Why: The order is the point. The name of the archive comes first, right after -f, and the things to pack come after that. Swapping them is how people lose files.
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.
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.
Task complete
Solution
rsync -av projects/shop/ mirror/ Why: With the slash, the contents land in mirror. Without it, you would get mirror/shop/app.conf. Run the same line again, and rsync transfers nothing, because nothing differs.
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
tar -czf empty.tar.gzBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
Check works once you have chosen an answer.
Next up: the disk is full, and someone also needs yesterday's configuration back.
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: 4 exercises, 3 quiz questions
What you can do now: tar -czf shop.tar.gz projects/shop, rsync -av projects/shop/ mirror/
+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: 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: done
- 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
-
tar -czf <archive> <folder>pack, overwrites without asking -
tar -tzf <archive>look inside first -
tar -xzf <archive> -C <folder>unpack into an existing folder -
zcat <file.gz>read without unpacking -
rsync -avn --delete <src>/ <dst>/dry run of a mirror
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, 47 KB)