0

When I copy a hard drive I often use the dd command

I have one question about monitoring.

To see the progress of the copy I do like this

sudo dd if=$source of=$pathToUsb bs=4M conv=fdatasync status=progress

and I get the following message:

2969567232 octets (3,0 GB, 2,8 GiB) copiés, 22 s, 134 MB/s

if i want to retrieve only the number of bytes how can i proceed ?

sudo dd if=$source of=$pathToUsb bs=4M conv=fdatasync status=progress > capture.txt 2>&1

I also tried this

sudo dd if=$source of=$usb bs=4M conv=fdatasync 2>&1 | awk 'END {print ($0+0)}'

But it doesn't work

Any ideas ?

kramer
  • 13
  • 1
  • 4

1 Answers1

1

If I understand the question, you just one the first column of the dd output printed. The following pipes stderr and stdout to stdbuf to force output into 1 char buffer sizes so that the piping does not buffer a 4K page at a time. The tr replaces the line carriage return with a newline so that cut can extract lines at a time, and the cut selects field one of the output, namely the byte count from dd.

dd if=/dev/zero of=/dev/null bs=1M status=progress 2>&1 | stdbuf -o1 tr '\r' '\n' | cut -d' ' -f1
Colin Ian King
  • 18,370
  • 3
  • 59
  • 70
  • Thanks. It's almost what i want, because i try to get only the bytes (number of bytes). so i add 'code'sudo dd if=$source of=$usb bs=4M conv=fdatasync status=progress 2>&1 | stdbuf -o1 tr '\r' '\n' | cut -d' ' -f1 | sed 's/[a-z]*//g' but monitoring appears when dd process is ended. Any idea why ? – kramer Jan 20 '21 at 07:47
  • I suspect sed is blocked waiting for a 4K pipe buffer to be full before it can read the data from the pipe. Putting stdbuf -o1 before the sed command may resolve this, e.g. stdbuf -o1 sed 's/[a-z]*//g – Colin Ian King Jan 20 '21 at 09:10
  • i tried with stdbuf -o1 sed 's/[a-z]*//g but same problem. – kramer Jan 20 '21 at 17:26
  • Try: dd if=/dev/zero of=/dev/null bs=1M status=progress 2>&1 | stdbuf -o1 tr '\r' '\n' | stdbuf -o1 cut -d' ' -f1 | sed 's/[a-z]*//g' – Colin Ian King Jan 20 '21 at 18:00
  • 1
    Wonderfull , now it works , thanks a lot – kramer Jan 21 '21 at 07:50