user7719034
user7719034

Reputation:

Bash Script to copy n number of files and rename sequentially

I have a template file that I use to submit PBS jobs to a server. I need to make n number of copies of this template. Ultimately I would like to enter the following command or something similar:

copy n pbs_template

I would like the newly made duplicate files to be named:

pbs_template_1
pbs_template_2
pbs_template_3
pbs_template_4
pbs_template_n....

The following is what i have so far...

function copy() {
    INPUT=pbs_template
    number=$1
    shift

    for n in $(seq $number); do
     cp "$INPUT" "$INPUT"
    done
}

Obviously I need to specify the name of my output (otherwise I get the following error cp: pbs_template and pbs_template are identical (not copied)), but how do I make them number sequentially n times?

Upvotes: 0

Views: 1549

Answers (2)

Prem Joshi
Prem Joshi

Reputation: 139

just change your statement to this,

cp -p "$INPUT" "$INPUT"_${n}

here i have used -p switch in order to preserve attributes of file. If you don't want then just ignore -p switch.

Hope that helps.

Upvotes: 0

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72845

Try something like this?

function duplicate () {
    for i in $(seq 1 $2)
    do
        cp $1 "$1_$i"
        echo "Made copy '$1_$i'"
    done

}

You call it like so duplicate foo.txt 10. It will create 10 copies of foo.txt each with a numerical suffix. In this invocation, $1 will be foo.txt and $2 will be 10.

Upvotes: 2

Related Questions