How can I prepend filenames with ascending numbers like 1_ 2_?











up vote
2
down vote

favorite












How can I add numbers to the files in one directory?



In one directory I have files like below:



fileA
fileB
fileC
fileD


I want to prepend ascending numbers to them, like this:



1_fileA
2_fileB
3_fileC
4_fileD


Thank you in advance.










share|improve this question









New contributor




paweljvn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • So you want to rename the files?
    – PerlDuck
    12 hours ago










  • @PerlDuck Yes, I have to enumarte them changing their name.
    – paweljvn
    12 hours ago










  • Related: askubuntu.com/q/839959
    – Justin
    32 mins ago















up vote
2
down vote

favorite












How can I add numbers to the files in one directory?



In one directory I have files like below:



fileA
fileB
fileC
fileD


I want to prepend ascending numbers to them, like this:



1_fileA
2_fileB
3_fileC
4_fileD


Thank you in advance.










share|improve this question









New contributor




paweljvn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • So you want to rename the files?
    – PerlDuck
    12 hours ago










  • @PerlDuck Yes, I have to enumarte them changing their name.
    – paweljvn
    12 hours ago










  • Related: askubuntu.com/q/839959
    – Justin
    32 mins ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











How can I add numbers to the files in one directory?



In one directory I have files like below:



fileA
fileB
fileC
fileD


I want to prepend ascending numbers to them, like this:



1_fileA
2_fileB
3_fileC
4_fileD


Thank you in advance.










share|improve this question









New contributor




paweljvn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











How can I add numbers to the files in one directory?



In one directory I have files like below:



fileA
fileB
fileC
fileD


I want to prepend ascending numbers to them, like this:



1_fileA
2_fileB
3_fileC
4_fileD


Thank you in advance.







command-line bash batch-rename






share|improve this question









New contributor




paweljvn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




paweljvn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 11 hours ago









Zanna

49k13123234




49k13123234






New contributor




paweljvn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 12 hours ago









paweljvn

165




165




New contributor




paweljvn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





paweljvn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






paweljvn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • So you want to rename the files?
    – PerlDuck
    12 hours ago










  • @PerlDuck Yes, I have to enumarte them changing their name.
    – paweljvn
    12 hours ago










  • Related: askubuntu.com/q/839959
    – Justin
    32 mins ago


















  • So you want to rename the files?
    – PerlDuck
    12 hours ago










  • @PerlDuck Yes, I have to enumarte them changing their name.
    – paweljvn
    12 hours ago










  • Related: askubuntu.com/q/839959
    – Justin
    32 mins ago
















So you want to rename the files?
– PerlDuck
12 hours ago




So you want to rename the files?
– PerlDuck
12 hours ago












@PerlDuck Yes, I have to enumarte them changing their name.
– paweljvn
12 hours ago




@PerlDuck Yes, I have to enumarte them changing their name.
– paweljvn
12 hours ago












Related: askubuntu.com/q/839959
– Justin
32 mins ago




Related: askubuntu.com/q/839959
– Justin
32 mins ago










3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










One of the solutions:



cd <your dir> then run in bash (copy and paste in command-line):



n=1; for f in *; do mv "$f" "$((n++))_$f"; done


Bash script case:



#!/bin/bash
n=1
for f in *
do
if [ "$f" = "rename.sh" ]
then
continue
fi
mv "$f" "$((n++))_$f"
done


save it as rename.sh to dir with files to rename, chmod +x rename.sh then run it ./rename.sh






share|improve this answer























  • but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
    – paweljvn
    11 hours ago










  • Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
    – S_Flash
    11 hours ago










  • Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
    – S_Flash
    11 hours ago


















up vote
3
down vote













If there are more than 9 files, I would use printf to pad the number to get the expected sort order, like this



n=0
for f in *
do printf -v new "%2d$((++n))_$f"
echo mv -v -- "$f" "$new"
done


Remove echo when you see the correct result.



Explanation



In this line, do printf -v new "%2d$((++n))_$f" we create a format for the new filenames and put it into the variable new.



%2d is a 2 digit decimal number. Instead of 2d, you can use 3d etc to get another leading 0 (if you have more than 99 files).



((++n)) increments the variable n (which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.



-v makes mv print what will be changed.



-- in the mv statement is to prevent filenames that start with - being interpreted as options.






share|improve this answer




























    up vote
    1
    down vote















    That’s a job for rename:



    rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *


    This defines a variable, increases it if it’s not set (else it would start with 0 instead of 1) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001, 002, …) use "%03d_". Running it with -n only prints the changes, to actually perform the renaming remove this flag.



    Example run



    $ ls -1
    fileA
    fileB
    fileC
    fileD
    $ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
    rename(fileA, 1_fileA)
    rename(fileB, 2_fileB)
    rename(fileC, 3_fileC)
    rename(fileD, 4_fileD)
    $ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
    $ ls -1
    1_fileA
    2_fileB
    3_fileC
    4_fileD





    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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
      });


      }
      });






      paweljvn is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1095456%2fhow-can-i-prepend-filenames-with-ascending-numbers-like-1-2%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








      up vote
      4
      down vote



      accepted










      One of the solutions:



      cd <your dir> then run in bash (copy and paste in command-line):



      n=1; for f in *; do mv "$f" "$((n++))_$f"; done


      Bash script case:



      #!/bin/bash
      n=1
      for f in *
      do
      if [ "$f" = "rename.sh" ]
      then
      continue
      fi
      mv "$f" "$((n++))_$f"
      done


      save it as rename.sh to dir with files to rename, chmod +x rename.sh then run it ./rename.sh






      share|improve this answer























      • but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
        – paweljvn
        11 hours ago










      • Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
        – S_Flash
        11 hours ago










      • Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
        – S_Flash
        11 hours ago















      up vote
      4
      down vote



      accepted










      One of the solutions:



      cd <your dir> then run in bash (copy and paste in command-line):



      n=1; for f in *; do mv "$f" "$((n++))_$f"; done


      Bash script case:



      #!/bin/bash
      n=1
      for f in *
      do
      if [ "$f" = "rename.sh" ]
      then
      continue
      fi
      mv "$f" "$((n++))_$f"
      done


      save it as rename.sh to dir with files to rename, chmod +x rename.sh then run it ./rename.sh






      share|improve this answer























      • but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
        – paweljvn
        11 hours ago










      • Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
        – S_Flash
        11 hours ago










      • Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
        – S_Flash
        11 hours ago













      up vote
      4
      down vote



      accepted







      up vote
      4
      down vote



      accepted






      One of the solutions:



      cd <your dir> then run in bash (copy and paste in command-line):



      n=1; for f in *; do mv "$f" "$((n++))_$f"; done


      Bash script case:



      #!/bin/bash
      n=1
      for f in *
      do
      if [ "$f" = "rename.sh" ]
      then
      continue
      fi
      mv "$f" "$((n++))_$f"
      done


      save it as rename.sh to dir with files to rename, chmod +x rename.sh then run it ./rename.sh






      share|improve this answer














      One of the solutions:



      cd <your dir> then run in bash (copy and paste in command-line):



      n=1; for f in *; do mv "$f" "$((n++))_$f"; done


      Bash script case:



      #!/bin/bash
      n=1
      for f in *
      do
      if [ "$f" = "rename.sh" ]
      then
      continue
      fi
      mv "$f" "$((n++))_$f"
      done


      save it as rename.sh to dir with files to rename, chmod +x rename.sh then run it ./rename.sh







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 11 hours ago

























      answered 11 hours ago









      S_Flash

      1,003117




      1,003117












      • but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
        – paweljvn
        11 hours ago










      • Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
        – S_Flash
        11 hours ago










      • Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
        – S_Flash
        11 hours ago


















      • but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
        – paweljvn
        11 hours ago










      • Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
        – S_Flash
        11 hours ago










      • Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
        – S_Flash
        11 hours ago
















      but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
      – paweljvn
      11 hours ago




      but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
      – paweljvn
      11 hours ago












      Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
      – S_Flash
      11 hours ago




      Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
      – S_Flash
      11 hours ago












      Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
      – S_Flash
      11 hours ago




      Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
      – S_Flash
      11 hours ago












      up vote
      3
      down vote













      If there are more than 9 files, I would use printf to pad the number to get the expected sort order, like this



      n=0
      for f in *
      do printf -v new "%2d$((++n))_$f"
      echo mv -v -- "$f" "$new"
      done


      Remove echo when you see the correct result.



      Explanation



      In this line, do printf -v new "%2d$((++n))_$f" we create a format for the new filenames and put it into the variable new.



      %2d is a 2 digit decimal number. Instead of 2d, you can use 3d etc to get another leading 0 (if you have more than 99 files).



      ((++n)) increments the variable n (which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.



      -v makes mv print what will be changed.



      -- in the mv statement is to prevent filenames that start with - being interpreted as options.






      share|improve this answer

























        up vote
        3
        down vote













        If there are more than 9 files, I would use printf to pad the number to get the expected sort order, like this



        n=0
        for f in *
        do printf -v new "%2d$((++n))_$f"
        echo mv -v -- "$f" "$new"
        done


        Remove echo when you see the correct result.



        Explanation



        In this line, do printf -v new "%2d$((++n))_$f" we create a format for the new filenames and put it into the variable new.



        %2d is a 2 digit decimal number. Instead of 2d, you can use 3d etc to get another leading 0 (if you have more than 99 files).



        ((++n)) increments the variable n (which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.



        -v makes mv print what will be changed.



        -- in the mv statement is to prevent filenames that start with - being interpreted as options.






        share|improve this answer























          up vote
          3
          down vote










          up vote
          3
          down vote









          If there are more than 9 files, I would use printf to pad the number to get the expected sort order, like this



          n=0
          for f in *
          do printf -v new "%2d$((++n))_$f"
          echo mv -v -- "$f" "$new"
          done


          Remove echo when you see the correct result.



          Explanation



          In this line, do printf -v new "%2d$((++n))_$f" we create a format for the new filenames and put it into the variable new.



          %2d is a 2 digit decimal number. Instead of 2d, you can use 3d etc to get another leading 0 (if you have more than 99 files).



          ((++n)) increments the variable n (which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.



          -v makes mv print what will be changed.



          -- in the mv statement is to prevent filenames that start with - being interpreted as options.






          share|improve this answer












          If there are more than 9 files, I would use printf to pad the number to get the expected sort order, like this



          n=0
          for f in *
          do printf -v new "%2d$((++n))_$f"
          echo mv -v -- "$f" "$new"
          done


          Remove echo when you see the correct result.



          Explanation



          In this line, do printf -v new "%2d$((++n))_$f" we create a format for the new filenames and put it into the variable new.



          %2d is a 2 digit decimal number. Instead of 2d, you can use 3d etc to get another leading 0 (if you have more than 99 files).



          ((++n)) increments the variable n (which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.



          -v makes mv print what will be changed.



          -- in the mv statement is to prevent filenames that start with - being interpreted as options.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 10 hours ago









          Zanna

          49k13123234




          49k13123234






















              up vote
              1
              down vote















              That’s a job for rename:



              rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *


              This defines a variable, increases it if it’s not set (else it would start with 0 instead of 1) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001, 002, …) use "%03d_". Running it with -n only prints the changes, to actually perform the renaming remove this flag.



              Example run



              $ ls -1
              fileA
              fileB
              fileC
              fileD
              $ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
              rename(fileA, 1_fileA)
              rename(fileB, 2_fileB)
              rename(fileC, 3_fileC)
              rename(fileD, 4_fileD)
              $ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
              $ ls -1
              1_fileA
              2_fileB
              3_fileC
              4_fileD





              share|improve this answer

























                up vote
                1
                down vote















                That’s a job for rename:



                rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *


                This defines a variable, increases it if it’s not set (else it would start with 0 instead of 1) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001, 002, …) use "%03d_". Running it with -n only prints the changes, to actually perform the renaming remove this flag.



                Example run



                $ ls -1
                fileA
                fileB
                fileC
                fileD
                $ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
                rename(fileA, 1_fileA)
                rename(fileB, 2_fileB)
                rename(fileC, 3_fileC)
                rename(fileD, 4_fileD)
                $ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
                $ ls -1
                1_fileA
                2_fileB
                3_fileC
                4_fileD





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote











                  That’s a job for rename:



                  rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *


                  This defines a variable, increases it if it’s not set (else it would start with 0 instead of 1) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001, 002, …) use "%03d_". Running it with -n only prints the changes, to actually perform the renaming remove this flag.



                  Example run



                  $ ls -1
                  fileA
                  fileB
                  fileC
                  fileD
                  $ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
                  rename(fileA, 1_fileA)
                  rename(fileB, 2_fileB)
                  rename(fileC, 3_fileC)
                  rename(fileD, 4_fileD)
                  $ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
                  $ ls -1
                  1_fileA
                  2_fileB
                  3_fileC
                  4_fileD





                  share|improve this answer














                  That’s a job for rename:



                  rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *


                  This defines a variable, increases it if it’s not set (else it would start with 0 instead of 1) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001, 002, …) use "%03d_". Running it with -n only prints the changes, to actually perform the renaming remove this flag.



                  Example run



                  $ ls -1
                  fileA
                  fileB
                  fileC
                  fileD
                  $ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
                  rename(fileA, 1_fileA)
                  rename(fileB, 2_fileB)
                  rename(fileC, 3_fileC)
                  rename(fileD, 4_fileD)
                  $ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
                  $ ls -1
                  1_fileA
                  2_fileB
                  3_fileC
                  4_fileD






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 9 hours ago









                  dessert

                  21.2k55896




                  21.2k55896






















                      paweljvn is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      paweljvn is a new contributor. Be nice, and check out our Code of Conduct.













                      paweljvn is a new contributor. Be nice, and check out our Code of Conduct.












                      paweljvn is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1095456%2fhow-can-i-prepend-filenames-with-ascending-numbers-like-1-2%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

                      Bundesstraße 106

                      Verónica Boquete

                      Ida-Boy-Ed-Garten