Bash: Using pv to Display Progress of dd

Bash: Using pv to Display Progress of dd

One of the biggest weaknesses of dd is that it has no way to display progress on its actions.  You can send a signal to the process which will pause, display statistics, and resume the process however this takes up alot of your terminal screen if you are doing any sort of long running copy.  Enter pv.  Pv allows us to monitor the progress of data through a pipe.

Create A 1GB Test File

This is the file that we will use to peform the following tests.

# dd if=/dev/zero of=/root/test.file bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 5.88164 s, 183 MB/s

Insert PV into the Pipe (Progress Bar Not Working)

Now in this example we have the basics of a progress bar.  At least we can see it is working, but the progress bar will just bounce back and forth like we are using Windows.

# dd if=/root/test.file | pv -tpreb | dd of=/root/test.file2
666MB 0:00:11 [36.5MB/s] [          <=>                                      ]

Insert PV into the Pipe (Progress Bar Fixed)

In order to get an accurate progress bar pv needs to be aware of how much data we are expecting.  Below this is showing accurate progress.  You can additionally use pv instead of the first dd, though I prefer to use pv simply to measure instead of rely on it to move the data.

# dd if=/root/test.file | pv -tpreb -s 1024M | dd of=/root/test.file2
666MB 0:00:05 [ 171MB/s] [=====================>             ] 65% ETA 0:00:02

Insert PV into the Pipe (Output is not Working)

Upon completion of the above command we will see that the output is not quite right.  Basically this is because dd is overwriting pv’s output upon completion.

# dd if=/root/test.file | pv -tpreb -s 1024M | dd of=/root/test.file2
2097152+0 records inMB/s] [=============================>     ] 88% ETA 0:00:00
2097152+0 records out
1073741824 bytes (1.1 GB) copied, 6.9598 s, 154 MB/s
1GB 0:00:06 [ 147MB/s] [=================================>] 100%
2097152+0 records in
2097152+0 records out
1073741824 bytes (1.1 GB) copied, 6.86994 s, 156 MB/s

Insert PV into the Pipe (Output is Working)

To fix the above we simply need to dump the output of dd, so that it doesn’t overwrite the pv output.

# dd if=/root/test.file 2&gt;/dev/null | pv -tpreb -s 1024M | dd of=/root/test.file2 2&gt;/dev/null
1GB 0:00:06 [ 148MB/s] [=================================>] 100%

Well there you go.  Keep in mind you can also use pv with netcat, cat, or anything else which ends up sending data through a stream.