# Clone yourself!

You are to create a program that creates exact clones of itself infinitely until stopped. Whatever is in the original program must be in the clones. In other words, the clones and original program are the same in every way except for that the clones don't have to be in the same type of file as the source code (they can be text files).

Example:

If my original program is:

for i in range(0, 10):
print i


the clone must also be:

for i in range(0, 10):
print i


Rules and Clarifications:

• Standard loopholes are forbidden

• The clones must be the exact copy of the original

• The clones must be readable files that can be run if put inside the correct interpreter

• The program can read its own source code

• All clones must be separate files

• Printing your program out is not allowed

• File names count into the number of bytes

• The clones don't have to be in the same directory as the source file nor share the same file name

• At least 1000 clones should be able to be created

Winning:

Least bytes wins!

# Zsh, 1917 9 bytes

#!/bin/zsh
<$0>$$;$0


Per consensus on meta, the shebang is excluded from the byte count.

Try it online!

Note that TIO's forkbomb protection will kill the process after 113 files have been generated. It can easily generate 1000 files on a system without such conservative resource limits.

• I like yes <$0|split -1 a lot better, but that doubles the byte count... Dec 27, 2016 at 20:10 • Too bad the in-out redirection like that does not work well in bash :( For one moment, I naïvely thought to have outgolfed you :) Dec 27, 2016 at 20:27 ## Batch, 32 bytes set/an=%1+1 copy %0 %n% %0 %n%  Not using @ because there's no restriction on STDOUT. %1 defaults to the empty string, so n becomes 1 the first time and increments on every pass. Alternatively, this might work for 28 bytes, but I have no idea how random %random% actually is when used like this: copy %0 %random%%random% %0  • IIRC %random% is based off the current time and can generate anything from 0 to something slightly above 32,000. Dec 27, 2016 at 20:08 • To clarify, %random% is a variable that, when accessed, yields a random number as @user2428118 mentioned. Dec 27, 2016 at 20:51 # Bash, 25, 16, 12, 11 bytes EDITS: • Removed the newline (-1 byte), Added "Try It Online". Thanks @Dennis ! • Use background job PID $! as the filename (will be reused every ~32k files, but that is now allowed), -4 bytes

Golfed

#!/bin/bash
$0&cp$0 $!  Explained Re-spawns itself as a background job with &, before doing a copy, so each iteration will run under its own PID. Uses the last job PID as the filename. This can run infinitely (or until stopped) but will be reusing clone filenames approx. every ~32k iterations. Could be a bit nasty to kill, but AFAIK is not against the rules. Try It Online! • Do you have a testing place where I can try this out for myself? Dec 27, 2016 at 17:22 • @PythonMaster, I will normally use Tutorial Point's online testbed, but it does not have uuid installed, and does not like the script doing so much IO. Dec 27, 2016 at 18:39 • TIO doesn't have a problem with I/O. Also, you don't need the newline after &. tio.run/nexus/bash#VY5BS8NAEIXP7q943W5tq6QpelMRRCIIGg/… Dec 27, 2016 at 20:37 ## Ruby, 78 bytes, 77 43 + 4 (filename: a.rb) = 47 bytes • Version 1.2 43 bytes + name / Thanks to @Alexis So much shorter!!! loop{File.write "#{$.+=1}",open("a.rb",?r)}


that's about as golfed as I think it's going to get

• Version 1.1

73 bytes + name / Thanks to @Alexis Anderson

i=0
loop do
i+=1
end

• Version 1.0

74 bytes + name

i=0
loop do
i+=1
end


This is my first Ruby answer so all inprovements are welcome.

• kernel open forwards to file open, you should be able to omit the File. altogether. ?w in place of "w". I think the do on the File.open is useless, since you have the curly braces. Dec 27, 2016 at 20:49
• open('a.rb',?r) in place of File.read('a.rb') Dec 27, 2016 at 20:57
• ?w is a character w. A string in it's own right. ?w == 'w'. so you don't need the quotes in "?w" Dec 27, 2016 at 20:59
• i=0 loop do File.write "#{i}",open("a.rb",?r) i+=1 end Dec 27, 2016 at 21:04
• actually, you can also use curly braces for the loop method too Dec 27, 2016 at 21:07

# Processing, 55 + 5 (name of file) = 60 bytes

I don't know if the filename counts as extra bytes

for(int i=0;;)saveStrings(i+++"",loadStrings("a.pde"));


Pretty much a series of builtins all chained together

### Explanation

for(int i=0;;)           //A complex piece of code that cannot be explained in words alone
saveStrings(             //Save param2 (String[]) into file param1 (String)
i+++"",                 // filename: the int i (and then is incremented)
loadStrings("a.pde")    // content: load the content of file a.pde as a String[]
);

• That "complex code" is smiling Dec 27, 2016 at 20:04

# ForceLang + the ForceLang-JS module, 162 bytes

set j require njs
j var a=function(){for(;;){var p=new java.io.PrintStream(Math.random()+".txt");p.println("set j require njs");p.print("j var a="+a+";a()")}};a()


## Python 2, 54 bytes

from shutil import*
a=__file__
whilecopy(a,a*2):a*=2

• Don't you have to import sys? Dec 27, 2016 at 21:18
• @SamuelShifterovich I would have had. I'm not sure how that edit survived as the byte count I was using was for the program now shown
– Blue
Dec 27, 2016 at 21:52

For[a=0,1>0,$Input~CopyFile~ToString@a++]  Full program. Copies its own source file into 0, 1, 2, etc. in the current directory. Python 2, 61 bytes (unix) 65 (cross-platform) Unix - 61 bytes import os;i=1 while os.popen('cp %s %i.py'%(__file__,i)):i+=1  On unix cp is the systems copy-file - command. Using the console via popen allows me to copy the file by cp. new files will spawn in old-files directory. CrossPlatform - 65 bytes i=0 while 1:i+=1;open('%i.py'%i,'w').write(open(__file__).read())  This works, as open on default allows reading. Funfact: replace 1:i+=1 with i:i-=1 and it will stop at i copies. Other than that, I see no way of making it shorter than @muddyfish did it. # PHP, 91 60 bytes Saved 31 bytes thanks to manatwork <?for(;;)file_put_contents(++$i,file_get_contents("f.php"));


Usage: $php f.php clones f.php and its code reproducing infinitely itself in filenames like 1, 2, 3... until timeout. Previous version: <?for($i=0;;$i++)$p=fwrite(fopen("$i.php","w"),fread(fopen($f="f.php","r"),filesize($f)));  Usage: $ php f.php clones f.php and its code reproducing infinitely itself like f1.php, f2.php, f3.php ... f(n).php until timeout.

• As the clone file name can be anything, why not use just numbers without extension? <?for(;;)file_put_contents(++$i,file_get_contents("f.php")); Dec 28, 2016 at 9:06 ## awk, 29 (21) bytes, {while(close(i++)||1)print>i}  awk is not really the tool for this as it requires close() when running indefinitely. Otherwise all it produces are empty files. If the clone count had a limit, for example 5: {while(++i<5)print>i}  the program would be 21 bytes. Paste the program to a file a and run: $ awk -f a a


# Python, 69 bytes

a=open(__file__).read()
i=0
while 1:i+=1;open(i+'.py','w').write(a)


I tried using a for the file name, but (unsurprisingly) Windows does not like files named a=open(__file__).read()\ni=0\nwhile 1:i+=1;open(i+'.py','w').write(a). I also tried this:

a=open(__file__).read()
i=''
while 1:i+='.py';open(i,'w').write(a)


However it gives me this error:

Traceback (most recent call last):
File "C:/Python27/codeGolfCopy.py", line 3, in <module>
while 1:i+='.py';open(i,'w').write(a)
IOError: [Errno 2] No such file or directory: '.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py'


It creates 83 files, but that is not even close to the needed 1000.

If you would like to remove these files, you can use this:

import os;i = 1
while 1:os.remove(i+'.py');i+=1


## RBX.Lua, 14 bytes

IS THIS EVEN A FILESYSTEM I DON'T KNOW

script:Clone()


Clones itself. As it gets instanced, in runs again, therefore providing automatic recursion.

# JavaScript ES6 34 bytes

Not sure if this counts, but here:

_=()=>alert("_="+_+";_()")&_();_()

# C, 80 bytes

main(a){while(1)printf(a="main(a){while(1)printf(a=%c%s%c,34,a,34);}",34,a,34);}


Prints itself until you kill it. Just a standard C quine with an infinite loop.