Pipe viewer: Using the pv command on Linux

The pv command is a rather unusual Linux command, but it can provide some insights into commands, especially those that send data through pipes. This includes some visual feedback that can sometimes be very reassuring as it tells you that something is occurring even when you’re sitting and waiting for some output to appear. The name “pv” stands for “pipe viewer” and you should be able to install it with one of these commands:

$ sudo dnf install pv
$ sudo apt -y install pv

When you insert the pv command into a pipeline between two processes, its standard input will be passed through to its standard output and progress will be shown on standard error. It is commonly used to display such things as the time elapsed, the completed progress (percentage bar), the current data transfer speed (also referred to as the throughput rate), the data transferred and the estimated completion time.

In this first example, the pv command with the -p (progress) option is examining a file and sending the data through a pipe to the wc command.

$ pv -p myfile | wc
[================================================================================================================>] 100%
   1000    3275   22596

The numbers (as you may understand), represent the counts provided by the wc command (i.e., the lines, the words and the characters in the file). The pv command provided the long bar of equal signs even though no reassurance would be needed in such a fast-running command. This simple example is meant to show you what to expect as you get used to the command.

In this next example, pv is simply copying a file to /tmp while sharing some data on the move.

$ pv myfile > /tmp/myfile
22.1KiB 0:00:00 [ 136MiB/s] [========================================================================>] 100%

Note that the bar gives an indication of the task’s progression. We also see the size of the file (22.1KiB) and the speed of its move to the /tmp directory (136MiB per second).

With the -t (timer) option, pv displays the runtime and, again, the wc command shows the stats for the lines, words and characters in the file.

$ pv -t H | wc
0:00:00
   1000    3275   22596

In this next command, a backup is made of the local videos directory and the pv command  provides the size of the resultant file and the rate of speed used (10.3MiB/s). Due to the speed, the time required didn’t amount to even one second.

$ tar -czf - ./videos/ | (pv -c --timer --rate --bytes > video_backup.tgz)
57.3KiB 0:00:00 [10.3MiB/s]

Here’s the file, so that you can get an idea of its size:

$ ls -l video_backup.tgz
-rw-r--r--. 1 shs shs 58630 May 25 12:49 video_backup.tgz

Some of the more commonly used options for the pv command include the following. However, the man page is quite extensive and is worth looking over should you want to see what else you can do with this command.

-p, --progress
       -t, --timer
       -e, --eta
       -r, --rate
       -a, --average-rate
       -b, --bytes

Wrap-up

The pv command is especially helpful when you are running tasks that take a long time to complete and you want some feedback that displays your progress.

Source:: Network World