Linux: Working With Archives
đ Wiki page | đ Last updated: Apr 5, 2022Working with archives in the shell is simple, you just need to understand two concepts: compression and archiving (and how they fit together).
First, let's take a look at the compression part. gzip
, bzip2
and xz
are the most common compression formats in the Linux world.
gzip, bzip2 & xz
Compressing a file:
gzip myfile
bzip2 myfile
xz myfile
Uncompressing a file:
gunzip myfile.gz
bunzip2 myfile.bz2
unxz myfile.xz
If (for some weird reason) you don't already have any of those tools installed, you can install them with something like apt install gzip bzip2 xz-utils tar
.
This works great for a single file, but it's not easy to create archives of multiple files/directories this way, so we need tar
.
tar
Making a tar archive of multiple files/directories is easy:
tar cvf archive.tar path1 path2
Now that we have a single file, we can easily compress it:
gzip archive.tar
bzip2 archive.tar
xz archive.tar
We could pipe the result of tar command to gzip/bzip2/xz, but that's still a bit cumbersome, so we can tell tar command directly to combine those two steps for us:
tar czvf archive.tar.gz path1 path2
tar cjvf archive.tar.bz2 path1 path2
tar cJvf archive.tar.xz path1 path2
(just remember that z
switch stands for gzip, j
for bzip2, and J
for xz compression)
Extracting archives:
tar xvf archive.tar
tar xzvf archive.tar.gz
tar xjvf archive.tar.bz2
tar xJvf archive.tar.xz
- with GNU tar, you can omit the switch for the compression type and it will be autodetected from the extension
- if you want to extract the archive into a different directory, just add
-C /destination_dir
.tgz
,.tbz2
and.txz
extensions are just a (less common) shorthands for .tar.gz, .tar.bz2 and .tar.xz
In terms of efficiency, xz generally has the highest compression rate, and it's faster than bzip2 at decompressing (at the expense of the compression time). gzip has the lowest compression rate, but it's fast.
Other formats
These formats are more common on other platforms than Linux, but working them from CLI is generally as easy as with tar archives.
zip
Creating archives:
zip archive.zip path1 path2
Extracting archives:
unzip archive.zip
- add
-d /destination_dir
to specify the target directory
Note:
- in case you don't have them already installed, you can install the required packages with something like
apt install zip unzip
rar
Creating archives:
rar a archive.rar path1 path2
Extracting archives:
unrar e archive.rar
To extract the archive to a specific directory, just add it at the end:
unrar e archive.rar /destination_dir
Note:
- in case you don't have them already installed, you can install the required packages with something like
apt install rar unrar
7z
Creating archives:
7z a archive.7z path1 path2
Extracting archives:
7z e archive.7z
- add
-o /destination_dir
to specify the target directory
Note:
- in case you don't have them already installed, you can install the required packages with something like
apt install p7zip
Ask me anything / Suggestions
If you find this site useful in any way, please consider supporting it.