pv command in Linux

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

pv is a terminal-based (command-line based) tool in Linux that allows us for the monitoring of data being sent through pipe. The full form of pv command is Pipe Viewer.

pv helps the user by giving him a visual display of the following,

  • Time Elapsed
  • Completed Progress (percentage bar)
  • Current data transfer speed (also reffered as throughput rate)
  • Data Transfered
  • ETA (Estimated Time) (Remaining time)

For installing pv command,

  1. debian based Operating System
$ apt-get install pv

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  doc-base
The following NEW packages will be installed:
  pv
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 48.3 kB of archives.
After this operation, 123 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu focal/main amd64 pv amd64 1.6.6-1 [48.3 kB]
Fetched 48.3 kB in 8s (6,234 B/s)
Selecting previously unselected package pv.
(Reading database ... 191130 files and directories currently installed.)
Preparing to unpack .../archives/pv_1.6.6-1_amd64.deb ...
Unpacking pv (1.6.6-1) ...
Setting up pv (1.6.6-1) ...
Processing triggers for man-db (2.9.1-1) ...
  1. rpm based Operating Systems
$ yum install pv

================================================================================
 Package      Architecture     Version                   Repository        Size
================================================================================
Installing:
 pv           x86_64           1.6.6-8.fc32              fedora            62 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 62 k
Installed size: 120 k
Is this ok [y/N]: y
Downloading Packages:
pv-1.6.6-8.fc32.x86_64.rpm                                        19 kB/s |  62 kB     00:03    
-------------------------------------------------------------------------------------------------
Total                                                            8.8 kB/s |  62 kB     00:07
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                        1/1 
  Installing       : pv-1.6.6-8.fc32.x86_64                                                 1/1 
  Running scriptlet: pv-1.6.6-8.fc32.x86_64                                                 1/1 
  Verifying        : pv-1.6.6-8.fc32.x86_64                                                 1/1 

Installed:
  pv-1.6.6-8.fc32.x86_64

Complete!

How to use the pv command?

pv is used to provide the ability of monitoring progress of a given application which lacks the mentioned functionality. It can be used by placing a pipe operator (|) between two processes.

Syntax of pv command:

pv fileName
pv OPTIONS fileName
pv fileName > outputFileName
pv OPTIONS | command > outputFileName
command1 | pv | command2

Standard Input of the pv command is passed to Standard Output and then the result is printed to Standard Error

Options for pv command

General Options

  • -help : Displays the usage information.
  • --version : Displays the version information.

Display Modifiers

  • -p : Displays the progress bar.
  • -timer : Displays the elapsed time.
  • -eta : Displays the Estimated Time for the operation. Guess for the time is based on the previous data transfer rate and th size of data to be transfered.
  • -rate : Turns on the rate counter for the operation.
  • -bytes : Displays the total amount of data transfered till now.
  • -n : Displays integer percentage instead of the visual representation.

Output Modifiers

  • --wait / -W : To wait for transfer of first byte before displaying progress.
  • --interval SECONDS / -i SECONDS : Specified the time interval between updates.
  • --force / -F : Forces an operation, i.e. forces pv to display visuals even when Standard Error is not a terminal.
  • --size SIZE / -s : Assumees the total data to be transwered is SIZE bytes for calculating computing percentage or ETA.
  • --line-mode / -L : Instead of counting size, progress bar will move if new line is found.
  • --name NAME / -n NAME : Prefix output info with name.
  • --cursor / -c : Use cursor positioning escape sequence instead of using carriage returns.

Data Transfer Modifier

  • --rate-limit RATE / -L RATE : Limit transfer to max of RATE byte per second.
  • --buffer-size BYTES / -B BYTES : Use transfer buffer size of BYTE bytes.
  • --remote PID / -R PID : If PID is instance of pv, will cause that instance to act, through it has been given instance's command line instead.

When no option is selected -p, -t, -e, -r, -b options are selected by default.

Examples

  1. Creating a progress bar with the copy command
$ pv history.log > $HOME/Documents/history.log

150KiB 0:00:00 [29.6MiB/s] [================================>] 100% 
  1. Making zip with the progress bar
$ pv history.log | $HOME/Documents/history.log

  adding: - 150KiB 0:00:00 [13.8MiB/s] [================================>] 100%            
 (deflated 98%)
  1. Count number of lines, words, bytes
$ pv -p history.log | wc

[========================================================================>] 100%
   7305   22724  154336
  1. Monitor tar progress
$ tar -czf - ./Documents/ | (pv -p --timer --rate --bytes > backup.tgz)

6.36KiB 0:00:00 [ 239KiB/s] [<=>                                               ]

With this article at OpenGenus, you must have the complete idea of using pv command in UNIX. Enjoy.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.