Swapping file and directory names

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








up vote
2
down vote

favorite












I have a collection of files at ./date-and-time/fixed/path/filename where date-and-time and filename are variable. I would like to move all these files to ./filename/date-and-time. The former path is the filename and the former filename is the path. Is there a good way to do this? There are ~1000 files in total with ~100 distinct filenames.










share|improve this question



























    up vote
    2
    down vote

    favorite












    I have a collection of files at ./date-and-time/fixed/path/filename where date-and-time and filename are variable. I would like to move all these files to ./filename/date-and-time. The former path is the filename and the former filename is the path. Is there a good way to do this? There are ~1000 files in total with ~100 distinct filenames.










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a collection of files at ./date-and-time/fixed/path/filename where date-and-time and filename are variable. I would like to move all these files to ./filename/date-and-time. The former path is the filename and the former filename is the path. Is there a good way to do this? There are ~1000 files in total with ~100 distinct filenames.










      share|improve this question















      I have a collection of files at ./date-and-time/fixed/path/filename where date-and-time and filename are variable. I would like to move all these files to ./filename/date-and-time. The former path is the filename and the former filename is the path. Is there a good way to do this? There are ~1000 files in total with ~100 distinct filenames.







      command-line files batch-rename filename






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 28 at 3:54









      muru

      130k19274467




      130k19274467










      asked Feb 28 at 3:23









      Charles

      222211




      222211




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Something like this should work (note I have echoed the actual commands; please check carefully that it is doing the right thing before removing them)



          #!/bin/bash

          shopt -s nullglob

          for file in */fixed/path/*; do
          [[ -f "$file" ]] || continue
          f="$file##*/"; d="$file%%/*"
          echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
          done





          share|improve this answer






















          • This worked perfectly once I switched from dash to bash. Thanks!
            – Charles
            Feb 28 at 4:42










          • @Charles oops sorry I should have mentioned that - the [[ ... ]] in particular is bash-specific, although in this context [ ... ] should work in either shell
            – steeldriver
            Feb 28 at 4:51










          • No worries, my fault for not specifying. :)
            – Charles
            Feb 28 at 4:52

















          up vote
          2
          down vote













          I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename with variable date-and-time and filename.



          date-and-time can be extracted via awk:



          $ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
          date-and-time


          filename is a bit easier to get as it can be extracted by basename:



          $ basename ./date-and-time/fixed/path/filename
          filename


          From these two code segments, an untested script follows for moving the files in the way you describe when executed in .. Please verify it before use.



          #!/bin/bash

          for i in ./*/fixed/path/*; do
          date_and_time=$(echo "$i" |awk -F "/" 'print $2');
          filename=$(basename "$i");
          mkdir "$filename";
          mv "$i" "$filename"/"$date_and_time";
          done;





          share|improve this answer



























            up vote
            0
            down vote













            Like this :



            mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time



            Edit:



            If you need to create more folders i recommend making a script something like this :



            Before you start, I really recommend you make a backup of the folder you are trying to do this on.



            cd into the folder you trying to operate on and then:



            for file in *; do
            if [[ -f "$file" ]]; then
            mkdir "$file%.*"
            mv "$file" "$file%.*"
            fi
            done


            1. Loop over all (*) the files in the current folder.

            2. create a folder (mkdir) from the file without its extension $file%.*

            3. move (mv) the file into that folder.





            share|improve this answer






















            • I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
              – Charles
              Feb 28 at 3:45










            • You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
              – An0n
              Feb 28 at 3:48










            • Then i recommend using a script. Edited my answer.
              – An0n
              Feb 28 at 3:51










            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',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2faskubuntu.com%2fquestions%2f1010489%2fswapping-file-and-directory-names%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            Something like this should work (note I have echoed the actual commands; please check carefully that it is doing the right thing before removing them)



            #!/bin/bash

            shopt -s nullglob

            for file in */fixed/path/*; do
            [[ -f "$file" ]] || continue
            f="$file##*/"; d="$file%%/*"
            echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
            done





            share|improve this answer






















            • This worked perfectly once I switched from dash to bash. Thanks!
              – Charles
              Feb 28 at 4:42










            • @Charles oops sorry I should have mentioned that - the [[ ... ]] in particular is bash-specific, although in this context [ ... ] should work in either shell
              – steeldriver
              Feb 28 at 4:51










            • No worries, my fault for not specifying. :)
              – Charles
              Feb 28 at 4:52














            up vote
            4
            down vote



            accepted










            Something like this should work (note I have echoed the actual commands; please check carefully that it is doing the right thing before removing them)



            #!/bin/bash

            shopt -s nullglob

            for file in */fixed/path/*; do
            [[ -f "$file" ]] || continue
            f="$file##*/"; d="$file%%/*"
            echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
            done





            share|improve this answer






















            • This worked perfectly once I switched from dash to bash. Thanks!
              – Charles
              Feb 28 at 4:42










            • @Charles oops sorry I should have mentioned that - the [[ ... ]] in particular is bash-specific, although in this context [ ... ] should work in either shell
              – steeldriver
              Feb 28 at 4:51










            • No worries, my fault for not specifying. :)
              – Charles
              Feb 28 at 4:52












            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            Something like this should work (note I have echoed the actual commands; please check carefully that it is doing the right thing before removing them)



            #!/bin/bash

            shopt -s nullglob

            for file in */fixed/path/*; do
            [[ -f "$file" ]] || continue
            f="$file##*/"; d="$file%%/*"
            echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
            done





            share|improve this answer














            Something like this should work (note I have echoed the actual commands; please check carefully that it is doing the right thing before removing them)



            #!/bin/bash

            shopt -s nullglob

            for file in */fixed/path/*; do
            [[ -f "$file" ]] || continue
            f="$file##*/"; d="$file%%/*"
            echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
            done






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 28 at 5:21

























            answered Feb 28 at 4:07









            steeldriver

            63.3k1198167




            63.3k1198167











            • This worked perfectly once I switched from dash to bash. Thanks!
              – Charles
              Feb 28 at 4:42










            • @Charles oops sorry I should have mentioned that - the [[ ... ]] in particular is bash-specific, although in this context [ ... ] should work in either shell
              – steeldriver
              Feb 28 at 4:51










            • No worries, my fault for not specifying. :)
              – Charles
              Feb 28 at 4:52
















            • This worked perfectly once I switched from dash to bash. Thanks!
              – Charles
              Feb 28 at 4:42










            • @Charles oops sorry I should have mentioned that - the [[ ... ]] in particular is bash-specific, although in this context [ ... ] should work in either shell
              – steeldriver
              Feb 28 at 4:51










            • No worries, my fault for not specifying. :)
              – Charles
              Feb 28 at 4:52















            This worked perfectly once I switched from dash to bash. Thanks!
            – Charles
            Feb 28 at 4:42




            This worked perfectly once I switched from dash to bash. Thanks!
            – Charles
            Feb 28 at 4:42












            @Charles oops sorry I should have mentioned that - the [[ ... ]] in particular is bash-specific, although in this context [ ... ] should work in either shell
            – steeldriver
            Feb 28 at 4:51




            @Charles oops sorry I should have mentioned that - the [[ ... ]] in particular is bash-specific, although in this context [ ... ] should work in either shell
            – steeldriver
            Feb 28 at 4:51












            No worries, my fault for not specifying. :)
            – Charles
            Feb 28 at 4:52




            No worries, my fault for not specifying. :)
            – Charles
            Feb 28 at 4:52












            up vote
            2
            down vote













            I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename with variable date-and-time and filename.



            date-and-time can be extracted via awk:



            $ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
            date-and-time


            filename is a bit easier to get as it can be extracted by basename:



            $ basename ./date-and-time/fixed/path/filename
            filename


            From these two code segments, an untested script follows for moving the files in the way you describe when executed in .. Please verify it before use.



            #!/bin/bash

            for i in ./*/fixed/path/*; do
            date_and_time=$(echo "$i" |awk -F "/" 'print $2');
            filename=$(basename "$i");
            mkdir "$filename";
            mv "$i" "$filename"/"$date_and_time";
            done;





            share|improve this answer
























              up vote
              2
              down vote













              I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename with variable date-and-time and filename.



              date-and-time can be extracted via awk:



              $ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
              date-and-time


              filename is a bit easier to get as it can be extracted by basename:



              $ basename ./date-and-time/fixed/path/filename
              filename


              From these two code segments, an untested script follows for moving the files in the way you describe when executed in .. Please verify it before use.



              #!/bin/bash

              for i in ./*/fixed/path/*; do
              date_and_time=$(echo "$i" |awk -F "/" 'print $2');
              filename=$(basename "$i");
              mkdir "$filename";
              mv "$i" "$filename"/"$date_and_time";
              done;





              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename with variable date-and-time and filename.



                date-and-time can be extracted via awk:



                $ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
                date-and-time


                filename is a bit easier to get as it can be extracted by basename:



                $ basename ./date-and-time/fixed/path/filename
                filename


                From these two code segments, an untested script follows for moving the files in the way you describe when executed in .. Please verify it before use.



                #!/bin/bash

                for i in ./*/fixed/path/*; do
                date_and_time=$(echo "$i" |awk -F "/" 'print $2');
                filename=$(basename "$i");
                mkdir "$filename";
                mv "$i" "$filename"/"$date_and_time";
                done;





                share|improve this answer












                I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename with variable date-and-time and filename.



                date-and-time can be extracted via awk:



                $ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
                date-and-time


                filename is a bit easier to get as it can be extracted by basename:



                $ basename ./date-and-time/fixed/path/filename
                filename


                From these two code segments, an untested script follows for moving the files in the way you describe when executed in .. Please verify it before use.



                #!/bin/bash

                for i in ./*/fixed/path/*; do
                date_and_time=$(echo "$i" |awk -F "/" 'print $2');
                filename=$(basename "$i");
                mkdir "$filename";
                mv "$i" "$filename"/"$date_and_time";
                done;






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 28 at 4:04









                dsstorefile1

                1,312111




                1,312111




















                    up vote
                    0
                    down vote













                    Like this :



                    mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time



                    Edit:



                    If you need to create more folders i recommend making a script something like this :



                    Before you start, I really recommend you make a backup of the folder you are trying to do this on.



                    cd into the folder you trying to operate on and then:



                    for file in *; do
                    if [[ -f "$file" ]]; then
                    mkdir "$file%.*"
                    mv "$file" "$file%.*"
                    fi
                    done


                    1. Loop over all (*) the files in the current folder.

                    2. create a folder (mkdir) from the file without its extension $file%.*

                    3. move (mv) the file into that folder.





                    share|improve this answer






















                    • I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
                      – Charles
                      Feb 28 at 3:45










                    • You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
                      – An0n
                      Feb 28 at 3:48










                    • Then i recommend using a script. Edited my answer.
                      – An0n
                      Feb 28 at 3:51














                    up vote
                    0
                    down vote













                    Like this :



                    mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time



                    Edit:



                    If you need to create more folders i recommend making a script something like this :



                    Before you start, I really recommend you make a backup of the folder you are trying to do this on.



                    cd into the folder you trying to operate on and then:



                    for file in *; do
                    if [[ -f "$file" ]]; then
                    mkdir "$file%.*"
                    mv "$file" "$file%.*"
                    fi
                    done


                    1. Loop over all (*) the files in the current folder.

                    2. create a folder (mkdir) from the file without its extension $file%.*

                    3. move (mv) the file into that folder.





                    share|improve this answer






















                    • I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
                      – Charles
                      Feb 28 at 3:45










                    • You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
                      – An0n
                      Feb 28 at 3:48










                    • Then i recommend using a script. Edited my answer.
                      – An0n
                      Feb 28 at 3:51












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Like this :



                    mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time



                    Edit:



                    If you need to create more folders i recommend making a script something like this :



                    Before you start, I really recommend you make a backup of the folder you are trying to do this on.



                    cd into the folder you trying to operate on and then:



                    for file in *; do
                    if [[ -f "$file" ]]; then
                    mkdir "$file%.*"
                    mv "$file" "$file%.*"
                    fi
                    done


                    1. Loop over all (*) the files in the current folder.

                    2. create a folder (mkdir) from the file without its extension $file%.*

                    3. move (mv) the file into that folder.





                    share|improve this answer














                    Like this :



                    mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time



                    Edit:



                    If you need to create more folders i recommend making a script something like this :



                    Before you start, I really recommend you make a backup of the folder you are trying to do this on.



                    cd into the folder you trying to operate on and then:



                    for file in *; do
                    if [[ -f "$file" ]]; then
                    mkdir "$file%.*"
                    mv "$file" "$file%.*"
                    fi
                    done


                    1. Loop over all (*) the files in the current folder.

                    2. create a folder (mkdir) from the file without its extension $file%.*

                    3. move (mv) the file into that folder.






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 28 at 3:51

























                    answered Feb 28 at 3:38









                    An0n

                    80418




                    80418











                    • I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
                      – Charles
                      Feb 28 at 3:45










                    • You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
                      – An0n
                      Feb 28 at 3:48










                    • Then i recommend using a script. Edited my answer.
                      – An0n
                      Feb 28 at 3:51
















                    • I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
                      – Charles
                      Feb 28 at 3:45










                    • You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
                      – An0n
                      Feb 28 at 3:48










                    • Then i recommend using a script. Edited my answer.
                      – An0n
                      Feb 28 at 3:51















                    I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
                    – Charles
                    Feb 28 at 3:45




                    I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
                    – Charles
                    Feb 28 at 3:45












                    You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
                    – An0n
                    Feb 28 at 3:48




                    You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
                    – An0n
                    Feb 28 at 3:48












                    Then i recommend using a script. Edited my answer.
                    – An0n
                    Feb 28 at 3:51




                    Then i recommend using a script. Edited my answer.
                    – An0n
                    Feb 28 at 3:51

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1010489%2fswapping-file-and-directory-names%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    pylint3 and pip3 broken

                    Missing snmpget and snmpwalk

                    How to enroll fingerprints to Ubuntu 17.10 with VFS491