-1

I have over 1,000 files that I will like to change one character on the filename, Ex: GM001001, GM001002, GM001003, etc.. to be rename to GX001001, GX001002, GX001003, etc... As you can see the common denominator will be the M to be replace for an X.

  • Sorry, voting-to-close because ***"Seeking recommendations for books, tools, software libraries, and more"***.... StackOverflow is dedicated to helping solve programming code problems. Your Q **may be** more appropriate for [su] , but read their help section regarding on-topic questions . AND please read [Help On-topic](https://stackoverflow.com/Help/On-topic) and [Help How-to-ask](https://stackoverflow.com/Help/How-to-ask) before posting more Qs here. Good luck. – shellter Aug 11 '20 at 21:41
  • Some systems have a Perl-based `rename` (occasionally `prename`) command. You'd write: `rename s/GM/GX/ GM[0-9][0-9][0-9][0-9][0-9][0-9]` to rename all the files starting with the letters GM and continuing with six digits. See also [Usiing `sed` to mass rename files](https://stackoverflow.com/q/2372719/15168) where the accepted answer advocates for using `rename` instead. – Jonathan Leffler Aug 12 '20 at 00:40

1 Answers1

0

You can combine mv with string replace to achieve this:

for f in $(ls)
do
  mv $f ${f/GM/GX}
done
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kassian Sun
  • 609
  • 4
  • 8
  • 1
    Note that using `for f in $(ls)` is generally not a good idea. A better choice would be a glob such as `for f in GM[0-9]*` which will handle spaces etc in the file names correctly (unlike the version using `ls`). Granted, the names are probably simple enough that it isn't an issue. The `ls` notation also would list files not starting `GM`. The `[0-9]` bit is a sop in the direction of ensuring that files names have the right format. Using `GM[0-9][0-9][0-9][0-9][0-9][0-9]` would be far more verbose but more reliable for the six-digit numbers shown in thee question. – Jonathan Leffler Aug 12 '20 at 00:39
  • Perfect, It worked fine. Thank you! – Patron0119 Aug 13 '20 at 04:12