66

I have the below list of files

aro_tty-mIF-45875564pmo_opt
aro_tty-mIF-45875664pmo_opt
aro_tty-mIF-45875964pmo_opt
aro_tty-mIF-45875514pmo_opt
aro_tty-mIF-45875524pmo_opt

that I need to rename to

aro_tty-mImpFRA-45875564pmo_opt
aro_tty-mImpFRA-45875664pmo_opt
aro_tty-mImpFRA-45875964pmo_opt
aro_tty-mImpFRA-45875514pmo_opt
aro_tty-mImpFRA-45875524pmo_opt
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

3 Answers3

87

Most standard shells provide a way to do simple text substitution within shell variables. http://tldp.org/LDP/abs/html/parameter-substitution.html explains as follows:

${var/Pattern/Replacement}

First match of Pattern, within var replaced with Replacement.

So use this script to loop through all the appropriate files and rename each of them:

for file in aro_tty-mIF-*_opt
do
    mv -i "${file}" "${file/-mIF-/-mImpFRA-}"
done

I have added a -i option so you have the chance to confirm each renaming operation. As always, you should make a backup of all your files before doing any large amount of renaming or deleting.

Mark Plotnick
  • 25,413
  • 3
  • 64
  • 82
  • 1
    Strangely, the -i option doesn't prompt for y/n before renaming files on the Redhat system I am on. Anyone know why? – timbram Aug 29 '17 at 14:55
  • Does the -i option to mv have no effect even when you're typing it to the shell directly (not using find)? – Mark Plotnick Aug 29 '17 at 15:03
  • Good call. Looks like I do not have that feature on the shared Red Hat server I am using. – timbram Aug 29 '17 at 15:10
  • That's unusual. Does type mv show something other than mv is /usr/bin/mv ? Someone could have made it an alias or a function. – Mark Plotnick Aug 29 '17 at 15:15
  • no, actually. It says "mv is hashed (/bin/mv)" not sure what that means. – timbram Aug 29 '17 at 15:30
  • 1
    "Hashed" is just a feature of your shell. /bin/mv is a plausible location for mv. As for mv not honoring the -i option, that's not standard. Maybe you have an older system or someone installed a custom version of mv. – Mark Plotnick Aug 29 '17 at 15:42
  • What does this -mIF-/-mImpFRA- syntax mean, where can I find doc for it? – Yngvar Kristiansen Nov 27 '19 at 12:22
  • 2
    @YngvarKristiansen Those strings are parts of the filenames used by the original poster. They're not command options or anything like that. – Mark Plotnick Nov 27 '19 at 14:45
21

If you don't have Perl's rename:

perl -e '
FILE:for $file (@ARGV){
        ($new_name = $file) =~ s/-mIF-/-mImpFRA-/
        next FILE if -e $new_name;
        rename $file => $new_name
}' *_opt

If you do have Perl's rename:

rename 's/-mIF-/-mImpFRA-/' *_opt
Joseph R.
  • 39,549
17

Before trying complex commands like the following, backup your files. You never know what a typo (mine or yours) can cause.

With mv (as you asked in comment --- rename as suggested in the other answer is probably safer, especially if you can have spaces or strange chars in your filenames) something of the style

for f in *_opt; do
    a="$(echo $f | sed s/-mIF-/-mImpFRA-/)"
    mv "$f" "$a"
done
Rmano
  • 3,425