1

According to this article, by replacing:

$ dd if=/dev/sda of=/dev/sdb [additional options]

with:

$ pv -tpreb /dev/sda | dd of=/dev/sdb [additional options]

one can augment the default dd behaviour by displaying a progress bar, similar to that in wget. This works well when I can remember to use it, so I thought aliasing the first command to the second was the logical next step.

After some research, it would appear a simple alias in .bash_rc can't accomplish this. Could someone provide a BASH function to capture the if=... and work it into the resultant command?

Timo
  • 6,332
James Fu
  • 119

2 Answers2

3

Aliases are only suitable to give a command a shorter name or give it extra arguments. For anything else, use a function. See Aliases vs functions vs scripts for more details.

pvdd () {
  pv -tpreb "$1" | dd of="$2"
}
pvdd /dev/sda /dev/sdb

However, do not use this function. dd if=foo of=bar is equivalent to cat <foo >bar, only

  • slower, and worse,
  • unreliable in certain circumstances, especially when reading or writing to a pipe.

The use of dd as a low-level command to access disks is a myth¹. The magic comes from the /dev entries, not from dd.

So the command you want is simply

pv -tpreb /dev/sda >/dev/sdb

and you can make an alias for dd -tpreb if you like.

¹ There is a historical origin to this myth: when accessing tapes, the control over block size that dd provides is sometimes necessary. But for everything else, imposing a block size the way dd does it can lead to data loss.

2

Your question isn't terribly clear, but it appears that you need a shell function. You could create a function mydd:

mydd() { pv -tpreb /dev/sda | dd "$@"; }

and invoke it by saying:

mydd of=/dev/sdb [additional options]

and the executed command would be:

pv -tpreb /dev/sda | dd of=/dev/sdb [additional options]
devnull
  • 10,691
  • Thanks for this. It's pretty close to where I need it, except /dev/sda should be a variable. Is it possible to make the shell function execute pv -tpreb [source] | dd of=[dest] [optional additional options] from dd if=[source] of=[dest] [optional additional options]? Thanks! – James Fu Mar 12 '14 at 07:15