Renaming Multiple Files on macOS Terminal

Viewed 5053

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!

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*
Related