pv 命令详解

| 选择喜欢的代码风格  



pv 命令安装:

-bash/zsh: pv: command not found

# Windows (WSL2)
sudo apt-get update sudo apt-get install pv

# Debian
apt-get install pv

# Ubuntu
apt-get install pv

# Alpine
apk add pv

# Arch Linux
pacman -S pv

# Kali Linux
apt-get install pv

# Fedora
dnf install pv

# OS X
brew install pv

# Raspbian
apt-get install pv

# Dockerfile

# Docker
docker run cmd.cat/pv pv

pv 命令补充说明:

pv 命令通过给出的方式显示数据通过管道的进度信息,例如经过的时间、完成的百分比(带有进度条)、当前吞吐率、传输的总数据量、和预计到达时间。

要使用 pv 命令,请将其插入两个进程之间的管道中,使用适当的选项。 它的标准输入将被传递直到其标准输出,进度将显示在标准误差。

pv 命令会将每个提供的文件依次复制到标准输出( - 表示标准输入),或者如果没有指定文件,则仅指定标准输入输入被复制。 这与 cat 命令的行为相同。

pv 命令语法:

pv [OPTION] [FILE]...
pv [-h|-V]

pv 命令选项:

pv takes many options, which are divided into display switches, output modifiers, and general options.

     Display Switches

If no display switches are specified, pv behaves as if -p, -t, -e, -r, and -b had been given (i.e. everything is switched on). 
Otherwise, only those display types that are explicitly switched on will be shown.

-p, --progress
Turn the progress bar on. If standard input is not a file and no size was given (with the -s modifier), the progress bar cannot indicate how close to completion the transfer is, so it will just move left and right to indicate that data is moving.

-t, --timer
Turn the timer on. This will display the total elapsed time that pv has been running for.

-e, --eta
Turn the ETA timer on. This will attempt to guess, based on previous transfer rates and the total data size, how long it will be before completion. This option will have no effect if the total data size cannot be determined.

-r, --rate
Turn the rate counter on. This will display the current rate of data transfer.

-b, --bytes
Turn the total byte counter on. This will display the total amount of data transferred so far.

-n, --numeric
Numeric output. Instead of giving a visual indication of progress, pv will give an integer percentage, one per line, on standard error, suitable for piping (via convoluted redirection) into dialog(1). Note that -f is not required if -n is being used.

-q, --quiet
No output. Useful if the -L option is being used on its own to just limit the transfer rate of a pipe.
Output Modifiers

-W, --wait
Wait until the first byte has been transferred before showing any progress information or calculating any ETAs. Useful if the program you are piping to or from requires extra information before it starts, eg piping data into gpg(1) or mcrypt(1) which require a passphrase before data can be processed.

-s SIZE, --size SIZE
Assume the total amount of data to be transferred is SIZE bytes when calculating percentages and ETAs. The same suffixes of "k", "m" etc can be used as with -L.

-l, --line-mode
Instead of counting bytes, count lines (newline characters). The progress bar will only move when a new line is found, and the value passed to the -s option will be interpreted as a line count.

-i SEC, --interval SEC
Wait SEC seconds between updates. The default is to update every second. Note that this can be a decimal such as 0.1.

-w WIDTH, --width WIDTH
Assume the terminal is WIDTH characters wide, instead of trying to work it out (or assuming 80 if it cannot be guessed).

-H HEIGHT, --height HEIGHT
Assume the terminal is HEIGHT rows high, instead of trying to work it out (or assuming 25 if it cannot be guessed).

-N NAME, --name NAME
Prefix the output information with NAME. Useful in conjunction with -c if you have a complicated pipeline and you want to be able to tell different parts of it apart.

-f, --force
Force output. Normally, pv will not output any visual display if standard error is not a terminal. This option forces it to do so.

-c, --cursor
Use cursor positioning escape sequences instead of just using carriage returns. This is useful in conjunction with -N (name) if you are using multiple pv invocations in a single, long, pipeline.
Data Transfer Modifiers

-L RATE, --rate-limit RATE
Limit the transfer to a maximum of RATE bytes per second. A suffix of "k", "m", "g", or "t" can be added to denote kilobytes (*1024), megabytes, and so on.

-B BYTES, --buffer-size BYTES
Use a transfer buffer size of BYTES bytes. A suffix of "k", "m", "g", or "t" can be added to denote kilobytes (*1024), megabytes, and so on. The default buffer size is the block size of the input file's filesystem multiplied by 32 (512kb max), or 400kb if the block size cannot be determined.

-R PID, --remote PID
If PID is an instance of pv that is already running, -R PID will cause that instance to act as though it had been given this instance's command line instead. For example, if pv -L 123k is running with process ID 9876, then running pv -R 9876 -L 321k will cause it to start using a rate limit of 321k instead of 123k. Note that some options cannot be changed while running, such as -c, -l, and -f.

     General Options

-h, --help
Print a usage message on standard output and exit successfully.

-V, --version
Print version information on standard output and exit successfully.

pv 命令参数:


pv 命令实例:

pv 打印文件内容并显示进度条:

pv path/to/file

pv 测量管道之间数据流的速度和数量( --size 是可选的):

command1 | pv --size expected_amount_of_data_for_eta | command2

pv 命令过滤文件,查看进度和输出数据量:

pv -cN in big_text_file | grep pattern | pv -cN out > filtered_file

pv 命令附加到已经运行的进程并查看其文件读取进度:

pv -d PID

pv 读取错误的文件,跳过错误,如 dd conv=sync,noerror 所示:

pv -EE path/to/faulty_media > image.img

pv 命令读取指定数量的数据后停止读取,速率限制为 1K/s

pv -L 1K --stop-at --size maximum_file_size_to_be_read

pv 命令扩展阅读:

pv 命令评论