How to pad a file with "FF" using dd?
How to pad a file with 0xFF
using dd
?
This command will pad the output file with zeroes until the file size reaches 100 KB:
dd if=inputFile.bin ibs=1k count=100 of=paddedFile.bin conv=sync
However, I want to pad a file with 0xFF
s instead of 0x00
s.
As far as I know there is no way to tell dd
to pad using 0xFF
. But there is a workaround.
First create a file with the required length filled with 0xFF
:
$ dd if=/dev/zero ibs=1k count=100 | tr "\000" "\377" >paddedFile.bin
100+0 records in
200+0 records out
102400 bytes (102 kB) copied, 0,0114595 s, 8,9 MB/s
tr
is used to replace zeroes with 0xFF
. tr
expects arguments in octal. 0xFF
in octal is \377
.
Result:
$ hexdump -C paddedFile.bin
00000000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................|
*
00019000
Then insert the input file at the beginning of the "padded" file:
$ dd if=inputFile.bin of=paddedFile.bin conv=notrunc
0+1 records in
0+1 records out
8 bytes (8 B) copied, 7,4311e-05 s, 108 kB/s
Note the conv=notrunc
which tells dd
to not truncate the output file.
Example input file:
$ hexdump -C inputFile.bin
00000000 66 6f 6f 0a 62 61 72 0a |foo.bar.|
00000008
Result:
$ hexdump -C paddedFile.bin
00000000 66 6f 6f 0a 62 61 72 0a ff ff ff ff ff ff ff ff |foo.bar.........|
00000010 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................|
*
00019000
A possible improvement on lesmana's answer is to operate on the file in-place. This could be a lot faster for large input files and will also keep sparse files sparse. However, in many situations you don't want to modify your input file, and so this method will be unsuitable.
The following example starts with a large, sparse input file and pads it up to a size of 1GB with FF chars. Simply change newsize
to your desired value. As you can see, the dd
portion takes only a fraction of a second despite this file being very large.
$ ls -ld inputFile.bin
-rw-rw-r-- 1 … 1073741700 … inputFile.bin
$ hexdump inputFile.bin
0000000 0000 0000 0000 0000 0000 0000 0000 0000
*
3fffff80 0000 0000
3fffff84
$ newsize=$((1024 * 1024 * 1024))
$ filesize=$(stat -c "%s" inputFile.bin)
$ padcount=$((newsize - filesize))
$ dd if=/dev/zero ibs=1 count="$padcount" | tr "\000" "\377" >> inputFile.bin
124+0 records in
0+1 records out
124 bytes (124 B) copied, 0.000162309 s, 764 kB/s
$ ls -ld inputFile.bin
-rw-rw-r-- 1 … 1073741824 … inputFile.bin
$ hexdump inputFile.bin
0000000 0000 0000 0000 0000 0000 0000 0000 0000
*
3fffff80 0000 0000 ffff ffff ffff ffff ffff ffff
3fffff90 ffff ffff ffff ffff ffff ffff ffff ffff
*
40000000