Count number of bytes piped from one process to another

Solution 1:

Use pv the pipe viewer. It's a great tool. Once you know about it you'll never know how you lived without it.

It can also show you a progress bar, and the 'speed' of transfering.

Solution 2:

Pipe through dd. dd's default input is stdin and default output is stdout; when it finishes stdin/stdout I/O, it will report to stderr on how much data it transferred.

If you want to capture the output of dd and the other programs already talk to stderr, then use another file-descriptor. Eg,

$ exec 4>~/fred
$ input-command | dd 2>&4 | output-command
$ exec 4>&-

Solution 3:

process_a | tee >(process_b) | wc --bytes might work. You can then redirect wc's count to where-ever you need it. If process_b outputs anything to stdout/stderr you will probably need to redirect this off somewhere, if only /dev/null.

For a slightly contrived example:

filestore:~# cat document.odt | tee >(dd of=/dev/null 2>/dev/null) | wc --bytes

By way of explanation: tee lets you direct output to multiple files (plus stdout) and the >() construct is bash's "process substitution" which makes a process look like a write-only file in this case so you can redirect to processes as well as files (see here, or this question+answer for an example of using tee to send output to many processes).