-1

I'd like to copy and rename multiple files within the same folder. Just like I have the files foo.c foo.h and want to use them as a template for new files named bar.c bar.h.

cp foo.* bar.*

would describe what I mean but won't work.

using rename will just overwrite the old files.

Is there some simple solution for this or do I have to create a whole script that opens a folder in /tmp, copy there, rename there and move back?

  • 3
    Possible duplicate of [Rename multiple files, but only rename part of the filename in Bash](https://stackoverflow.com/q/20657432/608639), [Rename multiple files while keeping the same extension on Linux](https://stackoverflow.com/q/26178469/608639), [Rename file while keeping the extension in Linux?](https://stackoverflow.com/q/46264427/608639), etc. – jww Nov 29 '19 at 12:59

2 Answers2

0

I just found the answer myself with that wonderful tool mcp

mcp 'foo.*' 'bar.#1'
0

You can do it with a simple for loop and some string manipulation

#!/bin/bash
# for each file following the pattern "foo."
for i in foo.* 
do 
    # copy file to "bar" + original extension
    cp $i bar.${i#foo.} 
done
Mathieu
  • 8,840
  • 7
  • 32
  • 45