Working with Archives¶
A lot of software packages will typically come in an archive file.
Archives are either compressed files (single file compression), archive containers
(e.g., .tar files), or both (.tar.gz, .tar.bz2, etc.).
More often than not, you'll see a compressed archive container (.tar.gz).
Creating Archives and Unarchiving¶
tl;dr¶
# Create archives
tar -czf foo.tar.gz dir/ # gzip
tar -cjf foo.tar.bz2 dir/ # bzip2
tar -cJf foo.tar.xz dir/ # xz
tar --zstd -cf foo.tar.zst dir/ # zstd
zip -r foo.zip dir/ # zip
7z a foo.7z dir/ # 7zip
# Extract
tar -xzf foo.tar.gz
tar -xjf foo.tar.bz2
tar -xJf foo.tar.xz
tar --zstd -xf foo.tar.zst
unzip foo.zip
7z x foo.7z
Unextracting with tar on modern Linux systems:
# This works for files compressed with gzip and bzip
tar --extract --file filename
# Same as above
tar -xf filename
# Same as above. The leading dash is optional
tar xf filename
# explicitly decompress gzip2-compressed file then
# pass uncompressed result directly into tar
gzip -cd filename.tgz | tar xf -
# same as above, but for bzip2-compressed files
bunzip2 -cd filename.tar.bz2 | tar xf -
The uncompression commands usually have options in tar. But you can decompress and
pipe to tar if you don't want to remember all the options.
Overview of Formats¶
There are a few different formats you'll see when working with archives on Linux/Unix.
.gz: Gzip file.gzipis a compression utility, not an archiving utility.- Compression is often used in conjunction with archives to make the file smaller.
.tar: Tar archive file..tar.gz: Tar archive file compressed withgzip.- Sometimes represented as
.tgz
- Sometimes represented as
Compression Formats¶
.gz (gzip)¶
Gzip is a fast compression utility, but it doesn't have a great compression ratio.
It's usually good for most things and is widely used.
Using gzip¶
-
Compress a file with
This creates agzipby simply passing it as an argument.
filename.gzfile, a compressed version offilename. -
Decompress a file with
gzipeither withgzip -dorgunzip -
View
These act the same way asgzipcompressed files as a stream withzcatandzless.
catandless.
.bz2 (bzip2)¶
Bzip2 is a compression utility. It's slower than gzip but has a better compression ratio.
Using bzip2¶
-
Compress a file with
This creates abzip2by passing it as an argument.
filename.bz2file. -
Decompress a
.bz2file with eitherbzip2 -dorbunzip2.
.xz (xz)¶
XZ is a compression utility that has a high compression ratio, but it's even slower
than bzip2.
Using xz¶
-
Compress in the same way as the other tools.
Creates a
filename.xzfile. -
Decompress with either
xz -dorunxz.
.zst (Zstandard/zstd)¶
Zstandard (zstd) is a compression utility that is both fast and has a good
compression ratio.
Drawback: It's backed by Facebook.
Using zstd¶
-
Compress a file with
This create azstd:
filename.zstfile. -
Decompress a file with
zstd -dorunzstd.
.lzma and .lz¶
These are legacy formats that were replaced by .xz.
But, if you have to work with them:
Archive Container Formats¶
.tar (Tape Archive)¶
The tar command creates archive files, which bundles multiple files into a single
file. There is no compression used by default.
Using tar¶
-
Create a
This bundles.tararchive withtar -cand specify an output file with-f.
file1,file2, anddir/(and all its contents) into thearchive.tararchive.- By default, archives are not compressed.
- You can use the
-zoption to automatically usegzipto compress your archive. - You can use the
-Joption to automatically usexzto compress your archive.
-
Extract an archive with
tar -xand specify an input file with-f.
tar -czf archive.tar.gz folder/
# -c: create archive
# -z: use gzip
# -f: filename to create (archive.tar.gz)
tar -xJf archive.tar.xz
# -x: extract
# -J: use xz
# -f: archive file name
.zip¶
The .zip format is a cross-platform archive format.
Supports compression and multi-file archiving.
-
Create an archive with
This bundleszip:file1,file2, anddir/intoarchive.zip. -
Extract an archive with
unzip.
.rar¶
This is a proprietary archive format.
-
You can not create a
.rarfile withoutrar, which is not free. -
Extracting a
.rarrequires theunrarpackage.
.7z (7zip)¶
The .7z format has a high compression ratio and requires the p7zip or 7zip
package to use.
-
Create a
This creates.7zarchive with7z a.archive.7zwith the files given. -
Extract a 7zip archive with
7z x.