Skip to content
Advertisement

Batch copy and rename multiple files in the same directory

I have 20 files like:

JavaScript

Files have a similar format in their names. They begin with 01, and they have 01*AAA*.sh format.

I wish to copy and rename files in the same directory, changing the number 01 to 02, 03, 04, and 05:

JavaScript

I wish to copy 20 of 01*.sh files to 02*.sh, 03*.sh, and 04*.sh. This will make the total number of files to 100 in the folder.

I’m really not sure how can I achieve this. I was trying to use for loop in the bash script. But not even sure what should I need to select as a for loop index.

JavaScript

does not work.

Advertisement

Answer

There are going to be a lot of ways to slice-n-dice this one …

One idea using a for loop, printf + brace expansion, and xargs:

JavaScript

The same thing but saving the printf in a variable up front:

JavaScript

Another idea using a pair of for loops:

JavaScript

Starting with:

JavaScript

All of the proposed code snippets leave us with:

JavaScript

NOTE: blank lines added for readability

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement