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.gzip
is 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
gzip
by simply passing it as an argument.
This creates agzip filename
filename.gz
file, a compressed version offilename
. -
Decompress a file with
gzip
either withgzip -d
orgunzip
gzip -d filename.gz gunzip filename.gz
-
View
gzip
compressed files as a stream withzcat
andzless
.
These act the same way aszcat filename.gz zless filename.gz
cat
andless
.
.bz2
(bzip2
)¶
Bzip2 is a compression utility. It's slower than gzip
but has a better compression ratio.
Using bzip2
¶
-
Compress a file with
bzip2
by passing it as an argument.
This creates abzip2 filename
filename.bz2
file. -
Decompress a
.bz2
file with eitherbzip2 -d
orbunzip2
.
bzip2 -d filename.bz2 bunzip2 filename.bz2
.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 axz filename
filename.xz
file. -
Decompress with either
xz -d
orunxz
.
xz -d filename.xz unxz filename.xz
.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
zstd
:
This create azstd filename
filename.zst
file. -
Decompress a file with
zstd -d
orunzstd
.
zstd -d filename.zst unzstd filename.zst
.lzma
and .lz
¶
These are legacy formats that were replaced by .xz
.
But, if you have to work with them:
lzma filename # Compress
unlzma filename.lzma # Decompress
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
.tar
archive withtar -c
and specify an output file with-f
.
This bundlestar -cf archive.tar file1 file2 dir1/
file1
,file2
, anddir/
(and all its contents) into thearchive.tar
archive.- By default, archives are not compressed.
- You can use the
-z
option to automatically usegzip
to compress your archive. - You can use the
-J
option to automatically usexz
to compress your archive.
-
Extract an archive with
tar -x
and specify an input file with-f
.
tar -xf archive.tar
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
zip
:This bundleszip archive.zip file1 file2 dir/
file1
,file2
, anddir/
intoarchive.zip
. -
Extract an archive with
unzip
.
unzip archive.zip
.rar
¶
This is a proprietary archive format.
-
You can not create a
.rar
file withoutrar
, which is not free. -
Extracting a
.rar
requires theunrar
package.
unrar x archive.rar
.7z
(7zip)¶
The .7z
format has a high compression ratio and requires the p7zip
or 7zip
package to use.
-
Create a
.7z
archive with7z a
.This creates7z a archive.7z file1 dir/
archive.7z
with the files given. -
Extract a 7zip archive with
7z x
.
7z x archive.7z