Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I can move 5 files to somewhere using:

$ mv my-{1..5}.jpg /path/to/dir/

How can I make copy of one file by 5 times easily

# doesn't work
$ cp my.jpg my-{1..5}.jpg

Is it possible not to use a for loop?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

Try this

for f in $f; do cp my.jpg my$f.jpg; done

(don't have bash here to try it myself)

share|improve this answer
5  
replace for f in $f with for f in $(seq 5). –  Dan D. Jan 18 '12 at 1:36
1  
Is it possible not to use a for loop? –  kev Jan 18 '12 at 1:38
    
@kev - I've always seen it done this way; also, more readabe. –  ldigas Jan 18 '12 at 1:48
3  
@kev, what is wrong with a for loop? This is what for loops are for... –  soandos Jan 18 '12 at 2:10
2  
@DanD.: seq isn't necessary in Bash. You can use for f in {1..5} or for ((f=1; f<=5; f++)) –  Dennis Williamson Jan 18 '12 at 15:57

Here is a way to do it without a for loop and without the risks of using eval:

printf '%s\n' {1..5} | xargs -I {} cp my.jpg my-{}.jpg

It's still effectively a loop.

share|improve this answer

You can do it without a loop.. using tee and {} brace expansion.

EDIT: (ammended as per Dennis Williamson's comment:

For a file named "my-.jpg"

pre="my-"; suf=".jpg"
<"$pre$suf" tee "$pre"{1..5}"$suf" >/dev/null
share|improve this answer
1  
It's not necessary to use eval. <"$pre$suf" tee "$pre"{1..5}"$suf" works just fine. –  Dennis Williamson Jan 18 '12 at 16:08
    
@Dennis: So it does.. thanks :) –  Peter.O Jan 18 '12 at 16:26

Try tee:

tee <my.jpg >/dev/null my-{1..5}.jpg

Or parallel:

parallel cp my.jpg ::: my-{1..5}.jpg
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.