13

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?

4 Answers 4

12

Try this

for f in {1..5}; do cp my.jpg my$f.jpg; done

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

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

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.

5

Try tee:

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

Or parallel:

parallel cp my.jpg ::: my-{1..5}.jpg
3

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
1
  • 1
    It's not necessary to use eval. <"$pre$suf" tee "$pre"{1..5}"$suf" works just fine. Jan 18, 2012 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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