Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a script that writes to a few files but I need them a specific size. So I'm wondering if there's a way of appending a specific number of null bytes from /dev/zero or however to a file?

share|improve this question

closed as off-topic by Jenny D, voretaq7 Oct 29 '13 at 15:24

  • This question does not appear to be about server, networking, or related infrastructure administration within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

This question appears to be off-topic because it is about programming, which belongs in Stack Overflow. –  Jenny D Oct 29 '13 at 8:48
Does oflag=append work for you? –  Mark K Cowan Jan 29 '14 at 18:05

6 Answers 6

Below is an example of appending 10MB to a file using only dd.

[root@rhel ~]# cp /etc/motd ./test
[root@rhel ~]# hexdump -C test |tail -5
000003e0  0a 0a 3d 3d 3d 3d 3e 20  54 65 78 74 20 6f 66 20  |..====> Text of |
000003f0  74 68 69 73 20 6d 65 73  73 61 67 65 20 69 73 20  |this message is |
00000400  69 6e 20 2f 65 74 63 2f  6d 6f 74 64 20 3c 3d 3d  |in /etc/motd <==|
00000410  3d 3d 0a                                          |==.|

[root@rhel ~]# dd if=/dev/zero of=/root/test ibs=1M count=10 obs=1M oflag=append conv=notrunc
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0208541 s, 503 MB/s

[root@rhel ~]# hexdump -C test |tail -5
00000410  3d 3d 0a 00 00 00 00 00  00 00 00 00 00 00 00 00  |==..............|
00000420  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00a00410  00 00 00                                          |...|
share|improve this answer

If you are padding your file with null bytes, my guess is that you are manipulating the file in a char * in C. If this is the case, you might not need to pad the file with null bytes, only adding a null byte at the end of the file and then padding it with random bytes could be enough. In this case, the C program bellow would be very efficient (to be used only on files smaller than the 2nd parameter, otherwise data would be overwritten). It might even do what you want (padding with null bytes) as the lseek function definition states that:

The lseek() function shall allow the file offset to be set beyond the end of the existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap.

In this case, the 1st call to lseek and write could be removed. But tests should be done on your system 1st...

#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

/* 1st parameter: a file name, 2nd parameter: a file size. */
int main(int argc, char ** args) {
   int nfd = open(args[1], O_WRONLY);
   lseek(nfd, 0, SEEK_END);
   write(nfd, "\0", 1);
   lseek(nfd, atoi(args[2]) - 1, SEEK_SET);
   write(nfd, "\0", 1);
   return 0;
share|improve this answer
cat "your file" /dev/zero | head -c "total number of bytes"


head -c "number of bytes to add" /dev/zero >> "your_file"

and to compute the size more easily:

head -c $(( "total number of bytes" - $(stat -c "%s" "your_file") )) /dev/zero >> "your_file"
share|improve this answer

truncate is more faster than dd. To grow the file with 10 bytes use:

 truncate -s +10 file.txt 
share|improve this answer
Its much faster because it'll generate a sparse file, which is what you want most of the time—but if you don't want a sparse file, the dd approach will work. –  derobert Dec 26 '11 at 23:42

You can try this as well

dd if=/dev/zero bs=1 count=NUMBER >> yourfile

This will read from /dev/zero and append to yourfile NUMBER bytes.

share|improve this answer

my first guess would be:

dd if=/dev/zero of=myfile bs=1 count=nb_of_bytes seek=$(stat -c%s myfile)

Basically, this command tells dd to "go" at the end of the file and add some bytes previously read from /dev/zero.


share|improve this answer

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