How to create a file from terminal repeating a set of words infinitely?
There's an easy way to repeat a line lots of times using the yes
command:
yes we have no bananas | head -n 10000 > out.txt
will result in out.txt containing 10,000 lines all saying "we have no bananas".
To limit the output to an exact number of bytes, use head
's -c
option instead of -n
. For example, this generates exactly 10 kB of text:
yes we have no bananas | head -c 10000 > out.txt
I can't recommend infinitely repeating text, but you could make a ~2GB file of repeated text with python like so...
python3 -c 'with open("bigfile", "w") as f: f.write(("hello world "*10+"\n")*2*10**7)'
That will print "hello world " 10 times and make a new line, and repeat that 20,000,000 times, writing the result to the file bigfile
. If all your chars are ASCII, then each one is one byte, so calculate appropriately depending on what you want to write...
Your cpu may be owned. I run out of RAM if I try doing more than 10,000,000 lines...
I'm running a toaster though
Perl has the nifty x
operator:
$ perl -e 'print "foo\n" x 5'
foo
foo
foo
foo
foo
So, as a simple solution, you could just write your line a few million times. For example, this command created a 3G file:
perl -e 'print "This is my line\n" x 200000000' > file
If you need to specify an exact size (2 GiB in this case), you can do:
perl -e 'use bytes; while(length($str)<2<<20){ $str.="This is my line\n"} print "$str\n"' > file