In Linux, the tar ("tape archive") command allows for the creation and extraction of archive files. Originally created to store file systems on to magnetic tape data storage, tar is commonly used to combine multiple files in to a single one for storage or distribution, and is often paired with a compression algorithm to do so efficiently.

In this post, we'll take a look at how to use tar with the Linux tools pv (a "pipe" viewer") and dialog (a scripting tool for the creation of widgets) to build a TUI progress bar.

We're using Debian 9 as our environment of choice, but what follows should work just as well in Ubuntu and other downstream distributions. That said, let's take care of a few preliminaries.

First, install the required packages:
sudo apt-get --yes install dialog pv.

Next, navigate to the current user's Desktop directory and create a file for tar to work with:
cd /home/*/Desktop && fallocate --length 2GB rando.img.

Then create the archive:
sudo tar --create --verbose --gzip --file=/usr/local/rando.tgz rando.img.

We now have a 2 GB gzip tar archive named "rando.img" sitting in /usr/local.

Let's extract this archive using tar, pv, and dialog:

untar_blob () {
    (pv --numeric /usr/local/rando.tgz | 
    tar --extract --gzip --directory=/usr/local) 2>&1 |
    dialog --backtitle "$script" --title "$program" --gauge "OPENING TAR ARCHIVE IN /usr/local..." 10 40
}

main () { 
  untar_blob
}

main "$@"

which results in:

tar_bar

To understand how the untar_blob function works, we need to be familiar with concept of I\O redirection, which this site does an excellent job of explaining. Briefly, I/O redirection allows us to send the output of one command to another place.

While we generally interact with files by having their output sent to our screen (standard output AKA stdout and denoted by the file descriptor 1) we may have reason to send output elsewhere. We can do this via redirection >, >>, <, but also via a pipeline |. Error messages (standard error AKA stderr and denoted by the file descriptor 2) are likewise sent to our screen, unless directed elsewhere.

With the above mentioned in mind, our function should now be a bit more readable. What we've done is extracted our archive to /usr/local using tar; utilized pv to give us an indication of the extraction process (via integer percentage) by sending the feedback from standard error to standard output; and then piped that output to dialog. The result is a suitable means for gauging how far a tar extraction has to go until completion.

FYI: the code used in this post is available here.

Cheers.