13

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?

5
  • No, it is for error correction.
    – Tim
    Aug 26, 2014 at 12:58
  • The _ is a typo I hope?
    – Rinzwind
    Aug 26, 2014 at 13:06
  • @Rinzwind yeah! that could be interesting - pick a random character for each? :p
    – Tim
    Aug 26, 2014 at 13:07
  • @Tim: Well, to generate a random "_" or "-" you can printf "%s\n" "$(cat /dev/urandom | tr -dc '_-' | fold -w 1 | head -n 1 )" :-P (and be warned that is a can of worms, because echo - is one of this corner cases...)
    – Rmano
    Aug 27, 2014 at 8:38
  • 1
    @Rmano I'll stick with - I think :P
    – Tim
    Aug 27, 2014 at 8:39

4 Answers 4

22

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}
1
22

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

1
  • 1
    What if I wanted every prime number to be a _ and every non prime to be -? 1 is not prime, and you pick for 0! :P Jokes!
    – Tim
    Aug 27, 2014 at 18:07
7

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.

4

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)
3
  • Yeah, I tried with python earlier, but didn't like the inelegance... I typically go for python :)
    – Tim
    Aug 26, 2014 at 14:00
  • @Tim It must be the font of the code on this site that hinders the prettyness of the python code :) Aug 26, 2014 at 14:22
  • 1
    There are several ways to make this.
    – Braiam
    Aug 27, 2014 at 11:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .