I try to duplicate a video file x times from the command line by using a for loop, I've tried it like this, but it does not work:

for i in {1..100}; do cp test.ogg echo "test$1.ogg"; done
  • 6
    Isn't your only error the echo which should not be there, and the $1 which should be $i? Commented Jun 21, 2016 at 6:44
  • if i remove the echo and only use test$1.ogg then it says: test.ogg and test.ogg are the same files, so it seems like $1 is not recognized?
    – Black
    Commented Jun 21, 2016 at 6:45
  • @EdwardBlack: Sound like I mis-understood your requirements. That solution is not suitable.
    – cuonglm
    Commented Jun 21, 2016 at 6:47
  • 1
    @JuliePelletier, damn, it happens sometimes to me that i accidentially write $1 instead of $i, it is earlie in the morning, sorry... thank you. I will use $x in the future instead of $i
    – Black
    Commented Jun 21, 2016 at 6:49

5 Answers 5


Your shell code has two issues:

  1. The echo should not be there.
  2. The variable $i ("dollar i") is mistyped as $1 ("dollar one") in the destination file name.

To make a copy of a file in the same directory as the file itself, use

cp thefile thecopy

If you use more than two arguments, e.g.

cp thefile theotherthing thecopy

then it is assumed that you'd like to copy thefile and theotherthing into the directory called thecopy.

In your case with cp test.ogg echo "test$1.ogg", it specifically looks for a file called test.ogg and one named echo to copy to the directory test$1.ogg.

The $1 will most likely expand to an empty string. This is why, when you delete the echo from the command, you get "test.ogg and test.ogg are the same files"; the command being executed is essentially

cp test.ogg test.ogg

This is probably a mistyping.

In the end, you want something like this:

for i in {1..100}; do cp test.ogg "test$i.ogg"; done

Or, as an alternative

while (( i++ < 100 )); do
  cp test.ogg "test$i.ogg"

Or, using tee:

tee test{1..100}.ogg <test.ogg >/dev/null

Note: This would most likely work for 100 copies, but for thousands of copies it may generate a "argument list too long" error. In that case, revert to using a loop.

for i in {1..100}; do cp test.ogg "test_$i.ogg" ; done

Short and precise

< test.ogg tee test{1..100}.ogg

or even better do

tee test{1..100}.ogg < test.ogg >/dev/null

see tee command usage for more help.


as suggested by @Gilles, using tee has the defect of not preserving any file metadata. To overcome that issue, you might have to run below command after that:

cp --attributes-only --preserve Source Target
  • 3
    This is potentially faster than multiple calls to cp (depends on the file size relative to available memory), but has the defect of not preserving any file metadata. Commented Jun 21, 2016 at 9:44
  • @Gilles ohh, can we overcome that ?
    – Rahul
    Commented Jun 21, 2016 at 10:09
  • 3
    You can run cp --attributes-only afterwards. Commented Jun 21, 2016 at 11:50
  • @Gilles thanks, updated my answer as per your help.
    – Rahul
    Commented Jun 21, 2016 at 11:57
  • FWIW none of the other answers used cp -p to preserve metadata either. Commented Jun 6, 2017 at 20:03

The folowing command will copy file.a 5 times:

$ seq 5 | xargs -I AA cp file.a fileAA.a

If you prefer dd (not the same as cp!):

$ seq 5 | xargs -I AA dd if=file.a of=fileAA.a

You have not called variable i while copying

use below script . As tested it worked fine

for i in {1..10}; do cp -rvfp test.ogg test$i.ogg ;done
  • 3
    This is the same as two previous answers and a comment except (1) the question asks about 100, and your answer uses 10, (2) you have unnecessarily added the -rvf options to cp, and (3) you have failed to quote your variable expansion. (Also note that it’s conventional to have a space between punctuation symbols like ; and the following word.) The addition of the -p option is valuable, but (4a) it’s been addressed already (in comments), and (4b) your answer isn’t helpful if you don’t explain what you’re doing. Commented Jan 17, 2018 at 20:03

You must log in to answer this question.

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