Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to create a new file with a specific number of zero's

Tags:

linux

bash

I have a directory tree that in each directory there is a file called zero-file
I want this file to be initialized with specific number of zero's for example 10000000000.
I want to write ASCII character '0'
I found 2 solutions for this

for i in `seq 1 10000000000`
do
    echo -n 0 >> zero-file
done 

The second one is this: I initialized a file called base and the copy this file in each directory

cp base zero-file

This was faster. I want to know if there exist a faster way.

like image 445
vida 1 Avatar asked Dec 31 '25 05:12

vida 1


2 Answers

Try using /dev/zero as a virtual input file that contains only zeros. You can copy a fixed number of bytes with dd:

dd if=/dev/zero of=zero-file bs=1 count=10000000000

This copies one byte 10000000000 times from /dev/zero to zero-file. You can adjust the blocksize and count as needed.

like image 155
e0k Avatar answered Jan 01 '26 21:01

e0k


To write zeros to zero-file:

dd if=/dev/zero bs=1 count=10000000000 | tr '\0' '0' > zero-file
like image 33
Cyrus Avatar answered Jan 01 '26 20:01

Cyrus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!