Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 0xFFs instead of 0x00s.

share|improve this question
add comment

3 Answers

up vote 13 down vote accepted

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

$ 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:

$ hexdump -C inputFile.bin 
00000000  66 6f 6f 0a 62 61 72 0a                           |foo.bar.|
00000008

$ 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

$ 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

In this dd invocation you have to use conv=notrunc so the output file does not get truncated.

share|improve this answer
    
Great answer, thank you! –  thegreendroid Dec 2 '13 at 21:30
add comment

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
share|improve this answer
add comment

i found a solution on comandlinefu, it involves tr to change the zeros to ones.

http://www.commandlinefu.com/commands/view/6483/fill-a-hard-drive-with-ones-like-zero-fill-but-the-opposite-

Good Luck

share|improve this answer
    
How do you prevent it from changing the actual data of inputFile.bin? –  Daniel Beck Apr 25 '11 at 6:28
    
I don't know, just found that command by googling for /dev/zero opposite :) –  kev Apr 25 '11 at 6:36
    
Well, it converts zeros to ones, but not only in the padding area. He doesn't end up with a lot of the data from inputFile.bin, does he? –  Daniel Beck Apr 25 '11 at 6:38
    
Right, didnt read the question very well. –  kev Apr 25 '11 at 6:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.