A practical cheatsheet for managing archives with tar

In this blog, we will explore how to create and manage archives using tar. We'll start from the beginning and cover what archives are and provide context to the options of the utility used to manage them. 

What is an archive?

Archives are efficiently compressed files widely used on the internet to simplify the process of distributing multiple files across the wire. Archive files are used for storing or sending large log files and metadata. Software that you use every day most likely got to your computer in an archive file format.

Archive files often store directory structures, error detection and correction information, arbitrary comments, and sometimes use built-in encryption.

tar (computing). From Wikipedia 

An archive file can also be known as a "tar file", a name inspired by the popular command-line utility known as "tar". Tar is commonly found pre-installed in modern computers. The command below uses tar to extract the contents of backup.tar.gz into the directory "restore". You can run this command now or simply run `tar` to check that you have it installed.

Initial directory structure

We start inside of a directory, where some files exist. We will use the `ls` command to inspect its contents.

There are three files: file1, file2, and file3. They are text files, our job is creating a single archive file that can hold them in a "single place". We will use these file examples in the rest of the article.

It's time to make an archive and play with the `tar` command!

Create an archive

Archives can be made up of 1 or as many files as you want. Most of the time you'll find that archive files are compressed and end with an extension like `.tar.gz`, where gz stands for gzip, a popular file format used by compressed files. 

In this section, we will create two archives: uncompressed and compressed.  

No Compression

The `tar` program will pack `file1`, `file2`, and `file3` into a single file that we will call `archive.tar`.

Notice that one new file appears. We created a new archive file named archive.tar and listed after running the `ls`

Let's inspect the new file with the utility `file`

Our new file is of type POSIX tar archive

Using Compression

We can reduce the file size with compression. We tell `tar` to compress the archive with the option `-z`.

A new file is listed `compressed_archive.tar.gz`. Notice that we added `.gz` to the file extension only as a hint that the archive is compressed. The file extension doesn't bother the system, it's for humans.

We see a different output after inspecting the file type of `compressed_arhive.tar.gz`. This file has compressed data.

Let's get a detailed list of the contents of the directory to see what's changed.

There are five files. The details are possible thanks to the `-l` option.

Inspect an archive

Archives may contain many files so tar has an option that lets you see what's in an archive. With the `-t` option, you tell `tar` to list the contents of the archive.

Extract an archive

This action will make a copy of the files and directory structure and put it in any directory you want, so long you have to write permissions on it. This operation does not delete the files inside of the archive.

You will notice that `tar` now uses a new option `-x` for extract.

Extract all files to another directory

You can specify the location where these files should go. In the commands below we create a new directory named `targetDirectory` and tell `tar` to extract the files there.

Extract only one file to another directory

We finally list the contents of the new directory and verify that the files exist.

The `newDirectory` contains file1.