3

Say, if I wanted to make N copies of a.txt, and wanted them to be numbered a1.txt, a2.txt, a3.txt, etc...

How would I do such a thing succinctly?

4 Answers 4

7

cp itself can only make one copy (of a single file) at a time, but it's not too difficult with a quick loop:

for i in {1..10}; do cp a.txt a$i.txt; done
3

Assuming you have your N value in a bash variable:

for i in `seq 1 $N`; do cp a.txt a$i.txt; done

or

for ((i=1;i<=$N;i++)); do cp a.txt a$i.txt; done

or with the loop shown in Kevin's answer.

2

Solutions using cp are somewhat inefficient, because you don't need to read the same file N times to make N copies. One can use tee instead:

cat a.txt | ( for i in `seq 1 $N`; do echo a"$i".txt; done | xargs tee )
0

If you want something more funky, you can for example do: for t in $(seq --format="funky-%03.0f-numbered.txt" 25 3 40); do cp orig $t; done.

You must log in to answer this question.

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