Thursday, 2 December 2010

Sparse copying from a pipe on Linux with cp

One of the annoying 'missing options' on gzip and bzip is one to write the file out in a sparse fashion. This is particularly irritating when you're manipulating virtual machine images - which are often 90%+ empty space.

So you try and pipe it into 'cp --sparse=always' and you find that it can't read from stdin natively.

Then you scratch your head for several months. Or search the internet for 'sparsifiers' - with little success.

And finally you stumble across the answer, which was staring you in the face all along.

zcat -f "$in" | cp --sparse=always /dev/stdin "$out"

I offer this to the Google gods in the hope that others find it faster than I did.