Invert last two subfolders of the directory structure

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











up vote
0
down vote

favorite












Basically, I request a unix way (bash, perl, etc) to invert the last two subfolders, like in this windows question:
https://superuser.com/questions/221/how-can-i-invert-a-directory-structure
This is an example, the objective is to modify the path of lots of files.



i.e. 
/dir1/dir2/dir3/dir4/myfile
to:
/dir1/dir2/dir4/dir3/myfile






share|improve this question






















  • Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process — the path to myfile?
    – Jeff Schaller
    Jan 23 at 21:24










  • show tree /dir1/dir2
    – RomanPerekhrest
    Jan 23 at 21:33






  • 1




    You are unhappy with mv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4 ?
    – Ralph Rönnquist
    Jan 23 at 21:35











  • Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
    – Ferroao
    Jan 23 at 21:43















up vote
0
down vote

favorite












Basically, I request a unix way (bash, perl, etc) to invert the last two subfolders, like in this windows question:
https://superuser.com/questions/221/how-can-i-invert-a-directory-structure
This is an example, the objective is to modify the path of lots of files.



i.e. 
/dir1/dir2/dir3/dir4/myfile
to:
/dir1/dir2/dir4/dir3/myfile






share|improve this question






















  • Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process — the path to myfile?
    – Jeff Schaller
    Jan 23 at 21:24










  • show tree /dir1/dir2
    – RomanPerekhrest
    Jan 23 at 21:33






  • 1




    You are unhappy with mv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4 ?
    – Ralph Rönnquist
    Jan 23 at 21:35











  • Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
    – Ferroao
    Jan 23 at 21:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Basically, I request a unix way (bash, perl, etc) to invert the last two subfolders, like in this windows question:
https://superuser.com/questions/221/how-can-i-invert-a-directory-structure
This is an example, the objective is to modify the path of lots of files.



i.e. 
/dir1/dir2/dir3/dir4/myfile
to:
/dir1/dir2/dir4/dir3/myfile






share|improve this question














Basically, I request a unix way (bash, perl, etc) to invert the last two subfolders, like in this windows question:
https://superuser.com/questions/221/how-can-i-invert-a-directory-structure
This is an example, the objective is to modify the path of lots of files.



i.e. 
/dir1/dir2/dir3/dir4/myfile
to:
/dir1/dir2/dir4/dir3/myfile








share|improve this question













share|improve this question




share|improve this question








edited Jan 23 at 21:41

























asked Jan 23 at 21:18









Ferroao

1256




1256











  • Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process — the path to myfile?
    – Jeff Schaller
    Jan 23 at 21:24










  • show tree /dir1/dir2
    – RomanPerekhrest
    Jan 23 at 21:33






  • 1




    You are unhappy with mv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4 ?
    – Ralph Rönnquist
    Jan 23 at 21:35











  • Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
    – Ferroao
    Jan 23 at 21:43

















  • Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process — the path to myfile?
    – Jeff Schaller
    Jan 23 at 21:24










  • show tree /dir1/dir2
    – RomanPerekhrest
    Jan 23 at 21:33






  • 1




    You are unhappy with mv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4 ?
    – Ralph Rönnquist
    Jan 23 at 21:35











  • Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
    – Ferroao
    Jan 23 at 21:43
















Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process — the path to myfile?
– Jeff Schaller
Jan 23 at 21:24




Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process — the path to myfile?
– Jeff Schaller
Jan 23 at 21:24












show tree /dir1/dir2
– RomanPerekhrest
Jan 23 at 21:33




show tree /dir1/dir2
– RomanPerekhrest
Jan 23 at 21:33




1




1




You are unhappy with mv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4 ?
– Ralph Rönnquist
Jan 23 at 21:35





You are unhappy with mv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4 ?
– Ralph Rönnquist
Jan 23 at 21:35













Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
– Ferroao
Jan 23 at 21:43





Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
– Ferroao
Jan 23 at 21:43











1 Answer
1






active

oldest

votes

















up vote
0
down vote













For a solution with four subdirectories you could use something like this:



for p4 in */*/*/*
do
p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
done


It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d exists and we are going to transpose a/b/c/d, we will end up with a/b/d/c/c.



If you have too many directories matching */*/*/* you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2 and $p3.






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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f419187%2finvert-last-two-subfolders-of-the-directory-structure%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    For a solution with four subdirectories you could use something like this:



    for p4 in */*/*/*
    do
    p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
    mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
    done


    It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d exists and we are going to transpose a/b/c/d, we will end up with a/b/d/c/c.



    If you have too many directories matching */*/*/* you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2 and $p3.






    share|improve this answer
























      up vote
      0
      down vote













      For a solution with four subdirectories you could use something like this:



      for p4 in */*/*/*
      do
      p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
      mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
      done


      It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d exists and we are going to transpose a/b/c/d, we will end up with a/b/d/c/c.



      If you have too many directories matching */*/*/* you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2 and $p3.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        For a solution with four subdirectories you could use something like this:



        for p4 in */*/*/*
        do
        p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
        mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
        done


        It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d exists and we are going to transpose a/b/c/d, we will end up with a/b/d/c/c.



        If you have too many directories matching */*/*/* you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2 and $p3.






        share|improve this answer












        For a solution with four subdirectories you could use something like this:



        for p4 in */*/*/*
        do
        p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
        mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
        done


        It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d exists and we are going to transpose a/b/c/d, we will end up with a/b/d/c/c.



        If you have too many directories matching */*/*/* you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2 and $p3.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 24 at 0:19









        roaima

        39.7k545108




        39.7k545108






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f419187%2finvert-last-two-subfolders-of-the-directory-structure%23new-answer', 'question_page');

            );

            Post as a guest













































































            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?