Unix & Linux: Duplicate file x times in command shell (5 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-15

Unix & Linux: Duplicate file x times in command shell


The Question: 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

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 13 people ==
for i in {1..100}; do cp test.ogg "test_$i.ogg" ; done

== This solution helped 12 people ==
Short and precise
＜ test.ogg tee test{1..100}.ogg
or even better do
tee test{1..100}.ogg ＜ test.ogg ＞/dev/null
see http://linux.101hacks.com/unix/tee-command-examples/ for more help.
Update
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

== This solution helped 29 people ==
Your shell code has two issues:
 1. The echo should not be there.
 2. The variable $i is mistyped as $1 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 insert anything else in there, 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, 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
i=0
while (( i++ ＜ 100 )); do
cp test.ogg "test$i.ogg"
done
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.

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user Rahul (https://unix.stackexchange.com/users/70697), user Praveen Kumar BS (https://unix.stackexchange.com/users/259343), user Kusalananda (https://unix.stackexchange.com/users/116858), user Gilles 'SO- stop being evil' (https://unix.stackexchange.com/users/885), user cy8g3n (https://unix.stackexchange.com/users/270618), user coffeMug (https://unix.stackexchange.com/users/28650), user Black (https://unix.stackexchange.com/users/124191), and the Stack Exchange Network (http://unix.stackexchange.com/questions/291065). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.