Search code examples
pipestderrpv

Print pv output (stderr) to file


How can I print the output of to a file? For example:

timeout 5s dd if=/dev/random | pv -r > /dev/null
[ 505kiB/s]

The rate line output is "updated" over the course of my five second timeout. I tried this but it does not work (log is empty):

timeout 5s dd if=/dev/random | pv -r > /dev/null 2> rates.log

I believe it has something to do with carriage returns in the output, but after an hour I am stuck. Ideally my log file would have multiple lines each time prints a new value:

[ 505kiB/s]
[ 498kiB/s]
[ 542kiB/s]
[ 513kiB/s]
[ 509kiB/s]

UPDATE:

To get the content into a file as I described above I had to use though I'm not sure why it is required ( alone didn't work, the file will be empty without ):

timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> >(stdbuf -oL tr '\r' '\n' > rates.log)

Solution

  • From man pv:

    -f, --force
    Force output. Normally, pv will not output any visual display if standard error is not a terminal. This option forces it to do so.

    Since rates.log isn't a terminal, you need to do pv -fr instead of pv -r.