🎓 A1Learner
🔍
0 of 50 lessons completed

Archives

An archive is one file that holds many files. Compression shrinks the archive, so it takes less room.

Pack a folder

The new archive holds the notes folder. The size here comes from this machine, so yours will differ.

$ cd ~/practice
$ tar -czf notes-backup.tar.gz notes
$ ls -l notes-backup.tar.gz

List the archive

The list shows the folder and the two files inside it.

$ cd ~/practice
$ tar -tzf notes-backup.tar.gz

Shrink one file

gzip adds a smaller copy with .gz at the end. gunzip brings the first file back.

$ cd ~/practice
$ gzip -k data/logs.txt
$ ls -l data/logs.txt*
$ gunzip -f data/logs.txt.gz
$ ls data/logs.txt*

Example

$ cd ~/practice
$ tar -czf notes-backup.tar.gz notes
$ tar -tzf notes-backup.tar.gz
$ mkdir -p restore
$ tar -xzf notes-backup.tar.gz -C restore
$ ls restore/notes

Key points

  • Use tar -czf to pack a folder into an archive.
  • A tarball is an archive made by tar.
  • Use tar -tzf to list what an archive holds.
  • Use tar -xzf to unpack an archive.
  • gzip shrinks one file.
  • gunzip restores the file it shrank.
  • The .gz ending marks a file that gzip shrank.

In this example, tar packs the notes folder into one archive. The list shows the folder and its two files. The archive then unpacks into restore, and the two files sit there.

Note

tar prints nothing when it packs, so ls shows the new archive.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does tar -tzf do?