Search code examples
bashtar

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


I'd like to print at the very least print # files extracted, from running a tarball extract

xz -dc /path/to/somearchive.tar.xz | sudo tar xvpf - -C /path/to/some_directory

I was thinking of using the "\r" as mentioned in this question, for instance

num=0
when [\n received]
    num=$(($num + 1))
    echo -ne "$num files extracted \r"
end when

my bash skills fail me.


Solution

  • 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!