copy and rename series of files which differ in extension within the same directory using terminal

I have a series of files which differ in their extensions, eg apple.txt, apple.tpf, apple.jpg, apple.png


I'm able to copy and rename these with:


for file in * ; do cp $file ${file//apple/orange} ; done


such that the directory now contains apple.txt, apple.tpf, apple.jpg, apple.png, orange.txt, orange.tpf, orange.jpg, orange.png


But what I'd like to be able to do is take a series of files which differ in their extensions (the initial files won't always be apple.*) and easily generate a number of copies (will vary depending upon circumstance) with different file names, eg:


One time, apple.* to orange.*, as summarized above

Another time, car.* to bus.* and also to train.*

Another time, grass.* to tree.*, grass.* to shrub.*, grass.* to hay.*


Is there a way to set this up, accounting for the generality of the initial file name, allowing for selection of the copied file name(s), and allowing this to be easily executed without having to edit and re-enter


for file in * ; do cp $file ${file//apple/orange} ; done


for all necessary permutations?


Formerly on PC, I was able to easily do this with


copy apple.* orange.*
copy car.* bus.*
copy car.* train.*
etc. 


Looking for the OSX equivalent.


Thanks in advance for any assistance offered.

MacBook Pro (2017 – 2020)

Posted on Jun 6, 2023 11:45 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 6, 2023 12:41 PM

Assuming zsh, which is the Apple-preferred shell...

autoload zmv  # needed once per session, and usually placed in a zsh login script like .zshrc
zmv -CW 'x.*' 'y.*'


From bash, it's possible to "borrow" zsh and do this all in one:

zsh -c "autoload zmv; zmv -CW 'x.*' 'y.*'"


Similar questions

2 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

copy and rename series of files which differ in extension within the same directory using terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.