DEV Community

Nick | OneThingWell.dev
Nick | OneThingWell.dev

Posted on • Edited on • Originally published at onethingwell.dev

Linux: Working With Archives

Working 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
Enter fullscreen mode Exit fullscreen mode

Uncompressing a file:

gunzip myfile.gz
bunzip2 myfile.bz2
unxz myfile.xz
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now that we have a single file, we can easily compress it:

gzip archive.tar
bzip2 archive.tar
xz archive.tar
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

(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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Extracting archives:

unzip archive.zip
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Extracting archives:

unrar e archive.rar
Enter fullscreen mode Exit fullscreen mode

To extract the archive to a specific directory, just add it at the end:

unrar e archive.rar /destination_dir
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Extracting archives:

7z e archive.7z
Enter fullscreen mode Exit fullscreen mode
  • 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

Note: this is a snapshot of (WIP) topic from the Understanding Simplicity wiki. All suggestions (and reactions) are welcome. You can find the latest version here: Working With Archives

Top comments (0)