How to copy a certain file several times with a regular ending?

18,599

Solution 1

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}

Solution 2

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

Solution 3

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.

Solution 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)
Share:
18,599

Related videos on Youtube

Tim
Author by

Tim

My name is Tim. I've graduated from the University of Nottingham with a First Class Computer Science BSc with Hons. In my spare time I do computer programming, often C or JavaScript, but also shell scripts, and answering questions on Stack Exchange. I used to spend most of my time on Ask Ubuntu; now I mostly browse the HNQ or Meta Stack Exchange. If you want to contact me it's [email protected] Do you need a reliable VPS? Try Digital Ocean. Sign up with this link for $10 free. One of my favourite sites is Kiva.org, a micro-funding organisation that allows people to lend money via the Internet to low-income entrepreneurs and students in over 80 countries. Kiva's mission is “to connect people through lending to alleviate poverty.” With just $25 you can make a difference - and 99% of loans are paid back in full - so you can lend again and again!

Updated on September 18, 2022

Comments

  • Tim
    Tim over 1 year

    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?

    • Rinzwind
      Rinzwind over 9 years
      The _ is a typo I hope?
    • Tim
      Tim over 9 years
      @Rinzwind yeah! that could be interesting - pick a random character for each? :p
    • Rmano
      Rmano over 9 years
      @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...)
    • Tim
      Tim over 9 years
      @Rmano I'll stick with - I think :P
  • Tim
    Tim over 9 years
    Yeah, I tried with python earlier, but didn't like the inelegance... I typically go for python :)
  • Jacob Vlijm
    Jacob Vlijm over 9 years
    @Tim It must be the font of the code on this site that hinders the prettyness of the python code :)
  • Radu Rădeanu
    Radu Rădeanu over 9 years
    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?
  • Braiam
    Braiam over 9 years
    There are several ways to make this.
  • Tim
    Tim over 9 years
    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!