Archiving and compression solve different problems. An archive combines a directory tree and its metadata into one stream. Compression encodes a stream to reduce its size. A .tar.gz file is conventionally a tar archive whose stream has been compressed with gzip.
Packages · Lesson 3
tar and gzip
Learn how to archive files with `tar`, compress streams with `gzip`, and inspect archives before safe extraction.
Compressing One Stream with `gzip`
By default, gzip compresses a file and replaces the original name with a .gz file:
$ gzip report.txt
This normally removes report.txt after successfully creating report.txt.gz. Decompress it with:
$ gunzip report.txt.gz
Use gzip -k report.txt where supported to keep the input file, or use standard streams when you need explicit control. A filename extension is a convention, not proof of the actual format; tools such as file can inspect content.
What is the primary role of gzip in this lesson?
Creating a Tar Archive
Create an uncompressed archive with:
$ tar -cvf project.tar file1 file2 directory1
-ccreates a new archive.-vlists members while processing and is optional.-f project.tarnames the archive file; because-fconsumes an argument, keep the filename beside it.
Paths are stored as archive member names. Create archives from a deliberate working directory and avoid unintentionally capturing secrets, caches, sockets, or broad absolute paths.
Which tar option creates a new archive?
Creating a Gzip-Compressed Tar Archive
GNU tar and many other implementations can invoke gzip with -z:
$ tar -czvf project.tar.gz file1 file2 directory1
The result is one gzip-compressed tar stream. Compression does not encrypt the archive or hide its contents from someone who can read and decompress it. If confidentiality is required, use an appropriate authenticated-encryption workflow and manage keys separately.
What does -z request in the shown tar command?
Listing Before Extracting
Treat an archive from another party as untrusted input. List its member names first:
$ tar -tzf download.tar.gz
Look for unexpected absolute paths, .. traversal components, surprising symbolic or hard links, device files, and names that would overwrite important files. Modern tar implementations apply protections, but behavior and options vary, and extracting still creates attacker-chosen names and content.
Extract into a newly created, nonprivileged staging directory:
$ mkdir extraction-stage
$ tar -xzf download.tar.gz -C extraction-stage
Do not extract an unreviewed archive as root. Verify what was created before moving selected files to their final locations.
Which operation lists archive members without extracting them?
Other Compression Formats
Tar implementations can work with compressors such as bzip2 and xz, commonly selected with -j and -J respectively in GNU tar. Format support and automatic detection differ, so consult tar --help or the local manual. ZIP is a separate archive format operated with tools such as zip and unzip.
Does gzip compression make a tar archive confidential?
Practice with disposable files in File Packaging and Compression, then apply inspection and staging in Create and Restore a Backup with tar.
Lesson complete
You finished tar and gzip
You can now combine tar archiving with gzip compression safely.
Distinguish a tar archive from gzip compression.
Create archives with
-cand gzip streams with-z.List members with
-tbefore extracting with-x.Extract untrusted content into a nonprivileged staging directory.
Treat compression as separate from encryption.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account