Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming Multiple Files on macOS Terminal [duplicate]

Is it possible to rename multiple files that share a similar name but are different types of files all at once?

Example:

apple.png

apple.pdf

apple.jpg

Can I substitute the apple for something else, for example "pear"? If this is possible, what would the command be? Many thanks for your time!

like image 672
user8958659 Avatar asked Aug 31 '25 03:08

user8958659


1 Answers

You can do this in bash natively by looping over the files beginning apple and renaming each one in turn using bash parameter expansion

$ for f in apple*; do mv "$f" "${f/apple/pear}"; done

The for f in apple* finds all files matching the wildcard. Each filename is then assigned to the variable f For each assignment to f bash calls the command mv to move (rename) the file from it's existing name to one where apple is replaced by pear

You could also install rename using a package manager like Homebrew and call

rename -e 's/apple/pear/' apple*
like image 189
Spangen Avatar answered Sep 02 '25 18:09

Spangen