31

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 to a file by using standard command line tools (e.g, by copying from /dev/zero)?

4
  • 1
    This question appears to be off-topic because it is about programming, which belongs in Stack Overflow.
    – Jenny D
    Oct 29, 2013 at 8:48
  • Does oflag=append work for you? Jan 29, 2014 at 18:05
  • To clarify a comment by @Mark K Cowan; he means the dd command. Nov 17, 2016 at 9:02
  • FYI seems like fallocate is another oprion to insert holes into a file. superuser.com/a/1172904/111432 Dec 19, 2018 at 19:39

6 Answers 6

47

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

 truncate -s +10 file.txt 
1
  • 4
    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, 2011 at 23:42
38

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.

0
7

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                                          |==.|
00000413

[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                                          |...|
00a00413
5

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.

Regards,

0
2
cat "your file" /dev/zero | head -c "total number of bytes"

or

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"
0

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);
   close(nfd);
   return 0;
}

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