Rename multiple files of different name and extensions with parent directory name












0














I'm trying to figure out how to rename multiple files (100s) that have different extensions to their respective sub directory name. Here is the structure:



Parentdir
|--subdir1
|---name1.txt
|---name2.jpeg
|--name3.ovc
|--subdir2
|--name4.txt
|--name5.tiff
|-name6.mpeg


what I would like to do is batch process a rename of all files with their respective subdir name while maintaining their extension.



From the parent directory I was using this command but I'm having issues with the file extensions:



for subdir in *; do mv $subdir/*  $subdir.mpeg; done;









share|improve this question









New contributor




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
















  • 2




    What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?
    – PerlDuck
    Dec 28 '18 at 19:35








  • 1




    Possible duplicate of Search folder, find and copy files to new folder corresponding file ending
    – PerlDuck
    Dec 28 '18 at 19:42










  • Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )
    – Sergiy Kolodyazhnyy
    yesterday
















0














I'm trying to figure out how to rename multiple files (100s) that have different extensions to their respective sub directory name. Here is the structure:



Parentdir
|--subdir1
|---name1.txt
|---name2.jpeg
|--name3.ovc
|--subdir2
|--name4.txt
|--name5.tiff
|-name6.mpeg


what I would like to do is batch process a rename of all files with their respective subdir name while maintaining their extension.



From the parent directory I was using this command but I'm having issues with the file extensions:



for subdir in *; do mv $subdir/*  $subdir.mpeg; done;









share|improve this question









New contributor




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
















  • 2




    What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?
    – PerlDuck
    Dec 28 '18 at 19:35








  • 1




    Possible duplicate of Search folder, find and copy files to new folder corresponding file ending
    – PerlDuck
    Dec 28 '18 at 19:42










  • Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )
    – Sergiy Kolodyazhnyy
    yesterday














0












0








0







I'm trying to figure out how to rename multiple files (100s) that have different extensions to their respective sub directory name. Here is the structure:



Parentdir
|--subdir1
|---name1.txt
|---name2.jpeg
|--name3.ovc
|--subdir2
|--name4.txt
|--name5.tiff
|-name6.mpeg


what I would like to do is batch process a rename of all files with their respective subdir name while maintaining their extension.



From the parent directory I was using this command but I'm having issues with the file extensions:



for subdir in *; do mv $subdir/*  $subdir.mpeg; done;









share|improve this question









New contributor




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











I'm trying to figure out how to rename multiple files (100s) that have different extensions to their respective sub directory name. Here is the structure:



Parentdir
|--subdir1
|---name1.txt
|---name2.jpeg
|--name3.ovc
|--subdir2
|--name4.txt
|--name5.tiff
|-name6.mpeg


what I would like to do is batch process a rename of all files with their respective subdir name while maintaining their extension.



From the parent directory I was using this command but I'm having issues with the file extensions:



for subdir in *; do mv $subdir/*  $subdir.mpeg; done;






rename mv






share|improve this question









New contributor




Rob Pomarico 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




Rob Pomarico 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 yesterday









Sourav Ghosh

39729




39729






New contributor




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









asked Dec 28 '18 at 19:28









Rob Pomarico

1




1




New contributor




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





New contributor





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






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








  • 2




    What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?
    – PerlDuck
    Dec 28 '18 at 19:35








  • 1




    Possible duplicate of Search folder, find and copy files to new folder corresponding file ending
    – PerlDuck
    Dec 28 '18 at 19:42










  • Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )
    – Sergiy Kolodyazhnyy
    yesterday














  • 2




    What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?
    – PerlDuck
    Dec 28 '18 at 19:35








  • 1




    Possible duplicate of Search folder, find and copy files to new folder corresponding file ending
    – PerlDuck
    Dec 28 '18 at 19:42










  • Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )
    – Sergiy Kolodyazhnyy
    yesterday








2




2




What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?
– PerlDuck
Dec 28 '18 at 19:35






What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?
– PerlDuck
Dec 28 '18 at 19:35






1




1




Possible duplicate of Search folder, find and copy files to new folder corresponding file ending
– PerlDuck
Dec 28 '18 at 19:42




Possible duplicate of Search folder, find and copy files to new folder corresponding file ending
– PerlDuck
Dec 28 '18 at 19:42












Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )
– Sergiy Kolodyazhnyy
yesterday




Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )
– Sergiy Kolodyazhnyy
yesterday










1 Answer
1






active

oldest

votes


















0














I conjured this script that can help to achieve what you want:



#!/usr/bin/env bash
set -e

for i in "$1"/*/*
do
old="${i##*/}"
ext="${i##*.}"
name=$(basename "${i%/*}")
[ ! -d "$i" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

done


Test:



parentdir
├── subdir1
│   ├── name1.ovc
│   └── name1.txt
└── subdir2
├── name2.ovc
└── name2.txt


Results:



parentdir
├── subdir1
│   ├── subdir1.ovc
│   └── subdir1.txt
└── subdir2
├── subdir2.ovc
└── subdir2.txt


Usage: ./script.sh Parentdir



Information:





  • old="${i##*/}": returns filename to change (name1.ovc)


  • ext="${i##*.}": returns file extension (mpeg, txt)


  • name=$(basename "${i%/*}"): returns immediate parent directory of target
    file (subdir1)


  • [ ! -d "$i" ]: check if not a directory






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


    }
    });






    Rob Pomarico 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%2f1105196%2frename-multiple-files-of-different-name-and-extensions-with-parent-directory-nam%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I conjured this script that can help to achieve what you want:



    #!/usr/bin/env bash
    set -e

    for i in "$1"/*/*
    do
    old="${i##*/}"
    ext="${i##*.}"
    name=$(basename "${i%/*}")
    [ ! -d "$i" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

    done


    Test:



    parentdir
    ├── subdir1
    │   ├── name1.ovc
    │   └── name1.txt
    └── subdir2
    ├── name2.ovc
    └── name2.txt


    Results:



    parentdir
    ├── subdir1
    │   ├── subdir1.ovc
    │   └── subdir1.txt
    └── subdir2
    ├── subdir2.ovc
    └── subdir2.txt


    Usage: ./script.sh Parentdir



    Information:





    • old="${i##*/}": returns filename to change (name1.ovc)


    • ext="${i##*.}": returns file extension (mpeg, txt)


    • name=$(basename "${i%/*}"): returns immediate parent directory of target
      file (subdir1)


    • [ ! -d "$i" ]: check if not a directory






    share|improve this answer




























      0














      I conjured this script that can help to achieve what you want:



      #!/usr/bin/env bash
      set -e

      for i in "$1"/*/*
      do
      old="${i##*/}"
      ext="${i##*.}"
      name=$(basename "${i%/*}")
      [ ! -d "$i" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

      done


      Test:



      parentdir
      ├── subdir1
      │   ├── name1.ovc
      │   └── name1.txt
      └── subdir2
      ├── name2.ovc
      └── name2.txt


      Results:



      parentdir
      ├── subdir1
      │   ├── subdir1.ovc
      │   └── subdir1.txt
      └── subdir2
      ├── subdir2.ovc
      └── subdir2.txt


      Usage: ./script.sh Parentdir



      Information:





      • old="${i##*/}": returns filename to change (name1.ovc)


      • ext="${i##*.}": returns file extension (mpeg, txt)


      • name=$(basename "${i%/*}"): returns immediate parent directory of target
        file (subdir1)


      • [ ! -d "$i" ]: check if not a directory






      share|improve this answer


























        0












        0








        0






        I conjured this script that can help to achieve what you want:



        #!/usr/bin/env bash
        set -e

        for i in "$1"/*/*
        do
        old="${i##*/}"
        ext="${i##*.}"
        name=$(basename "${i%/*}")
        [ ! -d "$i" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

        done


        Test:



        parentdir
        ├── subdir1
        │   ├── name1.ovc
        │   └── name1.txt
        └── subdir2
        ├── name2.ovc
        └── name2.txt


        Results:



        parentdir
        ├── subdir1
        │   ├── subdir1.ovc
        │   └── subdir1.txt
        └── subdir2
        ├── subdir2.ovc
        └── subdir2.txt


        Usage: ./script.sh Parentdir



        Information:





        • old="${i##*/}": returns filename to change (name1.ovc)


        • ext="${i##*.}": returns file extension (mpeg, txt)


        • name=$(basename "${i%/*}"): returns immediate parent directory of target
          file (subdir1)


        • [ ! -d "$i" ]: check if not a directory






        share|improve this answer














        I conjured this script that can help to achieve what you want:



        #!/usr/bin/env bash
        set -e

        for i in "$1"/*/*
        do
        old="${i##*/}"
        ext="${i##*.}"
        name=$(basename "${i%/*}")
        [ ! -d "$i" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

        done


        Test:



        parentdir
        ├── subdir1
        │   ├── name1.ovc
        │   └── name1.txt
        └── subdir2
        ├── name2.ovc
        └── name2.txt


        Results:



        parentdir
        ├── subdir1
        │   ├── subdir1.ovc
        │   └── subdir1.txt
        └── subdir2
        ├── subdir2.ovc
        └── subdir2.txt


        Usage: ./script.sh Parentdir



        Information:





        • old="${i##*/}": returns filename to change (name1.ovc)


        • ext="${i##*.}": returns file extension (mpeg, txt)


        • name=$(basename "${i%/*}"): returns immediate parent directory of target
          file (subdir1)


        • [ ! -d "$i" ]: check if not a directory







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 28 '18 at 21:03

























        answered Dec 28 '18 at 20:26









        George Udosen

        19.8k94267




        19.8k94267






















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










            draft saved

            draft discarded


















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













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












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
















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f1105196%2frename-multiple-files-of-different-name-and-extensions-with-parent-directory-nam%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

            Human spaceflight

            Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

            張江高科駅