Rename multiple files in sub directories to their directory names











up vote
0
down vote

favorite












I have a folder on my desktop named Models. In the folder, there are named folders with jpeg files without the .jpg extensions. The jpeg file names are random hashes. I would like to use a bash script to batch rename these jpeg files to their directory names with increments and append the .jpg extension on each file.



It's basically something like



Models/



 Alice/
a5ccB2ff3e
ee420bc4a
2acee54dc
...
Alex/
de33fa24c0
d1eaa48e0a
...


And I want to to be like



Models/



 Alice/
Alice001.jpg
Alice002.jpg
Alice003.jpg
...

Alex/
Alex001.jpg
Alex002.jpg
...









share|improve this question









New contributor




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
















  • 1




    Please show us, what have you tried so far
    – mrc02_kr
    Nov 18 at 7:01










  • I don't even know how to do a sub directory search. Trying to show what I tried would just be toying. I would try in php but bash is a different language.
    – Boygee
    Nov 18 at 7:17















up vote
0
down vote

favorite












I have a folder on my desktop named Models. In the folder, there are named folders with jpeg files without the .jpg extensions. The jpeg file names are random hashes. I would like to use a bash script to batch rename these jpeg files to their directory names with increments and append the .jpg extension on each file.



It's basically something like



Models/



 Alice/
a5ccB2ff3e
ee420bc4a
2acee54dc
...
Alex/
de33fa24c0
d1eaa48e0a
...


And I want to to be like



Models/



 Alice/
Alice001.jpg
Alice002.jpg
Alice003.jpg
...

Alex/
Alex001.jpg
Alex002.jpg
...









share|improve this question









New contributor




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
















  • 1




    Please show us, what have you tried so far
    – mrc02_kr
    Nov 18 at 7:01










  • I don't even know how to do a sub directory search. Trying to show what I tried would just be toying. I would try in php but bash is a different language.
    – Boygee
    Nov 18 at 7:17













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a folder on my desktop named Models. In the folder, there are named folders with jpeg files without the .jpg extensions. The jpeg file names are random hashes. I would like to use a bash script to batch rename these jpeg files to their directory names with increments and append the .jpg extension on each file.



It's basically something like



Models/



 Alice/
a5ccB2ff3e
ee420bc4a
2acee54dc
...
Alex/
de33fa24c0
d1eaa48e0a
...


And I want to to be like



Models/



 Alice/
Alice001.jpg
Alice002.jpg
Alice003.jpg
...

Alex/
Alex001.jpg
Alex002.jpg
...









share|improve this question









New contributor




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











I have a folder on my desktop named Models. In the folder, there are named folders with jpeg files without the .jpg extensions. The jpeg file names are random hashes. I would like to use a bash script to batch rename these jpeg files to their directory names with increments and append the .jpg extension on each file.



It's basically something like



Models/



 Alice/
a5ccB2ff3e
ee420bc4a
2acee54dc
...
Alex/
de33fa24c0
d1eaa48e0a
...


And I want to to be like



Models/



 Alice/
Alice001.jpg
Alice002.jpg
Alice003.jpg
...

Alex/
Alex001.jpg
Alex002.jpg
...






linux shell-script files






share|improve this question









New contributor




Boygee 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




Boygee 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 Nov 18 at 7:01





















New contributor




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









asked Nov 18 at 6:59









Boygee

33




33




New contributor




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





New contributor





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






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








  • 1




    Please show us, what have you tried so far
    – mrc02_kr
    Nov 18 at 7:01










  • I don't even know how to do a sub directory search. Trying to show what I tried would just be toying. I would try in php but bash is a different language.
    – Boygee
    Nov 18 at 7:17














  • 1




    Please show us, what have you tried so far
    – mrc02_kr
    Nov 18 at 7:01










  • I don't even know how to do a sub directory search. Trying to show what I tried would just be toying. I would try in php but bash is a different language.
    – Boygee
    Nov 18 at 7:17








1




1




Please show us, what have you tried so far
– mrc02_kr
Nov 18 at 7:01




Please show us, what have you tried so far
– mrc02_kr
Nov 18 at 7:01












I don't even know how to do a sub directory search. Trying to show what I tried would just be toying. I would try in php but bash is a different language.
– Boygee
Nov 18 at 7:17




I don't even know how to do a sub directory search. Trying to show what I tried would just be toying. I would try in php but bash is a different language.
– Boygee
Nov 18 at 7:17










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










From the description of your question I am assuming that total no. of files in a folder does not exceed 999.



This is a simple bash script run it in your top directory i.e Models:



#!/bin/bash
for i in ./*
do
if [ -d "$i" ]
then
j=001
for k in "$i"/*
do
mv "$k" "$i"/"$i""$j".jpg
j=${j#0}
if [ ${j:0:1} -eq 0 ]
then
j=${j#0}
fi
(( j++ ))
j=`printf '%03i' $j`
done
fi
done


It will first check every file in Models, whether it is a directory or not by if [ -d "$i" ] . Then for each file in this directory it will change their names by mv "$k" "$i"/"$i""$j".jpg. And



j=${j#0}
if [ ${j:0:1} -eq 0 ]
then
j=${j#0}
fi


Above code will remove all the zeroes from beginning so that you will not get the error like:



 ((: 008: value too great for base (error token is "008")
printf: 008: invalid octal number


And



j=`printf '%03i' $j`


will make value of j 3 digits long.






share|improve this answer























  • It worked partially but threw an error. It only renamed 8 pictures in each folder and deleted the rest (I have backup, don't worry). Then it printed this in the console: /home/myName/Desktop/for.sh: line 13: ((: 008: value too great for base (error token is "008")
    – Boygee
    Nov 18 at 8:35












  • @Boygee updated answer.
    – Debian_yadav
    Nov 18 at 9:39


















up vote
0
down vote













Try also this simplified version of Debian_yadav's proposal:



for i in *
do if [ -d "$i" ] && cd "$i"
then j=0
for k in *
do ((j++))
echo mv "$k" $(printf "%s%03d.jpg" $i $j )
done
cd ..
fi
done


The echo is for debugging only; remove if happy with what you see.



It cds into each directory available, resets the counter, loops across all target files, increments the counter for each, and the does the rename.






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


    }
    });






    Boygee 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%2funix.stackexchange.com%2fquestions%2f482481%2frename-multiple-files-in-sub-directories-to-their-directory-names%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








    up vote
    0
    down vote



    accepted










    From the description of your question I am assuming that total no. of files in a folder does not exceed 999.



    This is a simple bash script run it in your top directory i.e Models:



    #!/bin/bash
    for i in ./*
    do
    if [ -d "$i" ]
    then
    j=001
    for k in "$i"/*
    do
    mv "$k" "$i"/"$i""$j".jpg
    j=${j#0}
    if [ ${j:0:1} -eq 0 ]
    then
    j=${j#0}
    fi
    (( j++ ))
    j=`printf '%03i' $j`
    done
    fi
    done


    It will first check every file in Models, whether it is a directory or not by if [ -d "$i" ] . Then for each file in this directory it will change their names by mv "$k" "$i"/"$i""$j".jpg. And



    j=${j#0}
    if [ ${j:0:1} -eq 0 ]
    then
    j=${j#0}
    fi


    Above code will remove all the zeroes from beginning so that you will not get the error like:



     ((: 008: value too great for base (error token is "008")
    printf: 008: invalid octal number


    And



    j=`printf '%03i' $j`


    will make value of j 3 digits long.






    share|improve this answer























    • It worked partially but threw an error. It only renamed 8 pictures in each folder and deleted the rest (I have backup, don't worry). Then it printed this in the console: /home/myName/Desktop/for.sh: line 13: ((: 008: value too great for base (error token is "008")
      – Boygee
      Nov 18 at 8:35












    • @Boygee updated answer.
      – Debian_yadav
      Nov 18 at 9:39















    up vote
    0
    down vote



    accepted










    From the description of your question I am assuming that total no. of files in a folder does not exceed 999.



    This is a simple bash script run it in your top directory i.e Models:



    #!/bin/bash
    for i in ./*
    do
    if [ -d "$i" ]
    then
    j=001
    for k in "$i"/*
    do
    mv "$k" "$i"/"$i""$j".jpg
    j=${j#0}
    if [ ${j:0:1} -eq 0 ]
    then
    j=${j#0}
    fi
    (( j++ ))
    j=`printf '%03i' $j`
    done
    fi
    done


    It will first check every file in Models, whether it is a directory or not by if [ -d "$i" ] . Then for each file in this directory it will change their names by mv "$k" "$i"/"$i""$j".jpg. And



    j=${j#0}
    if [ ${j:0:1} -eq 0 ]
    then
    j=${j#0}
    fi


    Above code will remove all the zeroes from beginning so that you will not get the error like:



     ((: 008: value too great for base (error token is "008")
    printf: 008: invalid octal number


    And



    j=`printf '%03i' $j`


    will make value of j 3 digits long.






    share|improve this answer























    • It worked partially but threw an error. It only renamed 8 pictures in each folder and deleted the rest (I have backup, don't worry). Then it printed this in the console: /home/myName/Desktop/for.sh: line 13: ((: 008: value too great for base (error token is "008")
      – Boygee
      Nov 18 at 8:35












    • @Boygee updated answer.
      – Debian_yadav
      Nov 18 at 9:39













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    From the description of your question I am assuming that total no. of files in a folder does not exceed 999.



    This is a simple bash script run it in your top directory i.e Models:



    #!/bin/bash
    for i in ./*
    do
    if [ -d "$i" ]
    then
    j=001
    for k in "$i"/*
    do
    mv "$k" "$i"/"$i""$j".jpg
    j=${j#0}
    if [ ${j:0:1} -eq 0 ]
    then
    j=${j#0}
    fi
    (( j++ ))
    j=`printf '%03i' $j`
    done
    fi
    done


    It will first check every file in Models, whether it is a directory or not by if [ -d "$i" ] . Then for each file in this directory it will change their names by mv "$k" "$i"/"$i""$j".jpg. And



    j=${j#0}
    if [ ${j:0:1} -eq 0 ]
    then
    j=${j#0}
    fi


    Above code will remove all the zeroes from beginning so that you will not get the error like:



     ((: 008: value too great for base (error token is "008")
    printf: 008: invalid octal number


    And



    j=`printf '%03i' $j`


    will make value of j 3 digits long.






    share|improve this answer














    From the description of your question I am assuming that total no. of files in a folder does not exceed 999.



    This is a simple bash script run it in your top directory i.e Models:



    #!/bin/bash
    for i in ./*
    do
    if [ -d "$i" ]
    then
    j=001
    for k in "$i"/*
    do
    mv "$k" "$i"/"$i""$j".jpg
    j=${j#0}
    if [ ${j:0:1} -eq 0 ]
    then
    j=${j#0}
    fi
    (( j++ ))
    j=`printf '%03i' $j`
    done
    fi
    done


    It will first check every file in Models, whether it is a directory or not by if [ -d "$i" ] . Then for each file in this directory it will change their names by mv "$k" "$i"/"$i""$j".jpg. And



    j=${j#0}
    if [ ${j:0:1} -eq 0 ]
    then
    j=${j#0}
    fi


    Above code will remove all the zeroes from beginning so that you will not get the error like:



     ((: 008: value too great for base (error token is "008")
    printf: 008: invalid octal number


    And



    j=`printf '%03i' $j`


    will make value of j 3 digits long.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 18 at 10:07

























    answered Nov 18 at 7:24









    Debian_yadav

    1,2193922




    1,2193922












    • It worked partially but threw an error. It only renamed 8 pictures in each folder and deleted the rest (I have backup, don't worry). Then it printed this in the console: /home/myName/Desktop/for.sh: line 13: ((: 008: value too great for base (error token is "008")
      – Boygee
      Nov 18 at 8:35












    • @Boygee updated answer.
      – Debian_yadav
      Nov 18 at 9:39


















    • It worked partially but threw an error. It only renamed 8 pictures in each folder and deleted the rest (I have backup, don't worry). Then it printed this in the console: /home/myName/Desktop/for.sh: line 13: ((: 008: value too great for base (error token is "008")
      – Boygee
      Nov 18 at 8:35












    • @Boygee updated answer.
      – Debian_yadav
      Nov 18 at 9:39
















    It worked partially but threw an error. It only renamed 8 pictures in each folder and deleted the rest (I have backup, don't worry). Then it printed this in the console: /home/myName/Desktop/for.sh: line 13: ((: 008: value too great for base (error token is "008")
    – Boygee
    Nov 18 at 8:35






    It worked partially but threw an error. It only renamed 8 pictures in each folder and deleted the rest (I have backup, don't worry). Then it printed this in the console: /home/myName/Desktop/for.sh: line 13: ((: 008: value too great for base (error token is "008")
    – Boygee
    Nov 18 at 8:35














    @Boygee updated answer.
    – Debian_yadav
    Nov 18 at 9:39




    @Boygee updated answer.
    – Debian_yadav
    Nov 18 at 9:39












    up vote
    0
    down vote













    Try also this simplified version of Debian_yadav's proposal:



    for i in *
    do if [ -d "$i" ] && cd "$i"
    then j=0
    for k in *
    do ((j++))
    echo mv "$k" $(printf "%s%03d.jpg" $i $j )
    done
    cd ..
    fi
    done


    The echo is for debugging only; remove if happy with what you see.



    It cds into each directory available, resets the counter, loops across all target files, increments the counter for each, and the does the rename.






    share|improve this answer

























      up vote
      0
      down vote













      Try also this simplified version of Debian_yadav's proposal:



      for i in *
      do if [ -d "$i" ] && cd "$i"
      then j=0
      for k in *
      do ((j++))
      echo mv "$k" $(printf "%s%03d.jpg" $i $j )
      done
      cd ..
      fi
      done


      The echo is for debugging only; remove if happy with what you see.



      It cds into each directory available, resets the counter, loops across all target files, increments the counter for each, and the does the rename.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Try also this simplified version of Debian_yadav's proposal:



        for i in *
        do if [ -d "$i" ] && cd "$i"
        then j=0
        for k in *
        do ((j++))
        echo mv "$k" $(printf "%s%03d.jpg" $i $j )
        done
        cd ..
        fi
        done


        The echo is for debugging only; remove if happy with what you see.



        It cds into each directory available, resets the counter, loops across all target files, increments the counter for each, and the does the rename.






        share|improve this answer












        Try also this simplified version of Debian_yadav's proposal:



        for i in *
        do if [ -d "$i" ] && cd "$i"
        then j=0
        for k in *
        do ((j++))
        echo mv "$k" $(printf "%s%03d.jpg" $i $j )
        done
        cd ..
        fi
        done


        The echo is for debugging only; remove if happy with what you see.



        It cds into each directory available, resets the counter, loops across all target files, increments the counter for each, and the does the rename.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 at 12:26









        RudiC

        3,1311211




        3,1311211






















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










             

            draft saved


            draft discarded


















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













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












            Boygee 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%2funix.stackexchange.com%2fquestions%2f482481%2frename-multiple-files-in-sub-directories-to-their-directory-names%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

            Morgemoulin

            Scott Moir

            Souastre