3

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!

Community
  • 1
  • 1
user8958659
  • 31
  • 1
  • 2
  • There is no standard tool or anything built in to `bash` that will do this. There are various implementations of `rename` commands available that can, however. – chepner Nov 17 '17 at 16:13

1 Answers1

10

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*
Spangen
  • 4,420
  • 5
  • 37
  • 42