I just backed up the microSD card from my Raspberry Pi on my PC running a Linux distro using this command:
dd if=/dev/sdx of=file.bin bs=16M
The microSD card is only 3/4 full so I suppose there's a few gigs of null bytes at the end of the tremendous file. I am very sure I don't need that. How can I strip those null bytes from the end efficiently so that I can later restore it with this command?
cat file.bin /dev/zero | dd of=/dev/sdx bs=16M
Best Answer
To create a backup copy of a disk while saving space, use
gzip
:When you want to restore the disk from backup, use:
This will likely save much more space than merely stripping trailing NUL bytes.
Removing trailing NUL bytes
If you really want to remove trailing NUL bytes and you have GNU sed, you might try:
This might run into a problem if a large disk's data exceeds some internal limit of sed. While GNU sed has no built-in limit on data size, the GNU sed manual explains that system memory limitations may prevent processing of large files: