Rename files add dashes after fourth and sixth characters

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











up vote
1
down vote

favorite












I have some files such as:



20150716_something-here
20150716_something-heretoo
20150716_something-hereaswell


They need to be renamed as



2015-07-16_something-here
2015-07-16_something-heretoo
2015-07-16_something-hereaswell


I tried using a perl implementation of the rename command (see my comment on the accepted answer) but I was not successful.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I have some files such as:



    20150716_something-here
    20150716_something-heretoo
    20150716_something-hereaswell


    They need to be renamed as



    2015-07-16_something-here
    2015-07-16_something-heretoo
    2015-07-16_something-hereaswell


    I tried using a perl implementation of the rename command (see my comment on the accepted answer) but I was not successful.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have some files such as:



      20150716_something-here
      20150716_something-heretoo
      20150716_something-hereaswell


      They need to be renamed as



      2015-07-16_something-here
      2015-07-16_something-heretoo
      2015-07-16_something-hereaswell


      I tried using a perl implementation of the rename command (see my comment on the accepted answer) but I was not successful.










      share|improve this question















      I have some files such as:



      20150716_something-here
      20150716_something-heretoo
      20150716_something-hereaswell


      They need to be renamed as



      2015-07-16_something-here
      2015-07-16_something-heretoo
      2015-07-16_something-hereaswell


      I tried using a perl implementation of the rename command (see my comment on the accepted answer) but I was not successful.







      bash terminal regular-expression rename






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 22:25









      Rui F Ribeiro

      38.2k1475125




      38.2k1475125










      asked May 19 '16 at 21:52









      Adrien Be

      1085




      1085




















          6 Answers
          6






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Using the perl rename command (which is completely different to the rename command from util-linux):



          rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*


          (use -n rather than -v for a dry-run to test the command first).



          This perl version of rename may be called prename or file-rename on your system. It is far more capable and useful than the util-linux version of rename. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN



          BTW, you can tell if you already have it installed by running rename -V. If it produces output like this:



          $ rename -V
          Unknown option: V
          Usage: rename [-v] [-n] [-f] perlexpr [filenames]


          or this:



          $ rename -V
          /usr/bin/rename using File::Rename version 0.20


          Then you have perl rename installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl package). The latter indicates the current version (now a separate package called rename).






          share|improve this answer






















          • wooops, I did not realize I was using a perl implementation of rename. Installed this package using homebrew via brew install rename, now doing brew info rename prompts "Perl-powered file rename script"
            – Adrien Be
            May 27 '16 at 7:24










          • you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
            – cas
            May 27 '16 at 7:32

















          up vote
          2
          down vote













          With sed:



          LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file





          share|improve this answer
















          • 1




            How does this rename a file?
            – Wildcard
            May 20 '16 at 5:27










          • This one just format the text, it does not rename the file
            – cuonglm
            May 20 '16 at 5:34

















          up vote
          2
          down vote













          While rename is a very powerful tool, I normally prefer the simplicity of the mmv (multiple move) utility:



          mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'


          The ? in the search pattern stands for a single character, the * for an arbitrarily long sequence of characters. In the replacement pattern, every #<number> stands for a corresponding ? or * in the search pattern. In addition to ? and *, mmv supports character ranges within brackets (like [a-f]).



          (mmv will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)






          share|improve this answer



























            up vote
            2
            down vote













            Using bash's built-in substring expansion:



            for f in 2015* ; do
            mv "$f" "$f::4-$f:4:2-$f:6"
            done





            share|improve this answer



























              up vote
              0
              down vote













              On my distribution I have the perl-rename command, which can use a perl-style regex to bulk-rename files. The rename command only accepts a pair of fixed strings for the rename.






              share|improve this answer



























                up vote
                0
                down vote













                quick and dirty not full solution



                #!/usr/bin/env bash
                str=$1
                yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
                mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
                dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
                new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
                echo $new_str


                output:



                $ bash script.sh '20150716_something-here'
                2015-07-16_something-here





                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%2f284282%2frename-files-add-dashes-after-fourth-and-sixth-characters%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  1
                  down vote



                  accepted










                  Using the perl rename command (which is completely different to the rename command from util-linux):



                  rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*


                  (use -n rather than -v for a dry-run to test the command first).



                  This perl version of rename may be called prename or file-rename on your system. It is far more capable and useful than the util-linux version of rename. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN



                  BTW, you can tell if you already have it installed by running rename -V. If it produces output like this:



                  $ rename -V
                  Unknown option: V
                  Usage: rename [-v] [-n] [-f] perlexpr [filenames]


                  or this:



                  $ rename -V
                  /usr/bin/rename using File::Rename version 0.20


                  Then you have perl rename installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl package). The latter indicates the current version (now a separate package called rename).






                  share|improve this answer






















                  • wooops, I did not realize I was using a perl implementation of rename. Installed this package using homebrew via brew install rename, now doing brew info rename prompts "Perl-powered file rename script"
                    – Adrien Be
                    May 27 '16 at 7:24










                  • you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
                    – cas
                    May 27 '16 at 7:32














                  up vote
                  1
                  down vote



                  accepted










                  Using the perl rename command (which is completely different to the rename command from util-linux):



                  rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*


                  (use -n rather than -v for a dry-run to test the command first).



                  This perl version of rename may be called prename or file-rename on your system. It is far more capable and useful than the util-linux version of rename. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN



                  BTW, you can tell if you already have it installed by running rename -V. If it produces output like this:



                  $ rename -V
                  Unknown option: V
                  Usage: rename [-v] [-n] [-f] perlexpr [filenames]


                  or this:



                  $ rename -V
                  /usr/bin/rename using File::Rename version 0.20


                  Then you have perl rename installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl package). The latter indicates the current version (now a separate package called rename).






                  share|improve this answer






















                  • wooops, I did not realize I was using a perl implementation of rename. Installed this package using homebrew via brew install rename, now doing brew info rename prompts "Perl-powered file rename script"
                    – Adrien Be
                    May 27 '16 at 7:24










                  • you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
                    – cas
                    May 27 '16 at 7:32












                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  Using the perl rename command (which is completely different to the rename command from util-linux):



                  rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*


                  (use -n rather than -v for a dry-run to test the command first).



                  This perl version of rename may be called prename or file-rename on your system. It is far more capable and useful than the util-linux version of rename. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN



                  BTW, you can tell if you already have it installed by running rename -V. If it produces output like this:



                  $ rename -V
                  Unknown option: V
                  Usage: rename [-v] [-n] [-f] perlexpr [filenames]


                  or this:



                  $ rename -V
                  /usr/bin/rename using File::Rename version 0.20


                  Then you have perl rename installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl package). The latter indicates the current version (now a separate package called rename).






                  share|improve this answer














                  Using the perl rename command (which is completely different to the rename command from util-linux):



                  rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*


                  (use -n rather than -v for a dry-run to test the command first).



                  This perl version of rename may be called prename or file-rename on your system. It is far more capable and useful than the util-linux version of rename. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN



                  BTW, you can tell if you already have it installed by running rename -V. If it produces output like this:



                  $ rename -V
                  Unknown option: V
                  Usage: rename [-v] [-n] [-f] perlexpr [filenames]


                  or this:



                  $ rename -V
                  /usr/bin/rename using File::Rename version 0.20


                  Then you have perl rename installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl package). The latter indicates the current version (now a separate package called rename).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 20 '16 at 5:20

























                  answered May 20 '16 at 5:15









                  cas

                  38.3k44898




                  38.3k44898











                  • wooops, I did not realize I was using a perl implementation of rename. Installed this package using homebrew via brew install rename, now doing brew info rename prompts "Perl-powered file rename script"
                    – Adrien Be
                    May 27 '16 at 7:24










                  • you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
                    – cas
                    May 27 '16 at 7:32
















                  • wooops, I did not realize I was using a perl implementation of rename. Installed this package using homebrew via brew install rename, now doing brew info rename prompts "Perl-powered file rename script"
                    – Adrien Be
                    May 27 '16 at 7:24










                  • you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
                    – cas
                    May 27 '16 at 7:32















                  wooops, I did not realize I was using a perl implementation of rename. Installed this package using homebrew via brew install rename, now doing brew info rename prompts "Perl-powered file rename script"
                  – Adrien Be
                  May 27 '16 at 7:24




                  wooops, I did not realize I was using a perl implementation of rename. Installed this package using homebrew via brew install rename, now doing brew info rename prompts "Perl-powered file rename script"
                  – Adrien Be
                  May 27 '16 at 7:24












                  you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
                  – cas
                  May 27 '16 at 7:32




                  you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
                  – cas
                  May 27 '16 at 7:32












                  up vote
                  2
                  down vote













                  With sed:



                  LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file





                  share|improve this answer
















                  • 1




                    How does this rename a file?
                    – Wildcard
                    May 20 '16 at 5:27










                  • This one just format the text, it does not rename the file
                    – cuonglm
                    May 20 '16 at 5:34














                  up vote
                  2
                  down vote













                  With sed:



                  LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file





                  share|improve this answer
















                  • 1




                    How does this rename a file?
                    – Wildcard
                    May 20 '16 at 5:27










                  • This one just format the text, it does not rename the file
                    – cuonglm
                    May 20 '16 at 5:34












                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  With sed:



                  LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file





                  share|improve this answer












                  With sed:



                  LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 20 '16 at 4:02









                  cuonglm

                  101k23196297




                  101k23196297







                  • 1




                    How does this rename a file?
                    – Wildcard
                    May 20 '16 at 5:27










                  • This one just format the text, it does not rename the file
                    – cuonglm
                    May 20 '16 at 5:34












                  • 1




                    How does this rename a file?
                    – Wildcard
                    May 20 '16 at 5:27










                  • This one just format the text, it does not rename the file
                    – cuonglm
                    May 20 '16 at 5:34







                  1




                  1




                  How does this rename a file?
                  – Wildcard
                  May 20 '16 at 5:27




                  How does this rename a file?
                  – Wildcard
                  May 20 '16 at 5:27












                  This one just format the text, it does not rename the file
                  – cuonglm
                  May 20 '16 at 5:34




                  This one just format the text, it does not rename the file
                  – cuonglm
                  May 20 '16 at 5:34










                  up vote
                  2
                  down vote













                  While rename is a very powerful tool, I normally prefer the simplicity of the mmv (multiple move) utility:



                  mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'


                  The ? in the search pattern stands for a single character, the * for an arbitrarily long sequence of characters. In the replacement pattern, every #<number> stands for a corresponding ? or * in the search pattern. In addition to ? and *, mmv supports character ranges within brackets (like [a-f]).



                  (mmv will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)






                  share|improve this answer
























                    up vote
                    2
                    down vote













                    While rename is a very powerful tool, I normally prefer the simplicity of the mmv (multiple move) utility:



                    mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'


                    The ? in the search pattern stands for a single character, the * for an arbitrarily long sequence of characters. In the replacement pattern, every #<number> stands for a corresponding ? or * in the search pattern. In addition to ? and *, mmv supports character ranges within brackets (like [a-f]).



                    (mmv will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)






                    share|improve this answer






















                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      While rename is a very powerful tool, I normally prefer the simplicity of the mmv (multiple move) utility:



                      mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'


                      The ? in the search pattern stands for a single character, the * for an arbitrarily long sequence of characters. In the replacement pattern, every #<number> stands for a corresponding ? or * in the search pattern. In addition to ? and *, mmv supports character ranges within brackets (like [a-f]).



                      (mmv will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)






                      share|improve this answer












                      While rename is a very powerful tool, I normally prefer the simplicity of the mmv (multiple move) utility:



                      mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'


                      The ? in the search pattern stands for a single character, the * for an arbitrarily long sequence of characters. In the replacement pattern, every #<number> stands for a corresponding ? or * in the search pattern. In addition to ? and *, mmv supports character ranges within brackets (like [a-f]).



                      (mmv will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered May 20 '16 at 7:11









                      Dubu

                      2,3661122




                      2,3661122




















                          up vote
                          2
                          down vote













                          Using bash's built-in substring expansion:



                          for f in 2015* ; do
                          mv "$f" "$f::4-$f:4:2-$f:6"
                          done





                          share|improve this answer
























                            up vote
                            2
                            down vote













                            Using bash's built-in substring expansion:



                            for f in 2015* ; do
                            mv "$f" "$f::4-$f:4:2-$f:6"
                            done





                            share|improve this answer






















                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              Using bash's built-in substring expansion:



                              for f in 2015* ; do
                              mv "$f" "$f::4-$f:4:2-$f:6"
                              done





                              share|improve this answer












                              Using bash's built-in substring expansion:



                              for f in 2015* ; do
                              mv "$f" "$f::4-$f:4:2-$f:6"
                              done






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered May 20 '16 at 7:16









                              JigglyNaga

                              3,469828




                              3,469828




















                                  up vote
                                  0
                                  down vote













                                  On my distribution I have the perl-rename command, which can use a perl-style regex to bulk-rename files. The rename command only accepts a pair of fixed strings for the rename.






                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    On my distribution I have the perl-rename command, which can use a perl-style regex to bulk-rename files. The rename command only accepts a pair of fixed strings for the rename.






                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      On my distribution I have the perl-rename command, which can use a perl-style regex to bulk-rename files. The rename command only accepts a pair of fixed strings for the rename.






                                      share|improve this answer












                                      On my distribution I have the perl-rename command, which can use a perl-style regex to bulk-rename files. The rename command only accepts a pair of fixed strings for the rename.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered May 19 '16 at 22:05









                                      Chris Elston

                                      565




                                      565




















                                          up vote
                                          0
                                          down vote













                                          quick and dirty not full solution



                                          #!/usr/bin/env bash
                                          str=$1
                                          yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
                                          mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
                                          dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
                                          new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
                                          echo $new_str


                                          output:



                                          $ bash script.sh '20150716_something-here'
                                          2015-07-16_something-here





                                          share|improve this answer


























                                            up vote
                                            0
                                            down vote













                                            quick and dirty not full solution



                                            #!/usr/bin/env bash
                                            str=$1
                                            yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
                                            mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
                                            dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
                                            new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
                                            echo $new_str


                                            output:



                                            $ bash script.sh '20150716_something-here'
                                            2015-07-16_something-here





                                            share|improve this answer
























                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              quick and dirty not full solution



                                              #!/usr/bin/env bash
                                              str=$1
                                              yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
                                              mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
                                              dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
                                              new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
                                              echo $new_str


                                              output:



                                              $ bash script.sh '20150716_something-here'
                                              2015-07-16_something-here





                                              share|improve this answer














                                              quick and dirty not full solution



                                              #!/usr/bin/env bash
                                              str=$1
                                              yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
                                              mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
                                              dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
                                              new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
                                              echo $new_str


                                              output:



                                              $ bash script.sh '20150716_something-here'
                                              2015-07-16_something-here






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited May 20 '16 at 4:13









                                              cuonglm

                                              101k23196297




                                              101k23196297










                                              answered May 20 '16 at 2:21









                                              cuongnv23

                                              745




                                              745



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f284282%2frename-files-add-dashes-after-fourth-and-sixth-characters%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?

                                                  Displaying single band from multi-band raster using QGIS

                                                  How many registers does an x86_64 CPU actually have?