pv 测量数据传输进度工具

发布时间: 更新时间: 总字数:660 阅读时间:2m 作者: IP上海 分享 网址

Linux中pv命令主要用于测量显示数据在管道中的传输进度,它可以为用户提供一个可视化的界面来监控数据流的传输情况

介绍

通过pv命令,可以清楚地看到数据传输的速度、进度条、已经传输的数据量、花费的时间、剩余时间等信息,常用场景:

  • 网络流量统计
  • 数据备份、复制或转换过程中,数据传输的进度以及预计的剩余时间

安装

apt install pv

help

pv --help ...
$ pv --help
Usage: pv [OPTION] [FILE]...
Concatenate FILE(s), or standard input, to standard output, with monitoring.

  -p, --progress                 show progress bar
  -t, --timer                    show elapsed time
  -e, --eta                      show estimated time of arrival (completion)
  -I, --fineta                   show absolute estimated time of arrival (completion)
  -r, --rate                     show data transfer rate counter
  -a, --average-rate             show data transfer average rate counter
  -m, --average-rate-window SEC  compute average rate over past SEC seconds (default 30s)
  -b, --bytes                    show number of bytes transferred
  -8, --bits                     show number of bits transferred
  -T, --buffer-percent           show percentage of transfer buffer in use
  -A, --last-written NUM         show NUM bytes last written
  -F, --format FORMAT            set output format to FORMAT
  -n, --numeric                  output percentages, not visual information
  -q, --quiet                    do not output any transfer information at all

  -W, --wait                     display nothing until first byte transferred
  -D, --delay-start SEC          display nothing until SEC seconds have passed
  -s, --size SIZE                set estimated data size to SIZE bytes
  -l, --line-mode                count lines instead of bytes
  -0, --null                     lines are null-terminated
  -i, --interval SEC             update every SEC seconds
  -w, --width WIDTH              assume terminal is WIDTH characters wide
  -H, --height HEIGHT            assume terminal is HEIGHT rows high
  -N, --name NAME                prefix visual information with NAME
  -f, --force                    output even if standard error is not a terminal
  -c, --cursor                   use cursor positioning escape sequences

  -L, --rate-limit RATE          limit transfer to RATE bytes per second
  -B, --buffer-size BYTES        use a buffer size of BYTES
  -C, --no-splice                never use splice(), always use read/write
  -E, --skip-errors              skip read errors in input
  -Z, --error-skip-block BYTES   skip errors in BYTES blocks at a time
  -S, --stop-at-size             stop after --size bytes have been transferred
  -Y, --sync                     flush cache to disk after every write
  -K, --direct-io                use direct I/O to bypass cache
  -X, --discard                  discard input instead of writing to output
  -R, --remote PID               update settings of process PID

  -P, --pidfile FILE             save process ID in FILE

  -d, --watchfd PID[:FD]         watch file FD opened by process PID

  -h, --help                     show this help and exit
  -V, --version                  show version information and exit

Please report any bugs to: pv@ivarch.com

示例

实时监控网络速度

tcpdump -i ens33 -w - | pv -bert > /dev/null
# 12.0KiB 0:00:02 [8.00  B/s]

监视文件拷贝

# 生成 76M 数据
seq 1 500000000 > text.txt
du -lh text.txt

# 监视一个文件从源目录复制到目标目录的进度
$ pv text.txt > /tmp/text.txt
1.12GiB 0:00:03 [ 232MiB/s] [================>                                 ]  24% ETA 0:00:06

# 指定格式
$ pv -F 'Transfer: %t Elapsed: %e Transfer rate: %r' text.txt > /tmp/text.txt
Transfer: 0:00:01 Elapsed: ETA 0:00:04 Transfer rate: [ 816MiB/s]

监视tar速度

tar cf - source_directory/ | pv -s $(du -sb source_directory/ | awk '{print $1}') | gzip > archive.tar.gz

限制传输速度

限制为 1MB/s

$ cat text.txt | pv -L 1M > /tmp/text.txt
2.00MiB 0:00:02 [1023KiB/s] [   <=>                           ]
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数