I have a single file (a PDF) and I want to have lots of identical copies in the same folder (200 would be nice) named file-001
, file-002
etc.
How do I do it?
You could do something like
< file tee file-{001..200}
however if the medium becomes unreadable it will not matter how many copies are on it - fundamentally backups require diversity.
Note that tee
writes its standard input to standard output as well as to each of the given files - for large files, or for files containing binary data or other special characters that may interfere with your terminal settings, you will probably want to dump standard output to the bit bucket
< file > /dev/null tee file-{001..200}
tee file-{001..200} < file >/dev/null
looks for me more legibly. See this almost duplicate answer: How to copy a file to multiple folders in Terminal?
Aug 26, 2014 at 18:35
This is the classic case where shell tricks help a lot.
for i in {000..199}; do cp file file-$i; done
And I know it's a joke, but if you want a random _
or -
separating the number from the name you can use:
for i in {000..199}; do
cp file file$(cat /dev/urandom | tr -dc '_-' | fold -w 1 | head -n 1 )$i;
done
(multiple line to help readability...)
:-P
_
and every non prime to be -
? 1 is not prime, and you pick for 0! :P Jokes!
To make a single duplicate of a file you probably know that you can use cp
:
cp file file-001
Now, to make more duplicates to a file, you can combine cp
with xargs
. In your case:
echo file-{001..200} | xargs -n 1 cp file
will copy file
to file-001
, file-002
,... ,file-200
. See man xargs
for more info.
As always, the python truck comes late, but:
make it executable, drag it over a terminal window, drag the file to copy over the terminal window and set the number of copies:
script file number_ofcopies
The number of leading zeros is set automatically, the files are named file_001.pdf
, file_002.pdf
, with the filenumbers placed before the extension.
The script:
#!/usr/bin/env python3
import sys
import shutil
orig = sys.argv[1]; n = sys.argv[2]; size = len(str(n)); out = (orig[:orig.rfind(".")], orig[orig.rfind("."):])
for item in [out[0]+"_"+(size-len(str(item)))*"0"+str(item)+out[1] for item in range(1, int(n)+1)]:
shutil.copyfile(orig, item)
printf "%s\n" "$(cat /dev/urandom | tr -dc '_-' | fold -w 1 | head -n 1 )"
:-P (and be warned that is a can of worms, becauseecho -
is one of this corner cases...)-
I think :P