16.12.2017

The Advent of Void: Day 16: pv

A tool with many uses is pv(1), the ‘pipe viewer’. Essentially, you can put it between any pipe and you’ll see statistics on its throughput.

For example, we can measure the speed of /dev/urandom:

% pv < /dev/urandom > /dev/null
168MiB 0:00:01 [ 167MiB/s] [<=>                                               ]

Or look how fast we can extract a tar.xz file:

% xzcat big.xz | pv | tar xf -

Or copy a whole disk to a remote host, with a progress bar:

# dd if=/dev/sdb | pv | ssh me@remote dd of=/dev/sdc

pv has a few nifty flags, for example, it also can count lines instead of bytes. So we can build a progress bar for a find/xargs call (or rather, lr/ xe):

% find /tmp -name '*.png' | pv -l | xargs -P4 -n1 pngcrush -ow

(Or even better, use find -print0 and pv -0.)

Finally, pv also can limit the bandwidth, which can be useful if you want to limit transfer rates to lower strain on the system, for example, and buffer the input, which is useful to ensure a consistent write rate.