linux 命令pv_如何监视Linux命令的进度(带有pv和progress)

linux 命令pv

linux 命令pv

Linux terminal prompt on a laptop
Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com

Instead of flying blind, use the Linux pv and progress commands to track a command’s progress. These utilities will give you progress bars for commands that don’t normally have any. You’ll see an estimated time until completion, too.

不要盲目地使用Linux pvprogress命令来跟踪命令的进度。 这些实用程序将为您提供通常没有任何命令的进度条。 您还会看到估计的完成时间。

If you are on a long haul flight on an aircraft without video screens in the seatbacks, it isn’t easy to know how far through your journey you are. You know when you took off. You know how long the flight is expected to take. But how do you know if you are on track, on time, or way behind schedule? If you don’t want to watch the in-flight movie you can usually switch your video screen to show a map with the position of your aircraft on it. You also get some statistics, such as an expected time of arrival (ETA), which is great.

如果您乘坐的飞机是长途飞行,而座椅靠背上没有视频屏幕,那么很难知道您要走多远。 你知道什么时候起飞。 您知道预计飞行需要多长时间。 但是您如何知道自己是否按时,按时或落后于进度? 如果您不想看机上电影,通常可以切换视频屏幕以显示带有飞机位置的地图。 您还会得到一些统计数据,例如预期到达时间(ETA),这非常好。

Starting a command from the terminal window can sometimes feel like a long-haul flight without a video screen. You have nothing to give any indication if all is well or if the process has hung, nor how close to completion it is. A flashing cursor isn’t very informative.

从终端窗口启动命令有时会感觉像是没有视频屏幕的长途飞行。 您没有任何迹象表明一切是否正常或该过程是否已挂起,也没有完成的程度。 闪烁的光标不是很有用。

The pv and progress commands give you some statistics and a little visual feedback. You can see how close the process is to complete. That means you get an ETA for your running processes. Compared with staring at a cursor, that wins hands down.

pvprogress命令为您提供一些统计信息和一些视觉反馈。 您可以看到该过程完成的程度。 这意味着您将获得正在运行的进程的预计到达时间。 与盯着光标相比,这很容易。

安装光伏 (Installing pv)

You must install pv.

您必须安装pv

To install pv on Ubuntu use this command:

要在Ubuntu上安装pv ,请使用以下命令:

sudo apt-get install pv
sudo apt-get install pv in a terminal window

To install pv on Fedora use this command:

要在Fedora上安装pv ,请使用以下命令:

sudo dnf install pv
sudo dnf install pv in a terminal window

To install pv on Manjaro use this command:

要在Manjaro上安装pv ,请使用以下命令:

sudo pacman -Syu pv
sudo pacman -Syu pv in a terminal window

使用光伏 (Using pv)

pv stands for pipe viewer. Piping has to be involved in the command somewhere. Here’s an example where we’re piping an ISO image through zip to make a compressed zip file of the ISO.

pv代表管道查看器。 管道必须与某处的命令有关。 这是一个示例,我们通过zip传递ISO映像以制作ISO的压缩zip文件。

To slow the commands down enough that a screenshot could be taken, some of the files in the examples used for this article were stored on an old, slow, external USB called SILVERXHD.

为了使命令足够慢以可以截取屏幕截图,本文示例中的某些文件存储在名为SILVERXHD的旧的,缓慢的外部USB上。

pv /media/dave/SILVERXHD/gparted-live-1.0.0-1-amd64.iso | zip > gparted.zip
pv /media/dave/SILVERXHD/gparted-live-1.0.0-1-amd64.iso | zip > gparted.zip in a terminal window

The information pv gives us can be seen in the bottom line of the display.

pv给我们的信息可以在显示屏的底行看到。

pv output for creating a zip file in a terminal window

From left to right, the information that is displayed is:

从左到右,显示的信息是:

  • The data transferred so far.

    数据传输到此为止。
  • The time elapsed fo far.

    时间已经过去了。
  • The data transfer rate (throughput).

    数据传输速率(吞吐量)。
  • A progress bar and a percentage completed figure.

    进度条和完成百分比。
  • The estimated time left before completion (ETA).

    完成之前预计剩余的时间(ETA)。

用pv复制文件 (Copying a File with pv)

To copy a file with output from pv, use this command:

要复制带有pv输出的文件,请使用以下命令:

pv /media/dave/SILVERXHD/gparted-live-1.0.0-1-amd64.iso > gparted.iso
pv /media/dave/SILVERXHD/gparted-live-1.0.0-1-amd64.iso | gparted.iso in a terminal window

We get a progress report as the file is copied.

复制文件后,我们会收到进度报告。

copying a file with pv in a te terminal window

用pv复制多个文件 (Copying Multiple Files with pv)

To copy multiple files and folders with pv we need to use a little trick. We use tar to move the files for us.

要使用pv复制多个文件和文件夹,我们需要使用一些技巧。 我们使用tar为我们移动文件。

tar -c help-files/ | pv | tar -x -C Documents/
tar -c help-files/ | pv | tar -x -C Documents/ in a terminal window

The tar -c help-files/ portion of the command instructs tar to create (-c) an archive of the files in the help-files folder.  This is piped through pv so that we get a display of the progress. It is then piped back into tar for the last portion of the command. The archive is extracted (-x) and the directory is changed (-C) to Documents before the extraction.

命令的tar -c help-files/部分指示tar创建( -c )help-files文件夹中文件的存档。 这是通过pv传递的,以便我们显示进度。 然后将其通过管道输送回tar ,以执行命令的最后一部分。 在提取之前,将归档文件( -x )提取出来,并将目录( -C )更改为Documents。

So, the files and folders that are in help-files are copied to the Documents folder, with a progress display.

因此,帮助文件中的文件和文件夹将被复制到“文档”文件夹中,并显示进度。

copying files with pv and tar in a terminal window

The output is slightly different this time.

这次的输出略有不同。

We do not get an ETA. The progress bar now displays a moving indicator. It shows that the process is active, but it doesn’t grow from left to right like a traditional progress bar. pv is limited to displaying the information it can extract from the process that is being piped.

我们没有预计到达时间。 现在,进度条将显示一个移动指示器。 它表明该过程是活动的,但它没有像传统的进度条那样从左向右扩展。 pv仅限于显示其可以从正在传递的进程中提取的信息。

使用pv和tar创建档案 (Using pv and tar to Create an Archive)

Copying files with pv and tar does not leave us with an archive file. A sort of “virtual” archive is created by tar, which is fed straight back into tar to extract the files. If our objective is to copy files, that is accomplished. But what if we want to create an archive file?

使用pvtar复制文件不会给我们留下存档文件。 tar创建了一种“虚拟”存档,将其直接反馈到tar以提取文件。 如果我们的目标是复制文件,那就完成了。 但是,如果我们要创建一个存档文件怎么办?

We can still use tar to create an archive file and get a progress report from pv. The options used with tar are -c (create archive), -z (compress with gzip) and -f (filename of the archive).

我们仍然可以使用tar创建存档文件并从pv获取进度报告。 与tar一起使用的选项是-c (创建档案),- -z (使用gzip压缩)和-f (档案的文件名)。

Note that we’re using - as the filename, which causes tar to use stdout, and to write its output to the terminal window. We don’t see that output because it is piped through pv.

请注意,我们使用-作为文件名,这将导致tar使用stdout并将其输出写入终端窗口。 我们看不到该输出,因为它是通过pv传递的。

The actual name of the archive is going to be the filename that we pipe the output from pv into. In this case, it is “help-files.tgz”.

存档的实际名称将是我们将pv的输出传递到的文件名。 在这种情况下,它是“ help-files.tgz”。

tar -czf - ./help-files/ | pv > help-files.tgz
tar -czf - ./help-files/ | pv > help-files.tgz in a terminal window

We get the same progress indicators as before, and the archive file is created for us.

我们获得与以前相同的进度指示器,并且为我们创建了存档文件。

output of tar and pv in a terminal window

pv显示选项 (The pv Display Options)

There are a number of options you can use with pv to change the details of its report.

pv可以使用许多选项来更改其报告的详细信息。

If you use any of these options, all of the other options are turned off. So if you want to have three of the display options in use, then you need to specify those three options.

如果使用这些选项中的任何一个,则所有其他选项都将关闭。 因此,如果要使用三个显示选项,则需要指定这三个选项。

Using pv without any options is the same as using the -pterb options.

使用不带任何选项的pv与使用-pterb选项相同。

  • -p: display the percentage complete. This is the progress bar and the percentage completed figure.

    -p :显示完成百分比。 这是进度条和完成百分比图。

  • -t: display the elapsed time.

    -t :显示经过的时间

  • -e: display the ETA.

    -e :显示ETA

  • -r: display the rate of data transfer.

    -r :显示数据传输速率

  • -b: display the byte count (data transferred so far).

    -b :显示字节数(到目前为止已传输的数据)。

  • -n: display the percentage as an integer. This prints the percentage completed as an integer figure, with each new update on a new line.

    -n :将百分比显示为整数。 这会将完成的百分比打印为整数,并在新行上显示每个新更新。

Let’s repeat the last command and pass the -p (percentage completed) option to pv.

让我们重复最后一个命令,并将-p (完成百分比)选项传递给pv

tar -czf - ./help-files/ | pv - p > help-files.tgz
tar -czf - ./help-files/ | pv - p > help-files.tgz in a terminal window

This turns off all of the other display options. pv provides the percentage completed element only.

这将关闭所有其他显示选项。 pv仅提供完成百分比。

Because pv doesn’t get a percentage completed figure from tar, the progress bar is replaced with a moving indicator. There is no percentage figure.

由于pv无法从tar获取完成百分比,进度条将替换为移动指示器。 没有百分比数字。

output from pv and tar in a terminal window

与wc一起使用pv (Using pv With wc)

We can use pv to pipe a  text file (or files) into wc. wc will then count the carriage returns, characters, and words and pv will give us a progress report.

我们可以使用pv将一个(或多个)文本文件传输到wc 。 然后, wc将计算回车,字符和单词,而pv将向我们提供进度报告。

Here we are piping all of the “.page” files in the help-files directory into wc.

在这里,我们将help-files目录中的所有“ .page”文件传输到wc


   
   
pv ./help-files/*.page | wc in a terminal window

When wc completes we can see our count of carriage returns (lines), characters and words from all of the “.page” files in the help-files folder.

wc完成时,我们可以看到help-files文件夹中所有“ .page”文件中的回车计数(行),字符和单词。

output from pv and wc in a terminal window

安装进度命令 (Installing the progress Command)

The progress command gives the same sort of useful information as pv, but it works with a specific set of Linux commands.

progress命令提供了与pv相同的有用信息,但是它与一组特定Linux命令一起使用。

To install progress in Ubuntu, use this command:

要在Ubuntu中安装progress ,请使用以下命令:

sudo apt-get install progress
sudo apt-get install progress in a terminal window

To install progress in Fedora, use this command:

要在Fedora中安装progress ,请使用以下命令:

sudo dnf install progress
sudo dnf install progress in a terminal widow

To install progress in Manjaro, use this command:

要在Manjaro中安装progress ,请使用以下命令:

sudo pacman -Syu progress
sudo pacman -Syu progress in a terminal window

命令进度可用于 (The Commands progress Works With)

Typing progress in a terminal window and pressing Enter will give you a list of the commands that progress works with.

在终端窗口中输入progress ,然后按Enter键,将为您列出progress可使用的命令。

progress
output of progress commmand in a terminal window

对管道使用进度 (Using Progress With Pipes)

There are two techniques we can use to monitor commands with progress. The first is to use pipes.

我们可以使用两种技术来监视progress命令。 首先是使用管道。

The tar command is in the list of supported commands that progress can monitor, so let’s use tar.

tar命令位于progress可以监视的受支持命令的列表中,因此让我们使用tar

The options we’ll use are the standard -c (create archive), -z (compress with gzip) and -f (filename) options. We’re going to create a compressed archive of everything in the help-files folder, and the archive will be named “help.tgz”.

我们将使用的选项是标准的-c (创建档案),- -z (使用gzip压缩)和-f (文件名)选项。 我们将在help-files文件夹中创建所有内容的压缩存档,该存档将命名为“ help.tgz”。

We’re piping that into progress and using the -m (monitor) option so progress keeps reporting on the process until it has completed.

我们正在将其传递到progress并使用-m (监视程序)选项,因此progress会一直报告该过程,直到完成为止。

tar -czf help.tgz ./help-files/ | progress -m
tar -czf help.tgz ./help-files/ | progress -m in a terminal window

The terminal window will show the progress of the tar command as it creates the archive.

终端窗口将显示tar命令在创建归档文件时的进度。

progress command monitoring tar in a terminal window

As each file is processed, it is listed, with the following information:

在处理每个文件时,将列出该文件,并包含以下信息:

  • The process ID.

    进程ID。
  • The process name.

    进程名称。
  • Percentage completed.

    完成百分比。
  • Data processed and total size of the file.

    处理的数据和文件的总大小。
  • Data rate (throughput).

    数据速率(吞吐量)。
  • Estimated time remaining (ETA).

    估计剩余时间(ETA)。

You might be surprised to see a second data set appear. This first data set is for tar. The second one is for gzip. tar calls gzip to perform the compression. Because gzip is in the list of supported commands, progress reports on it.

您可能会惊讶地看到第二个数据集出现。 第一个数据集用于tar 。 第二个是gziptar调用gzip执行压缩。 因为gzip在受支持的命令列表中,所以它的progress报告。

在连续监视模式下使用进度 (Using Progress in Continual Monitor Mode)

You can use progress in a real-time continual monitor mode by using the -M (monitor) option.

您可以通过使用-M(监视)选项在实时连续监视模式下使用progress

Type the following command in a terminal window:

在终端窗口中键入以下命令:

progress -M
progress -M in a terminal window

progress will report that there are no commands running for it to monitor. But you are not returned to the command line. progress waits until a command that it can monitor starts. It will then automatically start reporting on it.

progress将报告没有任何要监视的命令正在运行。 但是您不会返回命令行。 progress一直等到它可以监视的命令启动。 然后它将自动开始对其进行报告。

progress waiting for a command to start in a terminal window

In another terminal window, type a command that is in the list of commands that progress can monitor.

在另一个终端窗口中,键入进度可以监视的命令列表中的命令。

We’re going to use cat. Commands that are over too quickly won’t register with progress, so we’ll list the contents of a very long text file.

我们将使用cat 。 速度过快的命令不会在progress注册,因此我们将列出一个很长的文本文件的内容。

cat words.page
cat words.page in a terminal window

In the terminal window with progress in it, you’ll see statistics for the cat command as it executes and works towards completion.

在其中包含progress的终端窗口中,您将在cat命令执行并完成时看到其统计信息。

progress reporting on cat in a terminal window

When cat finishes listing the file progress returns to its waiting state.

cat完成列出后,文件progress将返回其等待状态。

Each time one of the commands it can report on performs a sizeable task, progress will automatically monitor it and report on it.

每次它可以报告的命令之一执行较大的任务时, progress都会自动对其进行监视并报告。

That’s pretty neat.

那很整齐。

100%完成 (100% Completed)

Take the guesswork out of wondering how a long-running command is doing, and take a break from contemplating your cursor with pv and progress .

不用猜测一个长时间运行的命令是如何做的,而花点时间休息一下,用pvprogress来考虑光标。

翻译自: https://www.howtogeek.com/428654/how-to-monitor-the-progress-of-linux-commands-with-pv-and-progress/

linux 命令pv

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值