How can I create a new file and fill it with 1 Gigabyte worth of random data? I need this to test some software.
I would prefer to use /dev/random
or /dev/urandom
.
How can I create a new file and fill it with 1 Gigabyte worth of random data? I need this to test some software.
I would prefer to use /dev/random
or /dev/urandom
.
On most unices:
head -c 1G </dev/urandom >myfile
If your head
doesn't understand the G
suffix you can specify the size in bytes:
head -c 1073741824 </dev/urandom >myfile
If your head
doesn't understand the -c
option (it's common but not POSIX; you probably have OpenBSD):
dd bs=1024 count=1048576 </dev/urandom >myfile
Assuming that pseudo-random data is sufficient, dd if=/dev/urandom of=target-file bs=1M count=1000
will do what you want.
dd(1) will read blocks of data from an input file and write them to an output file. The command line language is a little quirky, but it is one of those really useful tools worth mastering the basics of.
In this case if
is input file, of
is output file, bs
is "block size" - and I used the GNU extension to set the size more conveniently. (You can also use 1048576 if your dd
doesn't have GNU extension.) count
is the number of blocks to read from if
and write to of
.
/dev/urandom
is a better choice than /dev/random
becuase, on Linux, it will fall back to strong pseudo-random data rather than blocking when genuinely random data is exhausted.
You may also want to look at http://www.random.org/ as another path to getting some random data without having to generate it yourself.
1M
are not available in every standard dd
variant. If your version of dd
is affected, use bs=1048576
.
– Chris Down
Mar 07 '12 at 00:39
1M==1024*1024
, use 1MB and count=1000
if you want 1 GB or 1M and count=1024
if you want 1 GiB. Or use 1GB or 1G and count=1
.
– 12431234123412341234123
Sep 07 '20 at 16:50
An alternative to using /dev/random/
and/or /dev/urandom
with an excellent Cryptographically Secure PseudoRandom Number Generator (CSPRNG) is given by openssl. You can even choose the algorithm and encrypting method in such rare cases that that is needed:
size=$( echo 1G | numfmt --from=iec ) # 1 G ==> 1073741824
openssl rand -out myfile "$size"
If you need base64:
openssl rand -base64 -out myfile "$size"
Read man rand
for the details.
while true;do head /dev/urandom | tr -dc A-Za-z0-9;done | head -c 5000K | tee 5000kb
Used this to generate 5MB random character data. If you need different size, change the -c
value of head, change the outfile name, execute and wait until the execution completes.
The solution doesn't use random but I use man pages to create sizable data. This does not however guarantee the max size so we might have to loop through a few cat
s to get the desired size,
man curl > /tmp/man-curl
Or if the size required is larger,
for i in {1..10}; do man curl >> /tmp/man-curl; done
The UNIX tool jot
can do exactly that. No need to pipe data from /dev/urandom
into a file with dd
.
jot -r -c $FILESIZE > $FILE
The flag -r
generates random data based on arc4random(3)
, -c
generates a char based on the ASCII table. Other data can be specified based on printf
format types with the -w
flag.
By default jot
returns to stdout.
Edit:
Another use case of jot
could be to generate random passwords:
$ jot -rcs '\'\'' 128 33 126
wFv/Fb7F}iO'|!cGy>?8yyez2$f,vy5vYqa;?s$-wBlVU^QX0|y~M.VeRq7}Jh!l/0'CcveuLTL]_1@P_yV#P)h8VZA\~,s{|69P{YwlL?;U{_yC3$\m*:H2V66RpdJ^
$ jot -rcs '\'\'' 128 33 126
X`&kh3[Z2'ej)w`7z'YXl~VG_MA@^jk6+STuB@.C/i\u)&g1|Y}%8;r>o>e5$UcyX]Q(F=XH6C@-BOTQ!<}CqI1Ff],7#;kb!GpZ^Ai85NaA,ya|YF+EuH]{FUgC0G7;
cargo install jot
?
– Pysis
Jul 17 '23 at 15:13
head
can read/dev/urandom
, buttail
cannot. – Stefan Lasiewski Mar 12 '12 at 23:24tail
first tries to go to the end of the input file, which takes forever (literally). – Gilles 'SO- stop being evil' Mar 12 '12 at 23:25/dev/zero
as well, if you don't like variety. – Gilles 'SO- stop being evil' Mar 12 '12 at 23:55head
don’t understand G,$((1<<30))
in Bash is easier to remember. – Franklin Yu Aug 01 '20 at 22:20<
file redirect, this works:head -c 1G /dev/urandom >myfile
– agc Aug 28 '20 at 12:41/dev/urandom
tohead
for it to open it as the shell can open it by itself with< /dev/urandom
, saving that extra effort. – Stéphane Chazelas Oct 25 '20 at 17:25