bash 如何将进度条添加到 somearchive.tar.xz 提取物中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19372373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:18:21  来源:igfitidea点击:

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

bashtar

提问by GlassGhost

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

我想至少打印 # 提取的文件,通过运行 tarball 提取

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

我在考虑使用作为中提到的“\ r”的这个问题,例如

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

my bash skills fail me.

我的 bash 技能让我失望。

采纳答案by Kevin

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 xzseparately, most tarimplementations will automatically detect and decompress it for you.
  • tarreads from stdin by default, you don't need f -.
  • 您可能不需要xz单独进行,大多数tar实现会自动为您检测和解压缩它。
  • tar默认情况下从 stdin 读取,您不需要f -.

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

您应该考虑使用pv它,它更精确且更普遍适用:

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

回答by sergej

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

提取时tar可以使用该--checkpoint选项。你得到的并不是真正的进度条,而是足以看到进度。

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

在这个例子中,tar每写入 100 条记录就会打印一条消息。如果您在等号后立即放置一个点,它只会打印一个..

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

Output:

输出:

.......

回答by Zen42

You can do this with xz files like this:

您可以使用 xz 文件执行此操作,如下所示:

unxz -v --stdout file.tar.xz | tar -x

回答by royarisse

While the pvsolution shows progress nicely, it doesn't show what files are extracted whereto. The --checkpoint=.100option shows the files and indicates that it's working, but doesn't show progress.

虽然该pv解决方案很好地显示了进度,但它没有显示哪些文件被提取到了哪里。该--checkpoint=.100选项显示文件并表明它正在运行,但不显示进度。

On the Tar Checkpointspage I found some information about the --checkpoint-actionoption, which is able to execute some bash magic for every checkpoint. Knowing that a checkpoint is created every 20 blocks of 512 bytes (at least that's the default, see man tar), It's possible to get the progress based on the current block and the original size.

Tar Checkpoints页面上,我找到了有关该--checkpoint-action选项的一些信息,该选项能够为每个检查点执行一些 bash 魔法。知道每 20 个 512 字节的块创建一个检查点(至少这是默认值,请参阅 参考资料man tar),可以根据当前块和原始大小获取进度。

The snippet below can be saved as a bash file (or put into a bash function):

下面的代码段可以保存为 bash 文件(或放入 bash 函数):

archive=""

originalsize=$(file $archive | rev | cut -d' ' -f1 | rev)
step=100
blocks=$(echo "$originalsize / 512 / 20 / $step" | bc)

tar -xvz --checkpoint=$step --totals \
 --checkpoint-action="exec='p=$(echo "$TAR_CHECKPOINT/$blocks" | bc -l);printf \"%.4f%%\r\" $p'" \
 -f $archive

Then using it is very straightforward:

然后使用它非常简单:

bash tarprogress.sh your.archive.tgz