bash 使用cp复制目录时如何制作进度条?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7128575/
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-09 20:56:24  来源:igfitidea点击:

How can I make a progress bar while copying a directory with cp?

bashprogress-barcp

提问by octosquidopus

I suppose I could compare the number of files in the source directory to the number of files in the target directory as cp progresses, or perhaps do it with folder size instead? I tried to find examples, but all bash progress bars seem to be written for copying single files. I want to copy a bunch of files (or a directory, if the former is not possible).

我想我可以在 cp 进行时将源目录中的文件数与目标目录中的文件数进行比较,或者用文件夹大小来代替?我试图找到示例,但所有 bash 进度条似乎都是为复制单个文件而编写的。我想复制一堆文件(或一个目录,如果前者不可能)。

回答by SteveLambert

You can also use rsyncinstead of cplike this:

您也可以使用rsync代替cp这样的:

rsync -Pa source destination

rsync -Pa source destination

Which will give you a progress bar and estimated time of completion. Very handy.

这将为您提供进度条和预计完成时间。非常便利。

回答by Shervin Emami

To show a progress bar while doing a recursive copy of files & folders & subfolders (including links and file attributes), you can use gcp(easily installed in Ubuntu and Debian by running "sudo apt-get install gcp"):

要在递归复制文件、文件夹和子文件夹(包括链接和文件属性)时显示进度条,您可以使用gcp(通过运行“sudo apt-get install gcp”轻松安装在 Ubuntu 和 Debian 中):

gcp -rf SRC DEST

Here is the typical output while copying a large folder of files:

这是复制大型文件文件夹时的典型输出:

Copying 1.33 GiB  73% |#####################      | 230.19 M/s ETA:  00:00:07

Notice that it shows just one progress bar for the whole operation, whereas if you want a single progress bar per file, you can use rsync:

请注意,它仅显示整个操作的一个进度条,而如果您希望每个文件有一个进度条,则可以使用rsync

rsync -ah --progress SRC DEST

回答by Thomas Berger

You may have a look at the tool vcp. Thats a simple copy tool with two progress bars: One for the current file, and one for overall.

你可以看看这个工具vcp。这是一个带有两个进度条的简单复制工具:一个用于当前文件,一个用于整体。

EDIT

编辑

Here is the link to the sources: http://members.iinet.net.au/~lynx/vcp/Manpage can be found here: http://linux.die.net/man/1/vcp

这是源的链接:http: //members.iinet.net.au/~lynx/vcp/ 可以在这里找到手册页:http: //linux.die.net/man/1/vcp

Most distributions have a package for it.

大多数发行版都有一个包。

回答by Thomas Berger

Here another solution: Use the tool bar

这是另一个解决方案:使用该工具 bar

You could invoke it like this:

你可以这样调用它:

#!/bin/bash
filesize=$(du -sb  | awk '{ print  }')
tar -cf - -C  ./ | bar --size ${filesize} | tar -xf - -C 

You have to go the way over tar, and it will be inaccurate on small files. Also you must take care that the target directory exists. But it is a way.

你必须绕过 tar,它在小文件上会不准确。此外,您必须注意目标目录是否存在。但这是一种方式。

回答by Nishant

A simple unixway is to go to the destination directory and do watch -n 5 du -s. Perhaps make it more pretty by showing as a bar . This can help in environments where you have just the standard unix utils and no scope of installing additional files . du-sh is the key , watch is to just do every 5 seconds. Pros: Works on any unix system Cons: No Progress Bar

一个简单的 unix方法是转到目标目录并执行watch -n 5 du -s. 也许通过显示为条形来使其更漂亮。这在您只有标准 unix 实用程序而没有安装附加文件范围的环境中会有所帮助。du-sh 是关键, watch 就是每 5 秒做一次。 优点:适用于任何 unix 系统 缺点:没有进度条

回答by nachoparker

To add another option, you can use cpv. It uses pvto imitate the usage of cp.

要添加另一个选项,您可以使用cpv. 它pv用来模仿cp.

It works like pvbut you can use it to recursively copy directories

它的工作原理类似,pv但您可以使用它来递归复制目录

enter image description here

在此处输入图片说明

You can get it here

你可以在这里得到

回答by elboletaire

My preferred option is Advanced Copy, as it uses the original cpsource files.

我的首选选项是Advanced Copy,因为它使用原始cp源文件。

$ wget http://ftp.gnu.org/gnu/coreutils/coreutils-8.21.tar.xz
$ tar xvJf coreutils-8.21.tar.xz
$ cd coreutils-8.21/
$ wget --no-check-certificate https://raw.githubusercontent.com/atdt/advcpmv/master/advcpmv-0.5-8.21.patch
$ patch -p1 -i advcpmv-0.5-8.21.patch
$ ./configure
$ make

The new programs are now located in src/cp and src/mv. You may choose to replace your existing commands:

新程序现在位于 src/cp 和 src/mv 中。您可以选择替换现有命令:

$ sudo cp src/cp /usr/local/bin/cp
$ sudo cp src/mv /usr/local/bin/mv

回答by Ken Brill

How about something like

怎么样的东西

find . -type f | pv -s $(find . -type f | wc -c) | xargs -i cp {} --parents /DEST/$(dirname {})

It finds all the files in the current directory, pipes that through PV while giving PV an estimated size so the progress meter works and then piping that to a CP command with the --parents flag so the DEST path matches the SRC path.

它查找当前目录中的所有文件,通过 PV 将这些文件通过 PV 进行管道传输,同时为 PV 提供估计的大小,以便进度表正常工作,然后将其传输到带有 --parents 标志的 CP 命令,以便 DEST 路径与 SRC 路径匹配。

One problem I have yet to overcome is that if you issue this command

我尚未克服的一个问题是,如果您发出此命令

find /home/user/test -type f | pv -s $(find . -type f | wc -c) | xargs -i cp {} --parents /www/test/$(dirname {})

the destination path becomes /www/test/home/user/test/....FILES... and I am unsure how to tell the command to get rid of the '/home/user/test' part. That why I have to run it from inside the SRC directory.

目标路径变为 /www/test/home/user/test/....FILES... 并且我不确定如何告诉命令摆脱“/home/user/test”部分。这就是为什么我必须从 SRC 目录中运行它的原因。

回答by Foo Bah

There's a tool pvto do this exact thing: http://www.ivarch.com/programs/pv.shtml

有一个工具pv可以做到这一点:http: //www.ivarch.com/programs/pv.shtml

There's a ubuntu version in apt

apt 中有一个 ubuntu 版本

回答by KIRAN B

Check the source code for progress_barin the below git repository of mine

在我的以下 git 存储库中检查progress_bar的源代码

https://github.com/Kiran-Bose/supreme

https://github.com/Kiran-Bose/supreme

Also try custom bash script package supremeto verify how progress bar work with cpand mvcomands

也可以尝试定制bash脚本包至上,以验证如何与进度条工作CPMV系统管理命令

Functionality overview

功能概述

(1)Open Apps ----Firefox ----Calculator ----Settings

(1)打开应用程序----Firefox ----计算器----设置

(2)Manage Files ----Search ----Navigate ----Quick access

(2)管理文件----搜索----导航----快速访问

            |----Select File(s)
            |----Inverse Selection
            |----Make directory
            |----Make file
                                          |----Open
                                          |----Copy
                                          |----Move
                                          |----Delete
                                          |----Rename
                                          |----Send to Device
                                          |----Properties

(3)Manage Phone ----Move/Copy from phone ----Move/Copy to phone ----Sync folders

(3)管理手机----从手机移动/复制----移动/复制到手机----同步文件夹

(4)Manage USB ----Move/Copy from USB ----Move/Copy to USB

(4)Manage USB ----Move/Copy from USB ----Move/Copy to USB