Few other answers calculate how many trailing zeros you need to append, then they append. In Linux there's this simple way:
truncate -s 5MB image
(this assumes you want 5 MB, i.e. 5*1000*1000 bytes. For 5 MiB, i.e. 5*1024*1024 bytes, use -s 5M
).
If originally image
is larger than the specified size, then the extra data will be lost. If originally image
is smaller, then padding zeros will be added. Added fragment will be sparse, if possible.
If for any reason you don't want sparseness, use fallocate
:
fallocate -l 5MB image
(similarly, yet not identically: -l 5MB
for 5 MB, -l 5MiB
for 5 MiB).
fallocate
used this way can only extend image
. If the file is already at least that big then its size won't change (sparseness can change, I won't elaborate).