How to add progress bar to a somearchive.tar.xz extract

If you really want to do it by file you could use:

tar xvpf /path/to/archive.tar.xz -C /path/to/dir 2>&1 | 
while read line; do
    x=$((x+1))
    echo -en "$x extracted\r"
done

Notes:

  • You probably don't need to xz separately, most tar implementations will automatically detect and decompress it for you.
  • tar reads from stdin by default, you don't need f -.

You should look into using pv instead, it's more precise and more generally applicable:

pv /path/to/archive.tar.xz | tar xp -C /path/to/dir

Using pv to pipe the file to tar.

  1. Firstly, you'll need to install pv, which on macOS can be done with:

    brew install pv
    

    On Debian or Ubuntu, it can be done with: apt install pv (Thanks @hyperbola!).

  2. Pipe the compressed file with pv to the tar command:

    pv mysql.tar.gz | tar -xz   
    

Here's the sample output of this command:

Sample output

For those curious, this works by pv knowing the total file size of the file you pass it and how much of it has been "piped" to the tar command. It uses those two things to determine the current progress, the average speed, and the estimated completion time. Neat!


When extracting with tar you can use the --checkpoint option. What you get is not really a progress bar but good enough to see the progress.

In this example, tar will print a message each 100 records written. If you place a dot immediately after the equal sign, it will just print a ..

tar -xf somearchive.tar.gz --checkpoint=.100

Output:

.......

Tags:

Bash

Tar