BASH file mass rename with counter

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












5















I want to rename all the files in a folder with PREFIX+COUNTER+FILENAME



for ex.
input:



england.txt 
canada.txt
france.txt


output:



CO_01_england.txt 
CO_02_canada.txt
CO_03_france.txt









share|improve this question
























  • How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?

    – Wildcard
    May 8 '16 at 6:41












  • no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)

    – user3148655
    May 8 '16 at 8:04











  • But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.

    – Wildcard
    May 8 '16 at 20:43











  • yes. you're correct

    – user3148655
    May 9 '16 at 4:14















5















I want to rename all the files in a folder with PREFIX+COUNTER+FILENAME



for ex.
input:



england.txt 
canada.txt
france.txt


output:



CO_01_england.txt 
CO_02_canada.txt
CO_03_france.txt









share|improve this question
























  • How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?

    – Wildcard
    May 8 '16 at 6:41












  • no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)

    – user3148655
    May 8 '16 at 8:04











  • But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.

    – Wildcard
    May 8 '16 at 20:43











  • yes. you're correct

    – user3148655
    May 9 '16 at 4:14













5












5








5








I want to rename all the files in a folder with PREFIX+COUNTER+FILENAME



for ex.
input:



england.txt 
canada.txt
france.txt


output:



CO_01_england.txt 
CO_02_canada.txt
CO_03_france.txt









share|improve this question
















I want to rename all the files in a folder with PREFIX+COUNTER+FILENAME



for ex.
input:



england.txt 
canada.txt
france.txt


output:



CO_01_england.txt 
CO_02_canada.txt
CO_03_france.txt






bash rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 10 '17 at 23:00









рüффп

78331529




78331529










asked May 8 '16 at 6:00









user3148655user3148655

7728




7728












  • How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?

    – Wildcard
    May 8 '16 at 6:41












  • no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)

    – user3148655
    May 8 '16 at 8:04











  • But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.

    – Wildcard
    May 8 '16 at 20:43











  • yes. you're correct

    – user3148655
    May 9 '16 at 4:14

















  • How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?

    – Wildcard
    May 8 '16 at 6:41












  • no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)

    – user3148655
    May 8 '16 at 8:04











  • But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.

    – Wildcard
    May 8 '16 at 20:43











  • yes. you're correct

    – user3148655
    May 9 '16 at 4:14
















How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?

– Wildcard
May 8 '16 at 6:41






How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?

– Wildcard
May 8 '16 at 6:41














no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)

– user3148655
May 8 '16 at 8:04





no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)

– user3148655
May 8 '16 at 8:04













But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.

– Wildcard
May 8 '16 at 20:43





But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.

– Wildcard
May 8 '16 at 20:43













yes. you're correct

– user3148655
May 9 '16 at 4:14





yes. you're correct

– user3148655
May 9 '16 at 4:14










2 Answers
2






active

oldest

votes


















9














This does what you ask:



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


How it works




  • n=1



    This initializes the variable n to 1.




  • for f in *.txt; do



    This starts a loop over all files in the current directory whose names end with .txt.




  • mv "$f" "CO_$((n++))_$f"



    This renames the files to have the CO_ prefix with n as the counter. The ++ symbol tells bash to increment the variable n.




  • done



    This signals the end of the loop.



Improvement



This version uses printf which allows greater control over how the number will be formatted:



n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done


In particular, the %02i format will put a leading zero before the number when n is still in single digits.






share|improve this answer

























  • Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt

    – user3148655
    May 8 '16 at 6:58












  • @user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.

    – Gilles
    May 8 '16 at 20:06


















1














With the prename utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle:



prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt


Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_ followed by the counter value $n (incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _, followed by the original name. To rename all files with the .txt extension, with the numbering in lexicographic order:



prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt


With zsh, use the zmv function, and the parameter expansion flag l to pad the number:



autoload -U zmv
zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'





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',
    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%2f281794%2fbash-file-mass-rename-with-counter%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









    9














    This does what you ask:



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


    How it works




    • n=1



      This initializes the variable n to 1.




    • for f in *.txt; do



      This starts a loop over all files in the current directory whose names end with .txt.




    • mv "$f" "CO_$((n++))_$f"



      This renames the files to have the CO_ prefix with n as the counter. The ++ symbol tells bash to increment the variable n.




    • done



      This signals the end of the loop.



    Improvement



    This version uses printf which allows greater control over how the number will be formatted:



    n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done


    In particular, the %02i format will put a leading zero before the number when n is still in single digits.






    share|improve this answer

























    • Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt

      – user3148655
      May 8 '16 at 6:58












    • @user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.

      – Gilles
      May 8 '16 at 20:06















    9














    This does what you ask:



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


    How it works




    • n=1



      This initializes the variable n to 1.




    • for f in *.txt; do



      This starts a loop over all files in the current directory whose names end with .txt.




    • mv "$f" "CO_$((n++))_$f"



      This renames the files to have the CO_ prefix with n as the counter. The ++ symbol tells bash to increment the variable n.




    • done



      This signals the end of the loop.



    Improvement



    This version uses printf which allows greater control over how the number will be formatted:



    n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done


    In particular, the %02i format will put a leading zero before the number when n is still in single digits.






    share|improve this answer

























    • Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt

      – user3148655
      May 8 '16 at 6:58












    • @user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.

      – Gilles
      May 8 '16 at 20:06













    9












    9








    9







    This does what you ask:



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


    How it works




    • n=1



      This initializes the variable n to 1.




    • for f in *.txt; do



      This starts a loop over all files in the current directory whose names end with .txt.




    • mv "$f" "CO_$((n++))_$f"



      This renames the files to have the CO_ prefix with n as the counter. The ++ symbol tells bash to increment the variable n.




    • done



      This signals the end of the loop.



    Improvement



    This version uses printf which allows greater control over how the number will be formatted:



    n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done


    In particular, the %02i format will put a leading zero before the number when n is still in single digits.






    share|improve this answer















    This does what you ask:



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


    How it works




    • n=1



      This initializes the variable n to 1.




    • for f in *.txt; do



      This starts a loop over all files in the current directory whose names end with .txt.




    • mv "$f" "CO_$((n++))_$f"



      This renames the files to have the CO_ prefix with n as the counter. The ++ symbol tells bash to increment the variable n.




    • done



      This signals the end of the loop.



    Improvement



    This version uses printf which allows greater control over how the number will be formatted:



    n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done


    In particular, the %02i format will put a leading zero before the number when n is still in single digits.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 21 '16 at 11:11









    Raphael Ahrens

    7,05152846




    7,05152846










    answered May 8 '16 at 6:04









    John1024John1024

    47.4k5110125




    47.4k5110125












    • Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt

      – user3148655
      May 8 '16 at 6:58












    • @user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.

      – Gilles
      May 8 '16 at 20:06

















    • Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt

      – user3148655
      May 8 '16 at 6:58












    • @user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.

      – Gilles
      May 8 '16 at 20:06
















    Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt

    – user3148655
    May 8 '16 at 6:58






    Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt

    – user3148655
    May 8 '16 at 6:58














    @user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.

    – Gilles
    May 8 '16 at 20:06





    @user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.

    – Gilles
    May 8 '16 at 20:06













    1














    With the prename utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle:



    prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt


    Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_ followed by the counter value $n (incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _, followed by the original name. To rename all files with the .txt extension, with the numbering in lexicographic order:



    prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt


    With zsh, use the zmv function, and the parameter expansion flag l to pad the number:



    autoload -U zmv
    zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'





    share|improve this answer



























      1














      With the prename utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle:



      prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt


      Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_ followed by the counter value $n (incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _, followed by the original name. To rename all files with the .txt extension, with the numbering in lexicographic order:



      prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt


      With zsh, use the zmv function, and the parameter expansion flag l to pad the number:



      autoload -U zmv
      zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'





      share|improve this answer

























        1












        1








        1







        With the prename utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle:



        prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt


        Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_ followed by the counter value $n (incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _, followed by the original name. To rename all files with the .txt extension, with the numbering in lexicographic order:



        prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt


        With zsh, use the zmv function, and the parameter expansion flag l to pad the number:



        autoload -U zmv
        zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'





        share|improve this answer













        With the prename utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle:



        prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt


        Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_ followed by the counter value $n (incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _, followed by the original name. To rename all files with the .txt extension, with the numbering in lexicographic order:



        prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt


        With zsh, use the zmv function, and the parameter expansion flag l to pad the number:



        autoload -U zmv
        zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 8 '16 at 23:17









        GillesGilles

        540k12810941607




        540k12810941607



























            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%2f281794%2fbash-file-mass-rename-with-counter%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?