Move contents of all sub subdirectories up into just their subdirectories

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

up vote
0
down vote

favorite

The problem I am facing is that I have a directory that contains thousands of subdirectories, each of those subdirectories contain more subdirectories, and inside of all of those are images.

What I have are thousands of these:

/1056/7624/image.png

I basically want to eliminate the 7624 directory here so that I end up with thousands of these instead:

/1056/image.png

I tried mv */*/* */* but that just freaked out… is this even possible to do with a terminal command?

I’m trying to do this so I can use this multifile uploader without going into 50 directories just to grab 50 images.

share|improve this question

    up vote
    0
    down vote

    favorite

    The problem I am facing is that I have a directory that contains thousands of subdirectories, each of those subdirectories contain more subdirectories, and inside of all of those are images.

    What I have are thousands of these:

    /1056/7624/image.png

    I basically want to eliminate the 7624 directory here so that I end up with thousands of these instead:

    /1056/image.png

    I tried mv */*/* */* but that just freaked out… is this even possible to do with a terminal command?

    I’m trying to do this so I can use this multifile uploader without going into 50 directories just to grab 50 images.

    share|improve this question

      up vote
      0
      down vote

      favorite

      up vote
      0
      down vote

      favorite

      The problem I am facing is that I have a directory that contains thousands of subdirectories, each of those subdirectories contain more subdirectories, and inside of all of those are images.

      What I have are thousands of these:

      /1056/7624/image.png

      I basically want to eliminate the 7624 directory here so that I end up with thousands of these instead:

      /1056/image.png

      I tried mv */*/* */* but that just freaked out… is this even possible to do with a terminal command?

      I’m trying to do this so I can use this multifile uploader without going into 50 directories just to grab 50 images.

      share|improve this question

      The problem I am facing is that I have a directory that contains thousands of subdirectories, each of those subdirectories contain more subdirectories, and inside of all of those are images.

      What I have are thousands of these:

      /1056/7624/image.png

      I basically want to eliminate the 7624 directory here so that I end up with thousands of these instead:

      /1056/image.png

      I tried mv */*/* */* but that just freaked out… is this even possible to do with a terminal command?

      I’m trying to do this so I can use this multifile uploader without going into 50 directories just to grab 50 images.

      files move

      share|improve this question

      share|improve this question

      share|improve this question

      share|improve this question

      edited Nov 29 at 16:15

      asked Nov 29 at 16:09

      Octoxan

      1033

      1033

          2 Answers
          2

          active

          oldest

          votes

          up vote
          1
          down vote

          accepted

          for dir in */*; do
            if [[ -d "$dir" ]]; then
              ( 
                cd "$dir"
                mv -n * ..
                cd ..
                rmdir "$( basename "$dir" )"
              )
            fi
          done
          

          Be aware that any duplicated file or subdirectory names will not be moved and so the deep directories in those cases will not be removed, due to still containing files.

          share|improve this answer

          • So run this from the directory that contains the initial subdirectories?
            – Octoxan
            Nov 29 at 16:20

          • Given your example of /1056/7624/image.png, run this from the directory that contains the 1056 directory.
            – DopeGhoti
            Nov 29 at 16:21

          • Oddly I get a bunch of rmdir: failed to remove '1103/1560': No such file or directory for every one of them
            – Octoxan
            Nov 29 at 16:22

          • It is moving the images up one directory though, just not removing the subdirectories, even though I can verify the ones in the error do exist.
            – Octoxan
            Nov 29 at 16:23

          • Fixed, I forgot to trim the outer directory from the rmdir.
            – DopeGhoti
            Nov 29 at 16:23

          up vote
          -1
          down vote

          this will move all files from current dir and any sub-dirs, up to the current level. Renaming any files with the same name with a number appended.

          find ./ -type f -print -exec mv --backup=numbered {} . ;
          

          then you can make sure you got everything with a

          find ./ -mindepth 2 -type f
          

          share|improve this answer

          • downvotes for literally no reason. i like it.
            – Wayne
            Dec 3 at 18:32

          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f484961%2fmove-contents-of-all-sub-subdirectories-up-into-just-their-subdirectories%23new-answer’, ‘question_page’);
          }
          );

          Post as a guest

          Required, but never shown

          2 Answers
          2

          active

          oldest

          votes

          2 Answers
          2

          active

          oldest

          votes

          active

          oldest

          votes

          active

          oldest

          votes

          up vote
          1
          down vote

          accepted

          for dir in */*; do
            if [[ -d "$dir" ]]; then
              ( 
                cd "$dir"
                mv -n * ..
                cd ..
                rmdir "$( basename "$dir" )"
              )
            fi
          done
          

          Be aware that any duplicated file or subdirectory names will not be moved and so the deep directories in those cases will not be removed, due to still containing files.

          share|improve this answer

          • So run this from the directory that contains the initial subdirectories?
            – Octoxan
            Nov 29 at 16:20

          • Given your example of /1056/7624/image.png, run this from the directory that contains the 1056 directory.
            – DopeGhoti
            Nov 29 at 16:21

          • Oddly I get a bunch of rmdir: failed to remove '1103/1560': No such file or directory for every one of them
            – Octoxan
            Nov 29 at 16:22

          • It is moving the images up one directory though, just not removing the subdirectories, even though I can verify the ones in the error do exist.
            – Octoxan
            Nov 29 at 16:23

          • Fixed, I forgot to trim the outer directory from the rmdir.
            – DopeGhoti
            Nov 29 at 16:23

          up vote
          1
          down vote

          accepted

          for dir in */*; do
            if [[ -d "$dir" ]]; then
              ( 
                cd "$dir"
                mv -n * ..
                cd ..
                rmdir "$( basename "$dir" )"
              )
            fi
          done
          

          Be aware that any duplicated file or subdirectory names will not be moved and so the deep directories in those cases will not be removed, due to still containing files.

          share|improve this answer

          • So run this from the directory that contains the initial subdirectories?
            – Octoxan
            Nov 29 at 16:20

          • Given your example of /1056/7624/image.png, run this from the directory that contains the 1056 directory.
            – DopeGhoti
            Nov 29 at 16:21

          • Oddly I get a bunch of rmdir: failed to remove '1103/1560': No such file or directory for every one of them
            – Octoxan
            Nov 29 at 16:22

          • It is moving the images up one directory though, just not removing the subdirectories, even though I can verify the ones in the error do exist.
            – Octoxan
            Nov 29 at 16:23

          • Fixed, I forgot to trim the outer directory from the rmdir.
            – DopeGhoti
            Nov 29 at 16:23

          up vote
          1
          down vote

          accepted

          up vote
          1
          down vote

          accepted

          for dir in */*; do
            if [[ -d "$dir" ]]; then
              ( 
                cd "$dir"
                mv -n * ..
                cd ..
                rmdir "$( basename "$dir" )"
              )
            fi
          done
          

          Be aware that any duplicated file or subdirectory names will not be moved and so the deep directories in those cases will not be removed, due to still containing files.

          share|improve this answer

          for dir in */*; do
            if [[ -d "$dir" ]]; then
              ( 
                cd "$dir"
                mv -n * ..
                cd ..
                rmdir "$( basename "$dir" )"
              )
            fi
          done
          

          Be aware that any duplicated file or subdirectory names will not be moved and so the deep directories in those cases will not be removed, due to still containing files.

          share|improve this answer

          share|improve this answer

          share|improve this answer

          edited Nov 29 at 16:23

          answered Nov 29 at 16:17

          DopeGhoti

          42.9k55382

          42.9k55382

          • So run this from the directory that contains the initial subdirectories?
            – Octoxan
            Nov 29 at 16:20

          • Given your example of /1056/7624/image.png, run this from the directory that contains the 1056 directory.
            – DopeGhoti
            Nov 29 at 16:21

          • Oddly I get a bunch of rmdir: failed to remove '1103/1560': No such file or directory for every one of them
            – Octoxan
            Nov 29 at 16:22

          • It is moving the images up one directory though, just not removing the subdirectories, even though I can verify the ones in the error do exist.
            – Octoxan
            Nov 29 at 16:23

          • Fixed, I forgot to trim the outer directory from the rmdir.
            – DopeGhoti
            Nov 29 at 16:23

          • So run this from the directory that contains the initial subdirectories?
            – Octoxan
            Nov 29 at 16:20

          • Given your example of /1056/7624/image.png, run this from the directory that contains the 1056 directory.
            – DopeGhoti
            Nov 29 at 16:21

          • Oddly I get a bunch of rmdir: failed to remove '1103/1560': No such file or directory for every one of them
            – Octoxan
            Nov 29 at 16:22

          • It is moving the images up one directory though, just not removing the subdirectories, even though I can verify the ones in the error do exist.
            – Octoxan
            Nov 29 at 16:23

          • Fixed, I forgot to trim the outer directory from the rmdir.
            – DopeGhoti
            Nov 29 at 16:23

          So run this from the directory that contains the initial subdirectories?
          – Octoxan
          Nov 29 at 16:20

          So run this from the directory that contains the initial subdirectories?
          – Octoxan
          Nov 29 at 16:20

          Given your example of /1056/7624/image.png, run this from the directory that contains the 1056 directory.
          – DopeGhoti
          Nov 29 at 16:21

          Given your example of /1056/7624/image.png, run this from the directory that contains the 1056 directory.
          – DopeGhoti
          Nov 29 at 16:21

          Oddly I get a bunch of rmdir: failed to remove '1103/1560': No such file or directory for every one of them
          – Octoxan
          Nov 29 at 16:22

          Oddly I get a bunch of rmdir: failed to remove '1103/1560': No such file or directory for every one of them
          – Octoxan
          Nov 29 at 16:22

          It is moving the images up one directory though, just not removing the subdirectories, even though I can verify the ones in the error do exist.
          – Octoxan
          Nov 29 at 16:23

          It is moving the images up one directory though, just not removing the subdirectories, even though I can verify the ones in the error do exist.
          – Octoxan
          Nov 29 at 16:23

          Fixed, I forgot to trim the outer directory from the rmdir.
          – DopeGhoti
          Nov 29 at 16:23

          Fixed, I forgot to trim the outer directory from the rmdir.
          – DopeGhoti
          Nov 29 at 16:23

          up vote
          -1
          down vote

          this will move all files from current dir and any sub-dirs, up to the current level. Renaming any files with the same name with a number appended.

          find ./ -type f -print -exec mv --backup=numbered {} . ;
          

          then you can make sure you got everything with a

          find ./ -mindepth 2 -type f
          

          share|improve this answer

          • downvotes for literally no reason. i like it.
            – Wayne
            Dec 3 at 18:32

          up vote
          -1
          down vote

          this will move all files from current dir and any sub-dirs, up to the current level. Renaming any files with the same name with a number appended.

          find ./ -type f -print -exec mv --backup=numbered {} . ;
          

          then you can make sure you got everything with a

          find ./ -mindepth 2 -type f
          

          share|improve this answer

          • downvotes for literally no reason. i like it.
            – Wayne
            Dec 3 at 18:32

          up vote
          -1
          down vote

          up vote
          -1
          down vote

          this will move all files from current dir and any sub-dirs, up to the current level. Renaming any files with the same name with a number appended.

          find ./ -type f -print -exec mv --backup=numbered {} . ;
          

          then you can make sure you got everything with a

          find ./ -mindepth 2 -type f
          

          share|improve this answer

          this will move all files from current dir and any sub-dirs, up to the current level. Renaming any files with the same name with a number appended.

          find ./ -type f -print -exec mv --backup=numbered {} . ;
          

          then you can make sure you got everything with a

          find ./ -mindepth 2 -type f
          

          share|improve this answer

          share|improve this answer

          share|improve this answer

          edited Nov 29 at 21:29

          answered Nov 29 at 19:46

          Wayne

          443

          443

          • downvotes for literally no reason. i like it.
            – Wayne
            Dec 3 at 18:32

          • downvotes for literally no reason. i like it.
            – Wayne
            Dec 3 at 18:32

          downvotes for literally no reason. i like it.
          – Wayne
          Dec 3 at 18:32

          downvotes for literally no reason. i like it.
          – Wayne
          Dec 3 at 18:32

          draft saved
          draft discarded

          Thanks for contributing an answer to Unix & Linux Stack Exchange!

          • 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%2funix.stackexchange.com%2fquestions%2f484961%2fmove-contents-of-all-sub-subdirectories-up-into-just-their-subdirectories%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

          Related Post

          Leave a Reply

          Your email address will not be published. Required fields are marked *