Remove subdirectory from path and promote files to parent











up vote
0
down vote

favorite












I have a bunch of files which contain within their path, a duplicated folder (in the following examples bar). I would like to remove this directory and move any contents in contains into the parent. To use search and replace as a metaphor, I would like to replace /foo/ with / in the path.



Initial State:



foo/bar/some_file.txt
foo/another_file.txt
quux/bar/yet_another_file.txt


Goal:



foo/some_file.txt
foo/another_file.txt
quux/yet_another_file.txt


I have a preference for solving this with bash, but would be open to any solution that doesn't have any dependencies and will work on Linux.





In case this is helpful for anyone who stumbles upon this thread, there is an existing answer for Windows using robocopy.










share|improve this question






















  • This is pretty straightforward with bash Parameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.
    – AFH
    Dec 3 at 14:52










  • @AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
    – Henry Marshall
    Dec 3 at 15:08










  • If fn contains the file name, then ${fn%%/*} is the first element of the path (removing the first '/' and all beyond), and ${fn##*/} is the file name (removing all up to the last '/'). So you simply need to create an mv target from these elements; you don't need external programs for the name processing.
    – AFH
    Dec 3 at 16:37












  • Will this work? mv foo/bar/*.* foo/*.*
    – Biswapriyo
    Dec 4 at 4:05















up vote
0
down vote

favorite












I have a bunch of files which contain within their path, a duplicated folder (in the following examples bar). I would like to remove this directory and move any contents in contains into the parent. To use search and replace as a metaphor, I would like to replace /foo/ with / in the path.



Initial State:



foo/bar/some_file.txt
foo/another_file.txt
quux/bar/yet_another_file.txt


Goal:



foo/some_file.txt
foo/another_file.txt
quux/yet_another_file.txt


I have a preference for solving this with bash, but would be open to any solution that doesn't have any dependencies and will work on Linux.





In case this is helpful for anyone who stumbles upon this thread, there is an existing answer for Windows using robocopy.










share|improve this question






















  • This is pretty straightforward with bash Parameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.
    – AFH
    Dec 3 at 14:52










  • @AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
    – Henry Marshall
    Dec 3 at 15:08










  • If fn contains the file name, then ${fn%%/*} is the first element of the path (removing the first '/' and all beyond), and ${fn##*/} is the file name (removing all up to the last '/'). So you simply need to create an mv target from these elements; you don't need external programs for the name processing.
    – AFH
    Dec 3 at 16:37












  • Will this work? mv foo/bar/*.* foo/*.*
    – Biswapriyo
    Dec 4 at 4:05













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a bunch of files which contain within their path, a duplicated folder (in the following examples bar). I would like to remove this directory and move any contents in contains into the parent. To use search and replace as a metaphor, I would like to replace /foo/ with / in the path.



Initial State:



foo/bar/some_file.txt
foo/another_file.txt
quux/bar/yet_another_file.txt


Goal:



foo/some_file.txt
foo/another_file.txt
quux/yet_another_file.txt


I have a preference for solving this with bash, but would be open to any solution that doesn't have any dependencies and will work on Linux.





In case this is helpful for anyone who stumbles upon this thread, there is an existing answer for Windows using robocopy.










share|improve this question













I have a bunch of files which contain within their path, a duplicated folder (in the following examples bar). I would like to remove this directory and move any contents in contains into the parent. To use search and replace as a metaphor, I would like to replace /foo/ with / in the path.



Initial State:



foo/bar/some_file.txt
foo/another_file.txt
quux/bar/yet_another_file.txt


Goal:



foo/some_file.txt
foo/another_file.txt
quux/yet_another_file.txt


I have a preference for solving this with bash, but would be open to any solution that doesn't have any dependencies and will work on Linux.





In case this is helpful for anyone who stumbles upon this thread, there is an existing answer for Windows using robocopy.







bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 3 at 14:44









Henry Marshall

1414




1414












  • This is pretty straightforward with bash Parameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.
    – AFH
    Dec 3 at 14:52










  • @AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
    – Henry Marshall
    Dec 3 at 15:08










  • If fn contains the file name, then ${fn%%/*} is the first element of the path (removing the first '/' and all beyond), and ${fn##*/} is the file name (removing all up to the last '/'). So you simply need to create an mv target from these elements; you don't need external programs for the name processing.
    – AFH
    Dec 3 at 16:37












  • Will this work? mv foo/bar/*.* foo/*.*
    – Biswapriyo
    Dec 4 at 4:05


















  • This is pretty straightforward with bash Parameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.
    – AFH
    Dec 3 at 14:52










  • @AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
    – Henry Marshall
    Dec 3 at 15:08










  • If fn contains the file name, then ${fn%%/*} is the first element of the path (removing the first '/' and all beyond), and ${fn##*/} is the file name (removing all up to the last '/'). So you simply need to create an mv target from these elements; you don't need external programs for the name processing.
    – AFH
    Dec 3 at 16:37












  • Will this work? mv foo/bar/*.* foo/*.*
    – Biswapriyo
    Dec 4 at 4:05
















This is pretty straightforward with bash Parameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.
– AFH
Dec 3 at 14:52




This is pretty straightforward with bash Parameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.
– AFH
Dec 3 at 14:52












@AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
– Henry Marshall
Dec 3 at 15:08




@AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
– Henry Marshall
Dec 3 at 15:08












If fn contains the file name, then ${fn%%/*} is the first element of the path (removing the first '/' and all beyond), and ${fn##*/} is the file name (removing all up to the last '/'). So you simply need to create an mv target from these elements; you don't need external programs for the name processing.
– AFH
Dec 3 at 16:37






If fn contains the file name, then ${fn%%/*} is the first element of the path (removing the first '/' and all beyond), and ${fn##*/} is the file name (removing all up to the last '/'). So you simply need to create an mv target from these elements; you don't need external programs for the name processing.
– AFH
Dec 3 at 16:37














Will this work? mv foo/bar/*.* foo/*.*
– Biswapriyo
Dec 4 at 4:05




Will this work? mv foo/bar/*.* foo/*.*
– Biswapriyo
Dec 4 at 4:05










1 Answer
1






active

oldest

votes

















up vote
0
down vote













I wound up solving it like this:



for iteration_path in ${1}/*; do
if [[ ! -z $iteration_path ]]; then
mv "${iteration_path}"/bar/* "${iteration_path}";
rmdir "${iteration_path}"/bar;
fi
done


If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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: 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%2fsuperuser.com%2fquestions%2f1380414%2fremove-subdirectory-from-path-and-promote-files-to-parent%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








    up vote
    0
    down vote













    I wound up solving it like this:



    for iteration_path in ${1}/*; do
    if [[ ! -z $iteration_path ]]; then
    mv "${iteration_path}"/bar/* "${iteration_path}";
    rmdir "${iteration_path}"/bar;
    fi
    done


    If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.






    share|improve this answer

























      up vote
      0
      down vote













      I wound up solving it like this:



      for iteration_path in ${1}/*; do
      if [[ ! -z $iteration_path ]]; then
      mv "${iteration_path}"/bar/* "${iteration_path}";
      rmdir "${iteration_path}"/bar;
      fi
      done


      If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I wound up solving it like this:



        for iteration_path in ${1}/*; do
        if [[ ! -z $iteration_path ]]; then
        mv "${iteration_path}"/bar/* "${iteration_path}";
        rmdir "${iteration_path}"/bar;
        fi
        done


        If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.






        share|improve this answer












        I wound up solving it like this:



        for iteration_path in ${1}/*; do
        if [[ ! -z $iteration_path ]]; then
        mv "${iteration_path}"/bar/* "${iteration_path}";
        rmdir "${iteration_path}"/bar;
        fi
        done


        If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 at 2:51









        Henry Marshall

        1414




        1414






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • 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%2fsuperuser.com%2fquestions%2f1380414%2fremove-subdirectory-from-path-and-promote-files-to-parent%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

            Index of /

            Tribalistas

            Listed building