Add or subtract a number from the names of all the files in a directory2019 Community Moderator ElectionMeaning of `10#N`For each subfolder, sort files by name and rename them to sequential padded numbers (regardless of extension)Batch rename files with unknown names and unknown extensionsHow to batch clean filenames containing invalid charactersHow do I rename lots of files on a directory without changing their extensions?How to find out all the files in other machines using bash shell script?What exactly files whose names' prefix is “AT.postflight.” are needed for?can mmv rename files by incrementing an index?How can I remove certain string from file name?errors in parameter expansion on filenames named after negative numbersRename all files in a directory to add a leading zero - for filenames with varying 'stems'

Basic combinatorial probability problem

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?

Are the IPv6 address space and IPv4 address space completely disjoint?

Fear of getting stuck on one programming language / technology that is not used in my country

What is this called? Old film camera viewer?

A social experiment. What is the worst that can happen?

Why a symmetric relation is defined: ∀x∀y( xRy⟹yRx) and not ∀x∀y (xRy⟺yRx)?

Pre-mixing cryogenic fuels and using only one fuel tank

Why is so much work done on numerical verification of the Riemann Hypothesis?

Melting point of aspirin, contradicting sources

Aragorn's "guise" in the Orthanc Stone

What does chmod -u do?

What is going wrong in this circuit which supposedly should step down AC to arduino friendly voltage?

Is Witten's Proof of the Positive Mass Theorem Rigorous?

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

How to write values with uncertainty and units with brackets: (339+-14) m/s

Is (0,1] a closed or open set?

Creepy dinosaur pc game identification

Added a new user on Ubuntu, set password not working?

How could a planet have erratic days?

Multiplicative persistence

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

How to advoid Unknown field: MyJSON.number

Creating nested elements dynamically



Add or subtract a number from the names of all the files in a directory



2019 Community Moderator ElectionMeaning of `10#N`For each subfolder, sort files by name and rename them to sequential padded numbers (regardless of extension)Batch rename files with unknown names and unknown extensionsHow to batch clean filenames containing invalid charactersHow do I rename lots of files on a directory without changing their extensions?How to find out all the files in other machines using bash shell script?What exactly files whose names' prefix is “AT.postflight.” are needed for?can mmv rename files by incrementing an index?How can I remove certain string from file name?errors in parameter expansion on filenames named after negative numbersRename all files in a directory to add a leading zero - for filenames with varying 'stems'










2















I have a number of png and jpg files whose names are numbers, e.g.0100.png, in a directory,



  • How can I add 1 to their names, for example, to get 0002.png and 0003.png from 0001.png and 0002.png respectively, without overwriting?


  • How can I subtract 2 from their names, so that 0100.png will not become 098.png but 0098.png instead?


Related https://stackoverflow.com/questions/26770060/subtracting-a-number-from-the-names-of-all-the-files-in-a-directory, but more difficult here.










share|improve this question




























    2















    I have a number of png and jpg files whose names are numbers, e.g.0100.png, in a directory,



    • How can I add 1 to their names, for example, to get 0002.png and 0003.png from 0001.png and 0002.png respectively, without overwriting?


    • How can I subtract 2 from their names, so that 0100.png will not become 098.png but 0098.png instead?


    Related https://stackoverflow.com/questions/26770060/subtracting-a-number-from-the-names-of-all-the-files-in-a-directory, but more difficult here.










    share|improve this question


























      2












      2








      2








      I have a number of png and jpg files whose names are numbers, e.g.0100.png, in a directory,



      • How can I add 1 to their names, for example, to get 0002.png and 0003.png from 0001.png and 0002.png respectively, without overwriting?


      • How can I subtract 2 from their names, so that 0100.png will not become 098.png but 0098.png instead?


      Related https://stackoverflow.com/questions/26770060/subtracting-a-number-from-the-names-of-all-the-files-in-a-directory, but more difficult here.










      share|improve this question
















      I have a number of png and jpg files whose names are numbers, e.g.0100.png, in a directory,



      • How can I add 1 to their names, for example, to get 0002.png and 0003.png from 0001.png and 0002.png respectively, without overwriting?


      • How can I subtract 2 from their names, so that 0100.png will not become 098.png but 0098.png instead?


      Related https://stackoverflow.com/questions/26770060/subtracting-a-number-from-the-names-of-all-the-files-in-a-directory, but more difficult here.







      bash filenames rename arithmetic






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '17 at 12:39









      Community

      1




      1










      asked Nov 13 '14 at 22:46









      TimTim

      28k78269488




      28k78269488




















          3 Answers
          3






          active

          oldest

          votes


















          3














          I would probably end up using temporary directory in this case:



          for file in [[:digit:]]*.png; do
          echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
          done


          The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.



          For example:



          $ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
          $ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
          mv 0001.png tmp/0000.png
          mv 0002.png tmp/0001.png
          mv 0010.png tmp/0009.png
          mv 0020.png tmp/0019.png
          mv 0100.png tmp/0099.png
          mv 0200.png tmp/0199.png





          share|improve this answer























          • So just run this command two times changing png->jpg, or store extension in variable with ext=$file/*./ or add additional loop over all different extensions...

            – jimmij
            Nov 13 '14 at 23:35












          • BTW, in zsh under default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename become tmp/$(printf %04d $(($file:r-1))).png

            – jimmij
            Nov 14 '14 at 0:04











          • Is the tmp dir under current dir? I run your command, but it doesn't do anything.

            – Tim
            Nov 14 '14 at 0:10











          • @Tim Yes in my case tmp is in current directory, so at the beginning mkdir tmp is need and at the end mv tmp/* .; rm -r tmp. Of course you can use /tmp (or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there is echo at the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...

            – jimmij
            Nov 14 '14 at 0:16












          • I created a dir tmp under the current one, but still it is empty.

            – Tim
            Nov 14 '14 at 0:20


















          2














          The solution for (1)



          Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.



          ls *.png | sort -rn | while read ...; do ... mv ...; done


          The solution for (2)



          Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:



          printf %04d.png 98
          0098.png





          share|improve this answer

























          • Can we automate the process?

            – Tim
            Nov 13 '14 at 22:53











          • Thanks, but what are ...? Can we also add and subtract a number?

            – Tim
            Nov 13 '14 at 22:58


















          1














          Using Perl's rename :



          -2 :



          $ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
          0100.png -> 0098.png


          +1 :



          $ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
          0001.png -> 0002.png
          0002.png -> 0003.png


          You can remove the -n (dry-run mode switch) when your tests become valids.



          warningThere are other tools with the same name which may or may not be able to do this, so be careful.



          If you run the following command (linux)



          $ file $(readlink -f $(type -p rename))


          and you have a result like



          .../rename: Perl script, ASCII text executable


          then this seems to be the right tool =)



          If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :



          $ sudo update-alternatives --set rename /path/to/rename


          (replace /path/to/rename to the path of your perl's rename command.




          If you don't have this command, search your package manager to install it or do it manually




          Last but not least, this tool was originally written by Larry Wall, the Perl's dad.






          share|improve this answer

























          • How do these work - without an intermediate temporary file?

            – steeldriver
            Nov 14 '14 at 0:03











          • On Ubuntu 12.04, I just get 0001.png not renamed: 0002.png already exists

            – steeldriver
            Nov 14 '14 at 0:22










          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',
          autoActivateHeartbeat: false,
          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%2f167862%2fadd-or-subtract-a-number-from-the-names-of-all-the-files-in-a-directory%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          I would probably end up using temporary directory in this case:



          for file in [[:digit:]]*.png; do
          echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
          done


          The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.



          For example:



          $ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
          $ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
          mv 0001.png tmp/0000.png
          mv 0002.png tmp/0001.png
          mv 0010.png tmp/0009.png
          mv 0020.png tmp/0019.png
          mv 0100.png tmp/0099.png
          mv 0200.png tmp/0199.png





          share|improve this answer























          • So just run this command two times changing png->jpg, or store extension in variable with ext=$file/*./ or add additional loop over all different extensions...

            – jimmij
            Nov 13 '14 at 23:35












          • BTW, in zsh under default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename become tmp/$(printf %04d $(($file:r-1))).png

            – jimmij
            Nov 14 '14 at 0:04











          • Is the tmp dir under current dir? I run your command, but it doesn't do anything.

            – Tim
            Nov 14 '14 at 0:10











          • @Tim Yes in my case tmp is in current directory, so at the beginning mkdir tmp is need and at the end mv tmp/* .; rm -r tmp. Of course you can use /tmp (or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there is echo at the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...

            – jimmij
            Nov 14 '14 at 0:16












          • I created a dir tmp under the current one, but still it is empty.

            – Tim
            Nov 14 '14 at 0:20















          3














          I would probably end up using temporary directory in this case:



          for file in [[:digit:]]*.png; do
          echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
          done


          The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.



          For example:



          $ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
          $ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
          mv 0001.png tmp/0000.png
          mv 0002.png tmp/0001.png
          mv 0010.png tmp/0009.png
          mv 0020.png tmp/0019.png
          mv 0100.png tmp/0099.png
          mv 0200.png tmp/0199.png





          share|improve this answer























          • So just run this command two times changing png->jpg, or store extension in variable with ext=$file/*./ or add additional loop over all different extensions...

            – jimmij
            Nov 13 '14 at 23:35












          • BTW, in zsh under default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename become tmp/$(printf %04d $(($file:r-1))).png

            – jimmij
            Nov 14 '14 at 0:04











          • Is the tmp dir under current dir? I run your command, but it doesn't do anything.

            – Tim
            Nov 14 '14 at 0:10











          • @Tim Yes in my case tmp is in current directory, so at the beginning mkdir tmp is need and at the end mv tmp/* .; rm -r tmp. Of course you can use /tmp (or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there is echo at the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...

            – jimmij
            Nov 14 '14 at 0:16












          • I created a dir tmp under the current one, but still it is empty.

            – Tim
            Nov 14 '14 at 0:20













          3












          3








          3







          I would probably end up using temporary directory in this case:



          for file in [[:digit:]]*.png; do
          echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
          done


          The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.



          For example:



          $ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
          $ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
          mv 0001.png tmp/0000.png
          mv 0002.png tmp/0001.png
          mv 0010.png tmp/0009.png
          mv 0020.png tmp/0019.png
          mv 0100.png tmp/0099.png
          mv 0200.png tmp/0199.png





          share|improve this answer













          I would probably end up using temporary directory in this case:



          for file in [[:digit:]]*.png; do
          echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
          done


          The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.



          For example:



          $ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
          $ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
          mv 0001.png tmp/0000.png
          mv 0002.png tmp/0001.png
          mv 0010.png tmp/0009.png
          mv 0020.png tmp/0019.png
          mv 0100.png tmp/0099.png
          mv 0200.png tmp/0199.png






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '14 at 23:21









          jimmijjimmij

          32.3k875109




          32.3k875109












          • So just run this command two times changing png->jpg, or store extension in variable with ext=$file/*./ or add additional loop over all different extensions...

            – jimmij
            Nov 13 '14 at 23:35












          • BTW, in zsh under default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename become tmp/$(printf %04d $(($file:r-1))).png

            – jimmij
            Nov 14 '14 at 0:04











          • Is the tmp dir under current dir? I run your command, but it doesn't do anything.

            – Tim
            Nov 14 '14 at 0:10











          • @Tim Yes in my case tmp is in current directory, so at the beginning mkdir tmp is need and at the end mv tmp/* .; rm -r tmp. Of course you can use /tmp (or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there is echo at the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...

            – jimmij
            Nov 14 '14 at 0:16












          • I created a dir tmp under the current one, but still it is empty.

            – Tim
            Nov 14 '14 at 0:20

















          • So just run this command two times changing png->jpg, or store extension in variable with ext=$file/*./ or add additional loop over all different extensions...

            – jimmij
            Nov 13 '14 at 23:35












          • BTW, in zsh under default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename become tmp/$(printf %04d $(($file:r-1))).png

            – jimmij
            Nov 14 '14 at 0:04











          • Is the tmp dir under current dir? I run your command, but it doesn't do anything.

            – Tim
            Nov 14 '14 at 0:10











          • @Tim Yes in my case tmp is in current directory, so at the beginning mkdir tmp is need and at the end mv tmp/* .; rm -r tmp. Of course you can use /tmp (or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there is echo at the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...

            – jimmij
            Nov 14 '14 at 0:16












          • I created a dir tmp under the current one, but still it is empty.

            – Tim
            Nov 14 '14 at 0:20
















          So just run this command two times changing png->jpg, or store extension in variable with ext=$file/*./ or add additional loop over all different extensions...

          – jimmij
          Nov 13 '14 at 23:35






          So just run this command two times changing png->jpg, or store extension in variable with ext=$file/*./ or add additional loop over all different extensions...

          – jimmij
          Nov 13 '14 at 23:35














          BTW, in zsh under default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename become tmp/$(printf %04d $(($file:r-1))).png

          – jimmij
          Nov 14 '14 at 0:04





          BTW, in zsh under default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename become tmp/$(printf %04d $(($file:r-1))).png

          – jimmij
          Nov 14 '14 at 0:04













          Is the tmp dir under current dir? I run your command, but it doesn't do anything.

          – Tim
          Nov 14 '14 at 0:10





          Is the tmp dir under current dir? I run your command, but it doesn't do anything.

          – Tim
          Nov 14 '14 at 0:10













          @Tim Yes in my case tmp is in current directory, so at the beginning mkdir tmp is need and at the end mv tmp/* .; rm -r tmp. Of course you can use /tmp (or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there is echo at the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...

          – jimmij
          Nov 14 '14 at 0:16






          @Tim Yes in my case tmp is in current directory, so at the beginning mkdir tmp is need and at the end mv tmp/* .; rm -r tmp. Of course you can use /tmp (or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there is echo at the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...

          – jimmij
          Nov 14 '14 at 0:16














          I created a dir tmp under the current one, but still it is empty.

          – Tim
          Nov 14 '14 at 0:20





          I created a dir tmp under the current one, but still it is empty.

          – Tim
          Nov 14 '14 at 0:20













          2














          The solution for (1)



          Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.



          ls *.png | sort -rn | while read ...; do ... mv ...; done


          The solution for (2)



          Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:



          printf %04d.png 98
          0098.png





          share|improve this answer

























          • Can we automate the process?

            – Tim
            Nov 13 '14 at 22:53











          • Thanks, but what are ...? Can we also add and subtract a number?

            – Tim
            Nov 13 '14 at 22:58















          2














          The solution for (1)



          Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.



          ls *.png | sort -rn | while read ...; do ... mv ...; done


          The solution for (2)



          Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:



          printf %04d.png 98
          0098.png





          share|improve this answer

























          • Can we automate the process?

            – Tim
            Nov 13 '14 at 22:53











          • Thanks, but what are ...? Can we also add and subtract a number?

            – Tim
            Nov 13 '14 at 22:58













          2












          2








          2







          The solution for (1)



          Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.



          ls *.png | sort -rn | while read ...; do ... mv ...; done


          The solution for (2)



          Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:



          printf %04d.png 98
          0098.png





          share|improve this answer















          The solution for (1)



          Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.



          ls *.png | sort -rn | while read ...; do ... mv ...; done


          The solution for (2)



          Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:



          printf %04d.png 98
          0098.png






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '14 at 22:56

























          answered Nov 13 '14 at 22:50









          Hauke LagingHauke Laging

          57.5k1287136




          57.5k1287136












          • Can we automate the process?

            – Tim
            Nov 13 '14 at 22:53











          • Thanks, but what are ...? Can we also add and subtract a number?

            – Tim
            Nov 13 '14 at 22:58

















          • Can we automate the process?

            – Tim
            Nov 13 '14 at 22:53











          • Thanks, but what are ...? Can we also add and subtract a number?

            – Tim
            Nov 13 '14 at 22:58
















          Can we automate the process?

          – Tim
          Nov 13 '14 at 22:53





          Can we automate the process?

          – Tim
          Nov 13 '14 at 22:53













          Thanks, but what are ...? Can we also add and subtract a number?

          – Tim
          Nov 13 '14 at 22:58





          Thanks, but what are ...? Can we also add and subtract a number?

          – Tim
          Nov 13 '14 at 22:58











          1














          Using Perl's rename :



          -2 :



          $ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
          0100.png -> 0098.png


          +1 :



          $ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
          0001.png -> 0002.png
          0002.png -> 0003.png


          You can remove the -n (dry-run mode switch) when your tests become valids.



          warningThere are other tools with the same name which may or may not be able to do this, so be careful.



          If you run the following command (linux)



          $ file $(readlink -f $(type -p rename))


          and you have a result like



          .../rename: Perl script, ASCII text executable


          then this seems to be the right tool =)



          If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :



          $ sudo update-alternatives --set rename /path/to/rename


          (replace /path/to/rename to the path of your perl's rename command.




          If you don't have this command, search your package manager to install it or do it manually




          Last but not least, this tool was originally written by Larry Wall, the Perl's dad.






          share|improve this answer

























          • How do these work - without an intermediate temporary file?

            – steeldriver
            Nov 14 '14 at 0:03











          • On Ubuntu 12.04, I just get 0001.png not renamed: 0002.png already exists

            – steeldriver
            Nov 14 '14 at 0:22















          1














          Using Perl's rename :



          -2 :



          $ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
          0100.png -> 0098.png


          +1 :



          $ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
          0001.png -> 0002.png
          0002.png -> 0003.png


          You can remove the -n (dry-run mode switch) when your tests become valids.



          warningThere are other tools with the same name which may or may not be able to do this, so be careful.



          If you run the following command (linux)



          $ file $(readlink -f $(type -p rename))


          and you have a result like



          .../rename: Perl script, ASCII text executable


          then this seems to be the right tool =)



          If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :



          $ sudo update-alternatives --set rename /path/to/rename


          (replace /path/to/rename to the path of your perl's rename command.




          If you don't have this command, search your package manager to install it or do it manually




          Last but not least, this tool was originally written by Larry Wall, the Perl's dad.






          share|improve this answer

























          • How do these work - without an intermediate temporary file?

            – steeldriver
            Nov 14 '14 at 0:03











          • On Ubuntu 12.04, I just get 0001.png not renamed: 0002.png already exists

            – steeldriver
            Nov 14 '14 at 0:22













          1












          1








          1







          Using Perl's rename :



          -2 :



          $ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
          0100.png -> 0098.png


          +1 :



          $ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
          0001.png -> 0002.png
          0002.png -> 0003.png


          You can remove the -n (dry-run mode switch) when your tests become valids.



          warningThere are other tools with the same name which may or may not be able to do this, so be careful.



          If you run the following command (linux)



          $ file $(readlink -f $(type -p rename))


          and you have a result like



          .../rename: Perl script, ASCII text executable


          then this seems to be the right tool =)



          If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :



          $ sudo update-alternatives --set rename /path/to/rename


          (replace /path/to/rename to the path of your perl's rename command.




          If you don't have this command, search your package manager to install it or do it manually




          Last but not least, this tool was originally written by Larry Wall, the Perl's dad.






          share|improve this answer















          Using Perl's rename :



          -2 :



          $ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
          0100.png -> 0098.png


          +1 :



          $ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
          0001.png -> 0002.png
          0002.png -> 0003.png


          You can remove the -n (dry-run mode switch) when your tests become valids.



          warningThere are other tools with the same name which may or may not be able to do this, so be careful.



          If you run the following command (linux)



          $ file $(readlink -f $(type -p rename))


          and you have a result like



          .../rename: Perl script, ASCII text executable


          then this seems to be the right tool =)



          If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :



          $ sudo update-alternatives --set rename /path/to/rename


          (replace /path/to/rename to the path of your perl's rename command.




          If you don't have this command, search your package manager to install it or do it manually




          Last but not least, this tool was originally written by Larry Wall, the Perl's dad.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 12 at 20:10









          Glorfindel

          3171511




          3171511










          answered Nov 13 '14 at 23:27









          Gilles QuenotGilles Quenot

          16.3k14053




          16.3k14053












          • How do these work - without an intermediate temporary file?

            – steeldriver
            Nov 14 '14 at 0:03











          • On Ubuntu 12.04, I just get 0001.png not renamed: 0002.png already exists

            – steeldriver
            Nov 14 '14 at 0:22

















          • How do these work - without an intermediate temporary file?

            – steeldriver
            Nov 14 '14 at 0:03











          • On Ubuntu 12.04, I just get 0001.png not renamed: 0002.png already exists

            – steeldriver
            Nov 14 '14 at 0:22
















          How do these work - without an intermediate temporary file?

          – steeldriver
          Nov 14 '14 at 0:03





          How do these work - without an intermediate temporary file?

          – steeldriver
          Nov 14 '14 at 0:03













          On Ubuntu 12.04, I just get 0001.png not renamed: 0002.png already exists

          – steeldriver
          Nov 14 '14 at 0:22





          On Ubuntu 12.04, I just get 0001.png not renamed: 0002.png already exists

          – steeldriver
          Nov 14 '14 at 0:22

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f167862%2fadd-or-subtract-a-number-from-the-names-of-all-the-files-in-a-directory%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







          -arithmetic, bash, filenames, rename

          Popular posts from this blog

          Creating 100m^2 grid automatically using QGIS?Creating grid constrained within polygon in QGIS?Createing polygon layer from point data using QGIS?Creating vector grid using QGIS?Creating grid polygons from coordinates using R or PythonCreating grid from spatio temporal point data?Creating fields in attributes table using other layers using QGISCreate .shp vector grid in QGISQGIS Creating 4km point grid within polygonsCreate a vector grid over a raster layerVector Grid Creates just one grid

          What is this called? Old film camera viewer?What makes a good film camera?What to do with an old film camera?What should one look for when buying a used film camera?What is the value and age of this pre-1967 Ricoh 35 mm camera?DSLR recommendation, question about old Canon 35mm film Camera & lensesCan anyone identify the silver rangefinder-style camera in this advertisement?What kind of a Polaroid 600-camera is this?Will an old film camera still work even when not used in a very long time?What is this camera / Can I develop the film?How to fit an action camera into antique (bellows) housing?What to check when buying used and old film bodies?

          Why is this plane circling around the Lucknow airport every day?Why do aircraft on Flight Radar 24 jump around randomly sometimes?What airport has this walkway over a taxiway?How does Chicago O'Hare's tower sequence aircraft at peak capacity?Which airport is featured in this Delta commercial?After a crash, for how long is the airport closed?Can a passenger plane stand still in the air, or hover at a fixed location above a ground?What are those trucks towing around, and why?What is this airport outside of Cairo, Egypt?Which US airport has the lowest circling MDH?What is this airport video?