Rename multiple files sequentially and starting by the same prefix












1















I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...










share|improve this question

























  • Likely related: Sequential renaming of files

    – steeldriver
    Jan 8 at 22:42
















1















I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...










share|improve this question

























  • Likely related: Sequential renaming of files

    – steeldriver
    Jan 8 at 22:42














1












1








1








I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...










share|improve this question
















I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...







command-line scripts rename batch-rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 9 at 15:14







singrium

















asked Jan 8 at 22:32









singriumsingrium

1,211424




1,211424













  • Likely related: Sequential renaming of files

    – steeldriver
    Jan 8 at 22:42



















  • Likely related: Sequential renaming of files

    – steeldriver
    Jan 8 at 22:42

















Likely related: Sequential renaming of files

– steeldriver
Jan 8 at 22:42





Likely related: Sequential renaming of files

– steeldriver
Jan 8 at 22:42










2 Answers
2






active

oldest

votes


















1














Try:





count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


Or, if you prefer your commands spread over multiple lines:



count=0
for f in *
do
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
done


How it works





  • count=0



    This initializes the variable count to zero.




  • for f in *; do



    This starts a loop over all files in the current directory.




  • [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"



    [ -f "$f" ] tests to see if the file $f is a regular file (not a directory).



    If $f is a regular file, then the move (mv) command is run. -i tells mv not to overwrite any existing files without asking. "cin_$((++count)).${f##*.}" is the name of the new file. cin_ is the prefix. $((++count)) returns the value of count after it has been incremented. ${f##*.} is the extension of file $f.




  • done



    This marks the end of the loop.




Example



Consider a directory with these three files:



$ ls
alpha.jpg beta.png gamma.txt


Let's run our command:



$ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


After running our command, the files in the directory now are:



$ ls
cin_1.jpg cin_2.png cin_3.txt





share|improve this answer





















  • 1





    That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!

    – singrium
    Jan 8 at 22:50



















1














A rename oneliner:



rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *


This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_ followed by an incrementing number and a dot. With the -n flag it just prints what it would do, remove it to perform the renaming.



Example run



$ ls
a.jpg b.jpg d.png e.png
$ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
rename(a.jpg, cin_1.jpg)
rename(b.jpg, cin_2.jpg)
rename(d.png, cin_3.png)
rename(e.png, cin_4.png)
$ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
$ ls
cin_1.jpg cin_2.jpg cin_3.png cin_4.png


Source: How to rename multiple files sequentially from command line?






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',
    autoActivateHeartbeat: false,
    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1108141%2frename-multiple-files-sequentially-and-starting-by-the-same-prefix%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Try:





    count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


    Or, if you prefer your commands spread over multiple lines:



    count=0
    for f in *
    do
    [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
    done


    How it works





    • count=0



      This initializes the variable count to zero.




    • for f in *; do



      This starts a loop over all files in the current directory.




    • [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"



      [ -f "$f" ] tests to see if the file $f is a regular file (not a directory).



      If $f is a regular file, then the move (mv) command is run. -i tells mv not to overwrite any existing files without asking. "cin_$((++count)).${f##*.}" is the name of the new file. cin_ is the prefix. $((++count)) returns the value of count after it has been incremented. ${f##*.} is the extension of file $f.




    • done



      This marks the end of the loop.




    Example



    Consider a directory with these three files:



    $ ls
    alpha.jpg beta.png gamma.txt


    Let's run our command:



    $ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


    After running our command, the files in the directory now are:



    $ ls
    cin_1.jpg cin_2.png cin_3.txt





    share|improve this answer





















    • 1





      That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!

      – singrium
      Jan 8 at 22:50
















    1














    Try:





    count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


    Or, if you prefer your commands spread over multiple lines:



    count=0
    for f in *
    do
    [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
    done


    How it works





    • count=0



      This initializes the variable count to zero.




    • for f in *; do



      This starts a loop over all files in the current directory.




    • [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"



      [ -f "$f" ] tests to see if the file $f is a regular file (not a directory).



      If $f is a regular file, then the move (mv) command is run. -i tells mv not to overwrite any existing files without asking. "cin_$((++count)).${f##*.}" is the name of the new file. cin_ is the prefix. $((++count)) returns the value of count after it has been incremented. ${f##*.} is the extension of file $f.




    • done



      This marks the end of the loop.




    Example



    Consider a directory with these three files:



    $ ls
    alpha.jpg beta.png gamma.txt


    Let's run our command:



    $ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


    After running our command, the files in the directory now are:



    $ ls
    cin_1.jpg cin_2.png cin_3.txt





    share|improve this answer





















    • 1





      That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!

      – singrium
      Jan 8 at 22:50














    1












    1








    1







    Try:





    count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


    Or, if you prefer your commands spread over multiple lines:



    count=0
    for f in *
    do
    [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
    done


    How it works





    • count=0



      This initializes the variable count to zero.




    • for f in *; do



      This starts a loop over all files in the current directory.




    • [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"



      [ -f "$f" ] tests to see if the file $f is a regular file (not a directory).



      If $f is a regular file, then the move (mv) command is run. -i tells mv not to overwrite any existing files without asking. "cin_$((++count)).${f##*.}" is the name of the new file. cin_ is the prefix. $((++count)) returns the value of count after it has been incremented. ${f##*.} is the extension of file $f.




    • done



      This marks the end of the loop.




    Example



    Consider a directory with these three files:



    $ ls
    alpha.jpg beta.png gamma.txt


    Let's run our command:



    $ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


    After running our command, the files in the directory now are:



    $ ls
    cin_1.jpg cin_2.png cin_3.txt





    share|improve this answer















    Try:





    count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


    Or, if you prefer your commands spread over multiple lines:



    count=0
    for f in *
    do
    [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
    done


    How it works





    • count=0



      This initializes the variable count to zero.




    • for f in *; do



      This starts a loop over all files in the current directory.




    • [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"



      [ -f "$f" ] tests to see if the file $f is a regular file (not a directory).



      If $f is a regular file, then the move (mv) command is run. -i tells mv not to overwrite any existing files without asking. "cin_$((++count)).${f##*.}" is the name of the new file. cin_ is the prefix. $((++count)) returns the value of count after it has been incremented. ${f##*.} is the extension of file $f.




    • done



      This marks the end of the loop.




    Example



    Consider a directory with these three files:



    $ ls
    alpha.jpg beta.png gamma.txt


    Let's run our command:



    $ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done


    After running our command, the files in the directory now are:



    $ ls
    cin_1.jpg cin_2.png cin_3.txt






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 8 at 22:53









    dessert

    23.3k565103




    23.3k565103










    answered Jan 8 at 22:45









    John1024John1024

    9,9692434




    9,9692434








    • 1





      That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!

      – singrium
      Jan 8 at 22:50














    • 1





      That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!

      – singrium
      Jan 8 at 22:50








    1




    1





    That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!

    – singrium
    Jan 8 at 22:50





    That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!

    – singrium
    Jan 8 at 22:50













    1














    A rename oneliner:



    rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *


    This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_ followed by an incrementing number and a dot. With the -n flag it just prints what it would do, remove it to perform the renaming.



    Example run



    $ ls
    a.jpg b.jpg d.png e.png
    $ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
    rename(a.jpg, cin_1.jpg)
    rename(b.jpg, cin_2.jpg)
    rename(d.png, cin_3.png)
    rename(e.png, cin_4.png)
    $ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
    $ ls
    cin_1.jpg cin_2.jpg cin_3.png cin_4.png


    Source: How to rename multiple files sequentially from command line?






    share|improve this answer




























      1














      A rename oneliner:



      rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *


      This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_ followed by an incrementing number and a dot. With the -n flag it just prints what it would do, remove it to perform the renaming.



      Example run



      $ ls
      a.jpg b.jpg d.png e.png
      $ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
      rename(a.jpg, cin_1.jpg)
      rename(b.jpg, cin_2.jpg)
      rename(d.png, cin_3.png)
      rename(e.png, cin_4.png)
      $ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
      $ ls
      cin_1.jpg cin_2.jpg cin_3.png cin_4.png


      Source: How to rename multiple files sequentially from command line?






      share|improve this answer


























        1












        1








        1







        A rename oneliner:



        rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *


        This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_ followed by an incrementing number and a dot. With the -n flag it just prints what it would do, remove it to perform the renaming.



        Example run



        $ ls
        a.jpg b.jpg d.png e.png
        $ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
        rename(a.jpg, cin_1.jpg)
        rename(b.jpg, cin_2.jpg)
        rename(d.png, cin_3.png)
        rename(e.png, cin_4.png)
        $ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
        $ ls
        cin_1.jpg cin_2.jpg cin_3.png cin_4.png


        Source: How to rename multiple files sequentially from command line?






        share|improve this answer













        A rename oneliner:



        rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *


        This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_ followed by an incrementing number and a dot. With the -n flag it just prints what it would do, remove it to perform the renaming.



        Example run



        $ ls
        a.jpg b.jpg d.png e.png
        $ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
        rename(a.jpg, cin_1.jpg)
        rename(b.jpg, cin_2.jpg)
        rename(d.png, cin_3.png)
        rename(e.png, cin_4.png)
        $ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
        $ ls
        cin_1.jpg cin_2.jpg cin_3.png cin_4.png


        Source: How to rename multiple files sequentially from command line?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 8 at 22:52









        dessertdessert

        23.3k565103




        23.3k565103






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1108141%2frename-multiple-files-sequentially-and-starting-by-the-same-prefix%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

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?