1. Overview

Compressing and uncompressing files and folders, along with inspecting and storing them, are common operations that we often need to perform through the command line. This is especially true when working on a server that has no GUI.

In this hands-on tutorial, we’ll learn the different ways to store, compress, inspect, and extract files and folders from the CLI (Command Line Interface) in a Unix environment.

2. Exploring the Main Archive Formats

Before jumping to the commands, a quick overview of the most popular archive formats might be useful.

2.1. Tar – the First Archiving Tool

Tar (Tape Archive) is a utility born more than forty years ago to efficiently write multiple files on tape. It simply stores several files within the same file, without any compression. The resultant archive file, easier to use, store and transfer, is called a tarball.

Without digging too deep into history (where we’d find compress, for example), let’s simply start from Zip, which has been the most famous compression software for decades.

Created with the specific goal of efficiently saving disk space, its most common compression scheme, deflate, is a commercial implementation of the LZ77 algorithm. Files compressed with Zip end with the .zip extension, as in archive.zip.

Due to Zip’s proprietary nature, GZip was created by the Unix community as an alternative for using deflate in free software.

GZip only compresses files, though, without archiving them; in order to compress multiple files, we need to first create a tarball, obtaining an archive.tar, then to compress the tarball with GZip, ending with an archive.tar.gz.

2.3. 7-Zip – the Modern and Efficient Tool

7-Zip is a popular tool that gained great momentum in the latest years, thanks to being very efficient, cross-platform, almost completely open-source (except for the part dealing with the proprietary format RAR). It can deal with many different archive formats other than its own, 7z, which is based on the LZMA and LZMA2 algorithm and creates files with the .7z extension, as in archive.7z.

2.4. BZip2, Lzip, XZ, and Others

During the years, many compression tools and archive formats have emerged.

Each one is based on a different compression algorithm and has its own features, and comparing them is out of the scope of this tutorial.

Their usage is a trade-off between the consumption of CPU, disk space, or time, and must be chosen upon our specific needs.

For now, let’s just know that Tar allows us to choose among them to compress a tarball and that, as we’ve seen in GZip, BZip2 files end in .tar.bz2, Lzip files end in .tar.lz, XZ files end in .tar.xz, and so on.

3. Storing Files and Folders

Let’s now see how we can store files into an archive, without any compression, but with the maximum speed:

3.1. Creating a Tarball

We can create a tarball including specific files and folders with the syntax:

tar cf archive.tar file1 dir1 file2 dir2

The directory traversing is recursive, which means that subfolders, along with their files, will be included by default.

In case we want to archive all the files and folders present in the current directory, we can simply do:

tar cvf archive.tar *

The option v verbosely prints out the names of the elements while processing them.

It’s worth knowing that, while historically lacking any symbol, Tar options can also be expressed with a starting hyphen:

tar -cvf archive.tar *

This is useful (and mandatory) if we want to put the options in a position not adjacent to the tar command.

3.2. Creating an Uncompressed Zip Archive

Although less common, we might want to store an uncompressed Zip, and we can do it by specifying the -0 compression ratio flag:

zip -0 archive.zip file1 dir1 file2 dir2

Differently from Tar, Zip will not traverse directories by default; we must use the -r flag for this:

zip -0 -r archive.zip *

It will be verbose by default, and we can use the -q flag in case we want to turn it silent.

3.3. Creating an Uncompressed Archive with 7-Zip

7-Zip allows us to create archives in almost each one of the formats we’ve seen previously, as we can see in the documentation.

If the compression format we’re using supports the storage-only option, we can create an uncompressed archive using 7-Zip as follows:

7z a -mx=0 archive.7z file1 dir1 file2 dir2

Or simply:

7z a -mx=0 archive.7z *

7-Zip archives include subfolders by default.

4. Compressing Files and Folders

Let’s now see how to effectively compress files and folders, using the default compression settings.

4.1. Compressing a Tarball

We can easily compress a tarball with GZip, the default compression method, by simply adding the z flag to the archive creation command, and using the proper archive extension:

tar czvf archive.tar.gz *

An uppercase Z will instead turn to the very old, LZW-based compress software:

tar cZvf archive.tar.gz *

Other formats might be chosen with their specific flags:

tar cvf --xz archive.tar.xz *
tar cvf --lzip archive.tar.lz *
tar cvf --lzma archive.tar.xz *

Finally, the a flag allows Tar to detect the compression mechanism from the file extension:

tar cavf archive.tar.xz *

4.2. Creating a Compressed Zip Archive

Creating a Zip from the CLI is straight-forward:

zip archive.zip *

As seen previously, subfolders can be included with:

zip -r archive.zip *

4.3. Creating a Compressed Archive with 7-Zip

7-Zip’s syntax for creating a compressed archive is also easy to remember:

7z a archive.7z *

5. Inspecting an Archive

5.1. Inspecting a Tarball

In case we want to inspect the content of a tarball, the syntax is:

tar tf archive.tar

This will also work with compressed archive:

tar tf archive.tar.gz

5.2. Inspecting a Zip with UnZip

In case of a Zip, instead, we can do it through UnZip:

unzip -v archive.zip 

5.3. Inspecting an Archive with 7-Zip

Finally, with 7-Zip the syntax for listing the content of an archive is:

7z l archive.7z 

6. Extracting Files and Folders

Let’s now explore the different commands to extract an archive.

6.1. Uncompressing a Tarball

No matter if the tarball is compressed or not, we can extract files and folders as follows:

tar xvf archive.tar
tar xvf archive.tar.gz
tar xvf archive.tar.xz

In case we want to extract the files in a different directory from the current one, we can exploit the C flag:

tar xvf archive.tar -C targetPath

6.2. Uncompressing a Zip Archive

With UnZip, extracting files and folders from a Zip archive is a very easy operation:

unzip archive.zip

We can even omit the extension, like in:

unzip archive

Similarly to tar, we can target a different directory with the d flag :

unzip archive -d targetPath

6.3. Uncompressing an Archive with 7-Zip

The correct syntax to extract an archive while preserving the original directory structure is:

7z x archive.7z

In case we want a flat extraction where every file and directory will be placed in the root of the extraction folder, we can use the e flag:

7z e archive.7z

Likewise, 7-Zip needs the o flag to specify a target path (with no spaces between):

7z x archive.7z -otargetPath

7. JAR: an Alternative for Edge Cases

In addition to the cases seen before, we might end up on a machine with Java installed and no other archiving software available.

If that’s the case, we should remember that is also possible to extract an archive with JAR (Java Archive), as per this StackExchange Q&A:

jar -xf archive.zip

8. Conclusion

In this tutorial, we’ve explored the most popular archiving software, along with the common commands to store, compress, inspect, and extract files and folders to and from archives.

Many options are available, and carefully described in their Man pages, properly linked at the beginning of the tutorial.