Skip to main content
4 of 5
Rename or copy?
Greenonline
  • 2.1k
  • 7
  • 23
  • 32

This command would seem to do what you wish

for file in *.* ; do cp $file ${file//a\./b\.} ; done

Adapted from this answer to Renaming part of a filename

Actually, you could just do

for file in * ; do cp $file ${file//a\./b\.} ; done

Note that the . has to be escaped using \.. This is primarily because your example is a filename of a single letter, which could potentially conflict with a letter in the extension.

If your filenames are a bit more explicit, such as doggy.txt, doggy.pdf, doggy.xls, etc., and you want to change them to feline.txt, feline.pdf, ..., etc., then you could do away with escaping the . (and the . all together), like so:

for file in *.* ; do cp $file ${file//doggy/feline} ; done

Note the commands above will create a copy of the files, i.e.:

% ls
a.pdf   a.txt   a.xls   b.pdf   b.txt   b.xls

If you wish to just rename them, then use mv instead:

for file in * ; do mv $file ${file//a\./b\.} ; done

or

for file in *.* ; do mv $file ${file//doggy/feline} ; done
Greenonline
  • 2.1k
  • 7
  • 23
  • 32