Rename files incrementally in a specific directory?how to rename and move files according to directory...

What do you call the air that rushes into your car in the highway?

Peter's Strange Word

Offered promotion but I'm leaving. Should I tell?

Does splitting a potentially monolithic application into several smaller ones help prevent bugs?

Good for you! in Russian

Force user to remove USB token

What is the meaning of triple curly braces {{{ }}} in phtml template files? When and how do we use them?

How much stiffer are 23c tires over 28c?

My story is written in English, but is set in my home country. What language should I use for the dialogue?

Grey hair or white hair

BitNot does not flip bits in the way I expected

Good allowance savings plan?

What is the chance of making a successful appeal to dismissal decision from a PhD program after failing the qualifying exam in the 2nd attempt?

How do anti-virus programs start at Windows boot?

2×2×2 rubik's cube corner is twisted!

Aliens englobed the Solar System: will we notice?

The bar has been raised

Is Gradient Descent central to every optimizer?

If the Captain's screens are out, does he switch seats with the co-pilot?

How do I locate a classical quotation?

Placing subfig vertically

Can someone explain what is being said here in color publishing in the American Mathematical Monthly?

How do I express some one as a black person?

What Happens when Passenger Refuses to Fly Boeing 737 Max?



Rename files incrementally in a specific directory?


how to rename and move files according to directory names?Simple CLI to append one file to the end of anotherHow to replace a string on the 5th line of multiple text files?Comparing two text filessplit a file based on pre-defined set of rowsOnly delete files but not folders with rmread file names from files for command linecp directories and files, preserving directories and overwriting filesNeed help with untar command - want to untar from multiple .tar files and then move files to common directoryHow to copy multiple “.gz” files from the sub directories to one directory by grepping a keyword?













7















I have a directory containing bunch of .txt files , I need a command to rename these files by one command , so their name will be : file1.txt , file2.txt, file3.txt , etc .



Any Help ?










share|improve this question

























  • do you mind if it is a python script?

    – Stormvirux
    Mar 8 '14 at 15:18











  • the script should be executed in terminal , but you can write your script .

    – nux
    Mar 8 '14 at 15:19






  • 2





    for i in *.txt; do echo mv "$i" "file${i#file}"; done

    – Fakhri Zulkifli
    Mar 8 '14 at 15:23
















7















I have a directory containing bunch of .txt files , I need a command to rename these files by one command , so their name will be : file1.txt , file2.txt, file3.txt , etc .



Any Help ?










share|improve this question

























  • do you mind if it is a python script?

    – Stormvirux
    Mar 8 '14 at 15:18











  • the script should be executed in terminal , but you can write your script .

    – nux
    Mar 8 '14 at 15:19






  • 2





    for i in *.txt; do echo mv "$i" "file${i#file}"; done

    – Fakhri Zulkifli
    Mar 8 '14 at 15:23














7












7








7








I have a directory containing bunch of .txt files , I need a command to rename these files by one command , so their name will be : file1.txt , file2.txt, file3.txt , etc .



Any Help ?










share|improve this question
















I have a directory containing bunch of .txt files , I need a command to rename these files by one command , so their name will be : file1.txt , file2.txt, file3.txt , etc .



Any Help ?







scripts gnome-terminal command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 '14 at 3:02







nux

















asked Mar 8 '14 at 15:15









nuxnux

23k2996117




23k2996117













  • do you mind if it is a python script?

    – Stormvirux
    Mar 8 '14 at 15:18











  • the script should be executed in terminal , but you can write your script .

    – nux
    Mar 8 '14 at 15:19






  • 2





    for i in *.txt; do echo mv "$i" "file${i#file}"; done

    – Fakhri Zulkifli
    Mar 8 '14 at 15:23



















  • do you mind if it is a python script?

    – Stormvirux
    Mar 8 '14 at 15:18











  • the script should be executed in terminal , but you can write your script .

    – nux
    Mar 8 '14 at 15:19






  • 2





    for i in *.txt; do echo mv "$i" "file${i#file}"; done

    – Fakhri Zulkifli
    Mar 8 '14 at 15:23

















do you mind if it is a python script?

– Stormvirux
Mar 8 '14 at 15:18





do you mind if it is a python script?

– Stormvirux
Mar 8 '14 at 15:18













the script should be executed in terminal , but you can write your script .

– nux
Mar 8 '14 at 15:19





the script should be executed in terminal , but you can write your script .

– nux
Mar 8 '14 at 15:19




2




2





for i in *.txt; do echo mv "$i" "file${i#file}"; done

– Fakhri Zulkifli
Mar 8 '14 at 15:23





for i in *.txt; do echo mv "$i" "file${i#file}"; done

– Fakhri Zulkifli
Mar 8 '14 at 15:23










3 Answers
3






active

oldest

votes


















19














You can use this in terminal to rename files as you wished,



j=1;for i in *.txt; do mv "$i" file"$j".txt; let j=j+1;done


It will do the job.



Explanation:




  • Set a counter j, initially set it to 1

  • Initiate a for loop and use a shell glob *.txt to obtain all txt files.

  • for each file rename it using mv and increase the counter by 1.






share|improve this answer





















  • 1





    can you explain your command ?

    – nux
    Mar 8 '14 at 15:24











  • @nux see the edits and feel free to discuss if necessary.

    – souravc
    Mar 8 '14 at 15:29











  • ok thank you ,i will consider it as a right answer , i found a small script that satisfy my needs too .

    – nux
    Mar 8 '14 at 15:34



















7














You can use the rename command, which is usually included in a default installation:



c=0 rename 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


Use the -n flag if you want to do a test first:



c=0 rename -n 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


The way this works is, for each argument, it executes the perl s/// expression, and performs the rename from the original to the replaced string. In the replacement string I use sprintf to format the name, where I use the environment variable c as the counter from 1.



In most cases you also may need leading "0" for each number, %05d does the trick, where 5 is number of digits.






share|improve this answer





















  • 1





    Great example, should be a top answer. With %05d instead of %d you can add leading 0 to the name, with 5 digits.

    – Kostanos
    5 hours ago



















2














The following command will also rename files incrementally :



cd (directory containing files )


Then run this script :



count=1
for i in *; do
mv "${i}" file${count}.`echo "${i}" | awk -F. '{print $2}'`
((++count))

done





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%2f431345%2frename-files-incrementally-in-a-specific-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









    19














    You can use this in terminal to rename files as you wished,



    j=1;for i in *.txt; do mv "$i" file"$j".txt; let j=j+1;done


    It will do the job.



    Explanation:




    • Set a counter j, initially set it to 1

    • Initiate a for loop and use a shell glob *.txt to obtain all txt files.

    • for each file rename it using mv and increase the counter by 1.






    share|improve this answer





















    • 1





      can you explain your command ?

      – nux
      Mar 8 '14 at 15:24











    • @nux see the edits and feel free to discuss if necessary.

      – souravc
      Mar 8 '14 at 15:29











    • ok thank you ,i will consider it as a right answer , i found a small script that satisfy my needs too .

      – nux
      Mar 8 '14 at 15:34
















    19














    You can use this in terminal to rename files as you wished,



    j=1;for i in *.txt; do mv "$i" file"$j".txt; let j=j+1;done


    It will do the job.



    Explanation:




    • Set a counter j, initially set it to 1

    • Initiate a for loop and use a shell glob *.txt to obtain all txt files.

    • for each file rename it using mv and increase the counter by 1.






    share|improve this answer





















    • 1





      can you explain your command ?

      – nux
      Mar 8 '14 at 15:24











    • @nux see the edits and feel free to discuss if necessary.

      – souravc
      Mar 8 '14 at 15:29











    • ok thank you ,i will consider it as a right answer , i found a small script that satisfy my needs too .

      – nux
      Mar 8 '14 at 15:34














    19












    19








    19







    You can use this in terminal to rename files as you wished,



    j=1;for i in *.txt; do mv "$i" file"$j".txt; let j=j+1;done


    It will do the job.



    Explanation:




    • Set a counter j, initially set it to 1

    • Initiate a for loop and use a shell glob *.txt to obtain all txt files.

    • for each file rename it using mv and increase the counter by 1.






    share|improve this answer















    You can use this in terminal to rename files as you wished,



    j=1;for i in *.txt; do mv "$i" file"$j".txt; let j=j+1;done


    It will do the job.



    Explanation:




    • Set a counter j, initially set it to 1

    • Initiate a for loop and use a shell glob *.txt to obtain all txt files.

    • for each file rename it using mv and increase the counter by 1.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 8 '14 at 15:48









    Avinash Raj

    52.4k41168219




    52.4k41168219










    answered Mar 8 '14 at 15:22









    souravcsouravc

    27.4k1377107




    27.4k1377107








    • 1





      can you explain your command ?

      – nux
      Mar 8 '14 at 15:24











    • @nux see the edits and feel free to discuss if necessary.

      – souravc
      Mar 8 '14 at 15:29











    • ok thank you ,i will consider it as a right answer , i found a small script that satisfy my needs too .

      – nux
      Mar 8 '14 at 15:34














    • 1





      can you explain your command ?

      – nux
      Mar 8 '14 at 15:24











    • @nux see the edits and feel free to discuss if necessary.

      – souravc
      Mar 8 '14 at 15:29











    • ok thank you ,i will consider it as a right answer , i found a small script that satisfy my needs too .

      – nux
      Mar 8 '14 at 15:34








    1




    1





    can you explain your command ?

    – nux
    Mar 8 '14 at 15:24





    can you explain your command ?

    – nux
    Mar 8 '14 at 15:24













    @nux see the edits and feel free to discuss if necessary.

    – souravc
    Mar 8 '14 at 15:29





    @nux see the edits and feel free to discuss if necessary.

    – souravc
    Mar 8 '14 at 15:29













    ok thank you ,i will consider it as a right answer , i found a small script that satisfy my needs too .

    – nux
    Mar 8 '14 at 15:34





    ok thank you ,i will consider it as a right answer , i found a small script that satisfy my needs too .

    – nux
    Mar 8 '14 at 15:34













    7














    You can use the rename command, which is usually included in a default installation:



    c=0 rename 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


    Use the -n flag if you want to do a test first:



    c=0 rename -n 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


    The way this works is, for each argument, it executes the perl s/// expression, and performs the rename from the original to the replaced string. In the replacement string I use sprintf to format the name, where I use the environment variable c as the counter from 1.



    In most cases you also may need leading "0" for each number, %05d does the trick, where 5 is number of digits.






    share|improve this answer





















    • 1





      Great example, should be a top answer. With %05d instead of %d you can add leading 0 to the name, with 5 digits.

      – Kostanos
      5 hours ago
















    7














    You can use the rename command, which is usually included in a default installation:



    c=0 rename 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


    Use the -n flag if you want to do a test first:



    c=0 rename -n 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


    The way this works is, for each argument, it executes the perl s/// expression, and performs the rename from the original to the replaced string. In the replacement string I use sprintf to format the name, where I use the environment variable c as the counter from 1.



    In most cases you also may need leading "0" for each number, %05d does the trick, where 5 is number of digits.






    share|improve this answer





















    • 1





      Great example, should be a top answer. With %05d instead of %d you can add leading 0 to the name, with 5 digits.

      – Kostanos
      5 hours ago














    7












    7








    7







    You can use the rename command, which is usually included in a default installation:



    c=0 rename 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


    Use the -n flag if you want to do a test first:



    c=0 rename -n 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


    The way this works is, for each argument, it executes the perl s/// expression, and performs the rename from the original to the replaced string. In the replacement string I use sprintf to format the name, where I use the environment variable c as the counter from 1.



    In most cases you also may need leading "0" for each number, %05d does the trick, where 5 is number of digits.






    share|improve this answer















    You can use the rename command, which is usually included in a default installation:



    c=0 rename 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


    Use the -n flag if you want to do a test first:



    c=0 rename -n 's/.*/sprintf("file%05d.txt", ++$ENV{c})/e' *


    The way this works is, for each argument, it executes the perl s/// expression, and performs the rename from the original to the replaced string. In the replacement string I use sprintf to format the name, where I use the environment variable c as the counter from 1.



    In most cases you also may need leading "0" for each number, %05d does the trick, where 5 is number of digits.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 15 mins ago









    Kostanos

    1,11711419




    1,11711419










    answered Mar 8 '14 at 15:26









    janosjanos

    3,8561545




    3,8561545








    • 1





      Great example, should be a top answer. With %05d instead of %d you can add leading 0 to the name, with 5 digits.

      – Kostanos
      5 hours ago














    • 1





      Great example, should be a top answer. With %05d instead of %d you can add leading 0 to the name, with 5 digits.

      – Kostanos
      5 hours ago








    1




    1





    Great example, should be a top answer. With %05d instead of %d you can add leading 0 to the name, with 5 digits.

    – Kostanos
    5 hours ago





    Great example, should be a top answer. With %05d instead of %d you can add leading 0 to the name, with 5 digits.

    – Kostanos
    5 hours ago











    2














    The following command will also rename files incrementally :



    cd (directory containing files )


    Then run this script :



    count=1
    for i in *; do
    mv "${i}" file${count}.`echo "${i}" | awk -F. '{print $2}'`
    ((++count))

    done





    share|improve this answer






























      2














      The following command will also rename files incrementally :



      cd (directory containing files )


      Then run this script :



      count=1
      for i in *; do
      mv "${i}" file${count}.`echo "${i}" | awk -F. '{print $2}'`
      ((++count))

      done





      share|improve this answer




























        2












        2








        2







        The following command will also rename files incrementally :



        cd (directory containing files )


        Then run this script :



        count=1
        for i in *; do
        mv "${i}" file${count}.`echo "${i}" | awk -F. '{print $2}'`
        ((++count))

        done





        share|improve this answer















        The following command will also rename files incrementally :



        cd (directory containing files )


        Then run this script :



        count=1
        for i in *; do
        mv "${i}" file${count}.`echo "${i}" | awk -F. '{print $2}'`
        ((++count))

        done






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 '14 at 17:15

























        answered Mar 8 '14 at 15:33









        nuxnux

        23k2996117




        23k2996117






























            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%2f431345%2frename-files-incrementally-in-a-specific-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







            Popular posts from this blog

            Why do type traits not work with types in namespace scope?What are POD types in C++?Why can templates only be...

            Will tsunami waves travel forever if there was no land?Why do tsunami waves begin with the water flowing away...

            Should I use Docker or LXD?How to cache (more) data on SSD/RAM to avoid spin up?Unable to get Windows File...