Renaming multiple files with rename

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite
1












I need to rename files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. I do know that they will start with NORMAL and end with -lib*. I'd like to replace everything in between with some string X. For example,



| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.translocation
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.concordant
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.deletion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.divergent
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion
| | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translocation


So it will probably be of the form rename "s/something/X/", but I don't know what that something should be, as I don't know how to use regex.










share|improve this question



























    up vote
    5
    down vote

    favorite
    1












    I need to rename files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. I do know that they will start with NORMAL and end with -lib*. I'd like to replace everything in between with some string X. For example,



    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.concordant
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.deletion
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.divergent
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.inversion
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.translocation
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.concordant
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.deletion
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.divergent
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion
    | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translocation


    So it will probably be of the form rename "s/something/X/", but I don't know what that something should be, as I don't know how to use regex.










    share|improve this question

























      up vote
      5
      down vote

      favorite
      1









      up vote
      5
      down vote

      favorite
      1






      1





      I need to rename files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. I do know that they will start with NORMAL and end with -lib*. I'd like to replace everything in between with some string X. For example,



      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.concordant
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.deletion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.divergent
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.inversion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.translocation
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.concordant
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.deletion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.divergent
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translocation


      So it will probably be of the form rename "s/something/X/", but I don't know what that something should be, as I don't know how to use regex.










      share|improve this question















      I need to rename files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. I do know that they will start with NORMAL and end with -lib*. I'd like to replace everything in between with some string X. For example,



      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.concordant
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.deletion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.divergent
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.inversion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.translocation
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.concordant
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.deletion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.divergent
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion
      | | |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translocation


      So it will probably be of the form rename "s/something/X/", but I don't know what that something should be, as I don't know how to use regex.







      shell shell-script rename






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 at 2:21









      Rui F Ribeiro

      38.2k1475123




      38.2k1475123










      asked Aug 1 '14 at 15:19









      goodcow

      348136




      348136




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          If you are lucky enough to have rename available, then the following should be sufficient:



          rename 's/(NORMAL).*(-lib)/$1X$2/' *





          share|improve this answer



























            up vote
            2
            down vote













            The useful Perl powered rename isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename utility that does basic search and replace and nothing much else.



            If you're unfortunate enough to be using one of those, then something like the following may help:



            for oldname in *; do
            newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
            mv "$oldname" "$newname"
            done





            share|improve this answer



























              up vote
              1
              down vote













              There are two unrelated programs called rename. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do



              rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*


              The s operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib matches the string up to the last occurrence of -lib (.* matches any string), so s/.*-lib/NORMAL_X-lib/ replaces the NORMAL_blahblah-lib part by NORMAL_X-lib.



              If you have a Linux distribution that isn't derived from Debian, then the rename utility is one that is suitable for almost no practical task. You can use a shell loop instead.



              for x in NORMAL_*-lib*; do
              mv "$x" "NORMAL_X-lib$x##*-lib"
              done


              $x##*-lib is the value of the variable x minus the part up to the last occurrence of -lib. This is standard parameter expansion syntax.



              This task is easiest in zsh, with its zmv function. Put autoload -U zmv in your .zshrc (or run it on the command line for a once-off), then run



              zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'





              share|improve this answer



























                up vote
                0
                down vote













                You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib) i.e.



                $ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
                NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
                NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
                NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation





                share|improve this answer




















                  Your Answer








                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "106"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f147879%2frenaming-multiple-files-with-rename%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  1
                  down vote



                  accepted










                  If you are lucky enough to have rename available, then the following should be sufficient:



                  rename 's/(NORMAL).*(-lib)/$1X$2/' *





                  share|improve this answer
























                    up vote
                    1
                    down vote



                    accepted










                    If you are lucky enough to have rename available, then the following should be sufficient:



                    rename 's/(NORMAL).*(-lib)/$1X$2/' *





                    share|improve this answer






















                      up vote
                      1
                      down vote



                      accepted







                      up vote
                      1
                      down vote



                      accepted






                      If you are lucky enough to have rename available, then the following should be sufficient:



                      rename 's/(NORMAL).*(-lib)/$1X$2/' *





                      share|improve this answer












                      If you are lucky enough to have rename available, then the following should be sufficient:



                      rename 's/(NORMAL).*(-lib)/$1X$2/' *






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 1 '14 at 19:04









                      Nick Tomlin

                      21738




                      21738






















                          up vote
                          2
                          down vote













                          The useful Perl powered rename isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename utility that does basic search and replace and nothing much else.



                          If you're unfortunate enough to be using one of those, then something like the following may help:



                          for oldname in *; do
                          newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
                          mv "$oldname" "$newname"
                          done





                          share|improve this answer
























                            up vote
                            2
                            down vote













                            The useful Perl powered rename isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename utility that does basic search and replace and nothing much else.



                            If you're unfortunate enough to be using one of those, then something like the following may help:



                            for oldname in *; do
                            newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
                            mv "$oldname" "$newname"
                            done





                            share|improve this answer






















                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              The useful Perl powered rename isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename utility that does basic search and replace and nothing much else.



                              If you're unfortunate enough to be using one of those, then something like the following may help:



                              for oldname in *; do
                              newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
                              mv "$oldname" "$newname"
                              done





                              share|improve this answer












                              The useful Perl powered rename isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename utility that does basic search and replace and nothing much else.



                              If you're unfortunate enough to be using one of those, then something like the following may help:



                              for oldname in *; do
                              newname=$(echo "$oldname" | sed -e 's/(NORMAL).*(-lib.*)/1X2/')
                              mv "$oldname" "$newname"
                              done






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 1 '14 at 18:27









                              garethTheRed

                              23.6k35978




                              23.6k35978




















                                  up vote
                                  1
                                  down vote













                                  There are two unrelated programs called rename. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do



                                  rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*


                                  The s operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib matches the string up to the last occurrence of -lib (.* matches any string), so s/.*-lib/NORMAL_X-lib/ replaces the NORMAL_blahblah-lib part by NORMAL_X-lib.



                                  If you have a Linux distribution that isn't derived from Debian, then the rename utility is one that is suitable for almost no practical task. You can use a shell loop instead.



                                  for x in NORMAL_*-lib*; do
                                  mv "$x" "NORMAL_X-lib$x##*-lib"
                                  done


                                  $x##*-lib is the value of the variable x minus the part up to the last occurrence of -lib. This is standard parameter expansion syntax.



                                  This task is easiest in zsh, with its zmv function. Put autoload -U zmv in your .zshrc (or run it on the command line for a once-off), then run



                                  zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'





                                  share|improve this answer
























                                    up vote
                                    1
                                    down vote













                                    There are two unrelated programs called rename. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do



                                    rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*


                                    The s operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib matches the string up to the last occurrence of -lib (.* matches any string), so s/.*-lib/NORMAL_X-lib/ replaces the NORMAL_blahblah-lib part by NORMAL_X-lib.



                                    If you have a Linux distribution that isn't derived from Debian, then the rename utility is one that is suitable for almost no practical task. You can use a shell loop instead.



                                    for x in NORMAL_*-lib*; do
                                    mv "$x" "NORMAL_X-lib$x##*-lib"
                                    done


                                    $x##*-lib is the value of the variable x minus the part up to the last occurrence of -lib. This is standard parameter expansion syntax.



                                    This task is easiest in zsh, with its zmv function. Put autoload -U zmv in your .zshrc (or run it on the command line for a once-off), then run



                                    zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'





                                    share|improve this answer






















                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      There are two unrelated programs called rename. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do



                                      rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*


                                      The s operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib matches the string up to the last occurrence of -lib (.* matches any string), so s/.*-lib/NORMAL_X-lib/ replaces the NORMAL_blahblah-lib part by NORMAL_X-lib.



                                      If you have a Linux distribution that isn't derived from Debian, then the rename utility is one that is suitable for almost no practical task. You can use a shell loop instead.



                                      for x in NORMAL_*-lib*; do
                                      mv "$x" "NORMAL_X-lib$x##*-lib"
                                      done


                                      $x##*-lib is the value of the variable x minus the part up to the last occurrence of -lib. This is standard parameter expansion syntax.



                                      This task is easiest in zsh, with its zmv function. Put autoload -U zmv in your .zshrc (or run it on the command line for a once-off), then run



                                      zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'





                                      share|improve this answer












                                      There are two unrelated programs called rename. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do



                                      rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib*


                                      The s operator performs a regular expression replacement. There are oodles of regular expression tutorials on the web, e.g. 1. .*-lib matches the string up to the last occurrence of -lib (.* matches any string), so s/.*-lib/NORMAL_X-lib/ replaces the NORMAL_blahblah-lib part by NORMAL_X-lib.



                                      If you have a Linux distribution that isn't derived from Debian, then the rename utility is one that is suitable for almost no practical task. You can use a shell loop instead.



                                      for x in NORMAL_*-lib*; do
                                      mv "$x" "NORMAL_X-lib$x##*-lib"
                                      done


                                      $x##*-lib is the value of the variable x minus the part up to the last occurrence of -lib. This is standard parameter expansion syntax.



                                      This task is easiest in zsh, with its zmv function. Put autoload -U zmv in your .zshrc (or run it on the command line for a once-off), then run



                                      zmv 'NORMAL_*-(lib*)' 'NORMAL_X-$1'






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 1 '14 at 23:16









                                      Gilles

                                      521k12610401569




                                      521k12610401569




















                                          up vote
                                          0
                                          down vote













                                          You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib) i.e.



                                          $ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
                                          NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
                                          NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
                                          NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation





                                          share|improve this answer
























                                            up vote
                                            0
                                            down vote













                                            You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib) i.e.



                                            $ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
                                            NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
                                            NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
                                            NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation





                                            share|improve this answer






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib) i.e.



                                              $ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
                                              NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
                                              NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
                                              NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation





                                              share|improve this answer












                                              You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib) i.e.



                                              $ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' *
                                              NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion
                                              NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion
                                              NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed as NORMALX-lib4.translation






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Aug 1 '14 at 16:16









                                              steeldriver

                                              33.6k34982




                                              33.6k34982



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f147879%2frenaming-multiple-files-with-rename%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown






                                                  Popular posts from this blog

                                                  How to check contact read email or not when send email to Individual?

                                                  Bahrain

                                                  Postfix configuration issue with fips on centos 7; mailgun relay