How to move a folder into itself?

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











up vote
2
down vote

favorite












Ok, that question sounds silly. What I mean is this: I have a directory



foo/
bar.txt
baz.yzw
wun/
a.out


Now, I would like to basically add a directory in between, i.e. I would like to make it



foo/
var1/
bar.txt
baz.yzw
wun/
a.out


with the intent of also adding other stuff to foo, but kept separate from the old contents.



I could of course do it like this:



$ mkdir foo-new
$ mv foo foo-new
$ mv foo-new foo


or



$ cd foo
$ mkdir var1
$ mv $(ls | grep -v var1) var1


but both seem inelegant and are error-prone.



Is there a better way to do it?







share|improve this question























    up vote
    2
    down vote

    favorite












    Ok, that question sounds silly. What I mean is this: I have a directory



    foo/
    bar.txt
    baz.yzw
    wun/
    a.out


    Now, I would like to basically add a directory in between, i.e. I would like to make it



    foo/
    var1/
    bar.txt
    baz.yzw
    wun/
    a.out


    with the intent of also adding other stuff to foo, but kept separate from the old contents.



    I could of course do it like this:



    $ mkdir foo-new
    $ mv foo foo-new
    $ mv foo-new foo


    or



    $ cd foo
    $ mkdir var1
    $ mv $(ls | grep -v var1) var1


    but both seem inelegant and are error-prone.



    Is there a better way to do it?







    share|improve this question





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Ok, that question sounds silly. What I mean is this: I have a directory



      foo/
      bar.txt
      baz.yzw
      wun/
      a.out


      Now, I would like to basically add a directory in between, i.e. I would like to make it



      foo/
      var1/
      bar.txt
      baz.yzw
      wun/
      a.out


      with the intent of also adding other stuff to foo, but kept separate from the old contents.



      I could of course do it like this:



      $ mkdir foo-new
      $ mv foo foo-new
      $ mv foo-new foo


      or



      $ cd foo
      $ mkdir var1
      $ mv $(ls | grep -v var1) var1


      but both seem inelegant and are error-prone.



      Is there a better way to do it?







      share|improve this question











      Ok, that question sounds silly. What I mean is this: I have a directory



      foo/
      bar.txt
      baz.yzw
      wun/
      a.out


      Now, I would like to basically add a directory in between, i.e. I would like to make it



      foo/
      var1/
      bar.txt
      baz.yzw
      wun/
      a.out


      with the intent of also adding other stuff to foo, but kept separate from the old contents.



      I could of course do it like this:



      $ mkdir foo-new
      $ mv foo foo-new
      $ mv foo-new foo


      or



      $ cd foo
      $ mkdir var1
      $ mv $(ls | grep -v var1) var1


      but both seem inelegant and are error-prone.



      Is there a better way to do it?









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 17 at 16:13









      leftaroundabout

      31917




      31917




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          $ cd foo
          $ mkdir var1
          $ mv * var1


          The shell and mv command are smart enough to not try to move the var1 directory into itself.






          share|improve this answer





















          • I should have just tried this... easy! It feels a bit inelegant because of the error message mv: cannot move 'var1' to a subdirectory of itself, 'foo/var1', but on the command-line this is clearly the way to go.
            – leftaroundabout
            May 25 at 16:02

















          up vote
          4
          down vote













          You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo



           $ mkdir var1 
          $ mv foo/* var1
          $ mv var1 foo/





          share|improve this answer




























            up vote
            2
            down vote













            I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/ between foo and its contents:



            d=$(mktemp -d ./tmp.XXXXXX)
            mkdir "$d"; mv foo "$d"/var1; mv "$d" foo


            That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.



            If we want to keep foo intact, then we'll need to create var1 anew, and move the contents. Using Bash:



            cd foo
            mkdir var1
            shopt -s dotglob # to get any dotfiles
            mv * var1 # ignore the error about moving var1 to itself


            Or, to avoid the error, use extglob:



            shopt -s extglob 
            mv !(var1) var1



            Note that $(ls | grep -v) will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).






            share|improve this answer




























              up vote
              1
              down vote













              You can use rsync which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.



              rsync -rv [--remove-source-files] foo/* foo/var1


              There is an option -m, --prune-empty-dirs. that prune empty directory chains from file-list in my rsync man page, but I didn't get that work!



              I put --remove-source-files within braces to be [warn] about that.



              to delete empty directories:



              find foo/ -depth -type d -empty -execdir rmdir "" ;





              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%2f444417%2fhow-to-move-a-folder-into-itself%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                5
                down vote



                accepted










                $ cd foo
                $ mkdir var1
                $ mv * var1


                The shell and mv command are smart enough to not try to move the var1 directory into itself.






                share|improve this answer





















                • I should have just tried this... easy! It feels a bit inelegant because of the error message mv: cannot move 'var1' to a subdirectory of itself, 'foo/var1', but on the command-line this is clearly the way to go.
                  – leftaroundabout
                  May 25 at 16:02














                up vote
                5
                down vote



                accepted










                $ cd foo
                $ mkdir var1
                $ mv * var1


                The shell and mv command are smart enough to not try to move the var1 directory into itself.






                share|improve this answer





















                • I should have just tried this... easy! It feels a bit inelegant because of the error message mv: cannot move 'var1' to a subdirectory of itself, 'foo/var1', but on the command-line this is clearly the way to go.
                  – leftaroundabout
                  May 25 at 16:02












                up vote
                5
                down vote



                accepted







                up vote
                5
                down vote



                accepted






                $ cd foo
                $ mkdir var1
                $ mv * var1


                The shell and mv command are smart enough to not try to move the var1 directory into itself.






                share|improve this answer













                $ cd foo
                $ mkdir var1
                $ mv * var1


                The shell and mv command are smart enough to not try to move the var1 directory into itself.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered May 17 at 16:30









                John

                11.2k11630




                11.2k11630











                • I should have just tried this... easy! It feels a bit inelegant because of the error message mv: cannot move 'var1' to a subdirectory of itself, 'foo/var1', but on the command-line this is clearly the way to go.
                  – leftaroundabout
                  May 25 at 16:02
















                • I should have just tried this... easy! It feels a bit inelegant because of the error message mv: cannot move 'var1' to a subdirectory of itself, 'foo/var1', but on the command-line this is clearly the way to go.
                  – leftaroundabout
                  May 25 at 16:02















                I should have just tried this... easy! It feels a bit inelegant because of the error message mv: cannot move 'var1' to a subdirectory of itself, 'foo/var1', but on the command-line this is clearly the way to go.
                – leftaroundabout
                May 25 at 16:02




                I should have just tried this... easy! It feels a bit inelegant because of the error message mv: cannot move 'var1' to a subdirectory of itself, 'foo/var1', but on the command-line this is clearly the way to go.
                – leftaroundabout
                May 25 at 16:02












                up vote
                4
                down vote













                You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo



                 $ mkdir var1 
                $ mv foo/* var1
                $ mv var1 foo/





                share|improve this answer

























                  up vote
                  4
                  down vote













                  You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo



                   $ mkdir var1 
                  $ mv foo/* var1
                  $ mv var1 foo/





                  share|improve this answer























                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo



                     $ mkdir var1 
                    $ mv foo/* var1
                    $ mv var1 foo/





                    share|improve this answer













                    You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo



                     $ mkdir var1 
                    $ mv foo/* var1
                    $ mv var1 foo/






                    share|improve this answer













                    share|improve this answer



                    share|improve this answer











                    answered May 17 at 16:37









                    Arushix

                    9968




                    9968




















                        up vote
                        2
                        down vote













                        I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/ between foo and its contents:



                        d=$(mktemp -d ./tmp.XXXXXX)
                        mkdir "$d"; mv foo "$d"/var1; mv "$d" foo


                        That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.



                        If we want to keep foo intact, then we'll need to create var1 anew, and move the contents. Using Bash:



                        cd foo
                        mkdir var1
                        shopt -s dotglob # to get any dotfiles
                        mv * var1 # ignore the error about moving var1 to itself


                        Or, to avoid the error, use extglob:



                        shopt -s extglob 
                        mv !(var1) var1



                        Note that $(ls | grep -v) will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).






                        share|improve this answer

























                          up vote
                          2
                          down vote













                          I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/ between foo and its contents:



                          d=$(mktemp -d ./tmp.XXXXXX)
                          mkdir "$d"; mv foo "$d"/var1; mv "$d" foo


                          That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.



                          If we want to keep foo intact, then we'll need to create var1 anew, and move the contents. Using Bash:



                          cd foo
                          mkdir var1
                          shopt -s dotglob # to get any dotfiles
                          mv * var1 # ignore the error about moving var1 to itself


                          Or, to avoid the error, use extglob:



                          shopt -s extglob 
                          mv !(var1) var1



                          Note that $(ls | grep -v) will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).






                          share|improve this answer























                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/ between foo and its contents:



                            d=$(mktemp -d ./tmp.XXXXXX)
                            mkdir "$d"; mv foo "$d"/var1; mv "$d" foo


                            That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.



                            If we want to keep foo intact, then we'll need to create var1 anew, and move the contents. Using Bash:



                            cd foo
                            mkdir var1
                            shopt -s dotglob # to get any dotfiles
                            mv * var1 # ignore the error about moving var1 to itself


                            Or, to avoid the error, use extglob:



                            shopt -s extglob 
                            mv !(var1) var1



                            Note that $(ls | grep -v) will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).






                            share|improve this answer













                            I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/ between foo and its contents:



                            d=$(mktemp -d ./tmp.XXXXXX)
                            mkdir "$d"; mv foo "$d"/var1; mv "$d" foo


                            That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.



                            If we want to keep foo intact, then we'll need to create var1 anew, and move the contents. Using Bash:



                            cd foo
                            mkdir var1
                            shopt -s dotglob # to get any dotfiles
                            mv * var1 # ignore the error about moving var1 to itself


                            Or, to avoid the error, use extglob:



                            shopt -s extglob 
                            mv !(var1) var1



                            Note that $(ls | grep -v) will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).







                            share|improve this answer













                            share|improve this answer



                            share|improve this answer











                            answered May 17 at 16:31









                            ilkkachu

                            48.1k669133




                            48.1k669133




















                                up vote
                                1
                                down vote













                                You can use rsync which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.



                                rsync -rv [--remove-source-files] foo/* foo/var1


                                There is an option -m, --prune-empty-dirs. that prune empty directory chains from file-list in my rsync man page, but I didn't get that work!



                                I put --remove-source-files within braces to be [warn] about that.



                                to delete empty directories:



                                find foo/ -depth -type d -empty -execdir rmdir "" ;





                                share|improve this answer

























                                  up vote
                                  1
                                  down vote













                                  You can use rsync which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.



                                  rsync -rv [--remove-source-files] foo/* foo/var1


                                  There is an option -m, --prune-empty-dirs. that prune empty directory chains from file-list in my rsync man page, but I didn't get that work!



                                  I put --remove-source-files within braces to be [warn] about that.



                                  to delete empty directories:



                                  find foo/ -depth -type d -empty -execdir rmdir "" ;





                                  share|improve this answer























                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    You can use rsync which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.



                                    rsync -rv [--remove-source-files] foo/* foo/var1


                                    There is an option -m, --prune-empty-dirs. that prune empty directory chains from file-list in my rsync man page, but I didn't get that work!



                                    I put --remove-source-files within braces to be [warn] about that.



                                    to delete empty directories:



                                    find foo/ -depth -type d -empty -execdir rmdir "" ;





                                    share|improve this answer













                                    You can use rsync which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.



                                    rsync -rv [--remove-source-files] foo/* foo/var1


                                    There is an option -m, --prune-empty-dirs. that prune empty directory chains from file-list in my rsync man page, but I didn't get that work!



                                    I put --remove-source-files within braces to be [warn] about that.



                                    to delete empty directories:



                                    find foo/ -depth -type d -empty -execdir rmdir "" ;






                                    share|improve this answer













                                    share|improve this answer



                                    share|improve this answer











                                    answered May 17 at 17:08









                                    αғsнιη

                                    14.7k82361




                                    14.7k82361






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444417%2fhow-to-move-a-folder-into-itself%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?