Recursively extract rar files












0














I ran into a little problem when extracting rar files recursively. Im using this line find . -name '*.rar' -exec unrar e {} ; -exec rm {} ; && rm *.r* and it seems to work! But the problem is that the files that are being extracted, ends up in the current working folder. When I want them to be extracted in the folder were they are.



This is a example of the file structure, two folders, Holdiday_pictures_greece and Holdiday_pictures_malta. They contain rar files (or rar archives). Holdiday_pictures_greece.rar and the corresponding archive Holdiday_pictures_greece.r01 Holdiday_pictures_greece.r02 and so on.



~$ ls
Holdiday_pictures_greece
Holdiday_pictures_malta

~$ cd Holdiday_pictures_greece
Holdiday_pictures_greece $ ls
Holdiday_pictures_greece.rar
Holdiday_pictures_greece.r01
Holdiday_pictures_greece.r02

Holdiday_pictures_greece $ cd ..
~ $ find . -name '*.rar' -exec unrar e {} ; -exec rm {} ; && rm *.r*
~ $ ls
greece_01.jpg
greece_02.jpg
malta_01.jpg
malta_02.jpg


As you can see from my example above, all files from both archives (Malta and Greece) end up in the ~ directory, were I ran the script. The expected result would be that they ended up in the respective folder, Holdiday_pictures_malta and Holdiday_pictures_greece. There can also be sub folders in the folders. Example Holdiday_pictures_greece/2011 containing more rar files.



I have tried with -execdir, same result, files end up in current dir.



So the expected result would be to loop through all subfolder within the current dir, extract the rar files in the folder where they are found, and then delete the rar archive.










share|improve this question




















  • 4




    Please edit your question and clarify. What folder? Holdiday_pictures_greece? Are all the rar files in the same directory or can they also be in subdirectories? Does it work if you just replace -exec with -execdir?
    – terdon
    May 11 '17 at 13:41










  • May I ask why you think the tags rar, extract irrelevant to the post?
    – Anwar
    May 11 '17 at 13:52












  • You still haven't explained where the rar files are. You seem to be implying you have more than one dir of rar files, if so, please tell us. And we also need to know whether they will always be in the top level, whether you will always have Holdiday_pictures_greece/file1.rar or if you can also have Holdiday_pictures_greece/subDir1/file1.rar. Finally, I ask again, does it work if you just replace -exec with -execdir?
    – terdon
    May 11 '17 at 13:54










  • @terdon The rar files is nested in subfolders yes, and no execdir doesn't work
    – Adam
    May 11 '17 at 14:04








  • 1




    Using the wildcard *.r* is dangerous in that context though - at very least it should be a specific one that says that the characters after 'r' are digits (otherwise, e.g. for photography purposes, you'll end up destroying anything that's saved as .raw).
    – Jez W
    May 11 '17 at 15:52
















0














I ran into a little problem when extracting rar files recursively. Im using this line find . -name '*.rar' -exec unrar e {} ; -exec rm {} ; && rm *.r* and it seems to work! But the problem is that the files that are being extracted, ends up in the current working folder. When I want them to be extracted in the folder were they are.



This is a example of the file structure, two folders, Holdiday_pictures_greece and Holdiday_pictures_malta. They contain rar files (or rar archives). Holdiday_pictures_greece.rar and the corresponding archive Holdiday_pictures_greece.r01 Holdiday_pictures_greece.r02 and so on.



~$ ls
Holdiday_pictures_greece
Holdiday_pictures_malta

~$ cd Holdiday_pictures_greece
Holdiday_pictures_greece $ ls
Holdiday_pictures_greece.rar
Holdiday_pictures_greece.r01
Holdiday_pictures_greece.r02

Holdiday_pictures_greece $ cd ..
~ $ find . -name '*.rar' -exec unrar e {} ; -exec rm {} ; && rm *.r*
~ $ ls
greece_01.jpg
greece_02.jpg
malta_01.jpg
malta_02.jpg


As you can see from my example above, all files from both archives (Malta and Greece) end up in the ~ directory, were I ran the script. The expected result would be that they ended up in the respective folder, Holdiday_pictures_malta and Holdiday_pictures_greece. There can also be sub folders in the folders. Example Holdiday_pictures_greece/2011 containing more rar files.



I have tried with -execdir, same result, files end up in current dir.



So the expected result would be to loop through all subfolder within the current dir, extract the rar files in the folder where they are found, and then delete the rar archive.










share|improve this question




















  • 4




    Please edit your question and clarify. What folder? Holdiday_pictures_greece? Are all the rar files in the same directory or can they also be in subdirectories? Does it work if you just replace -exec with -execdir?
    – terdon
    May 11 '17 at 13:41










  • May I ask why you think the tags rar, extract irrelevant to the post?
    – Anwar
    May 11 '17 at 13:52












  • You still haven't explained where the rar files are. You seem to be implying you have more than one dir of rar files, if so, please tell us. And we also need to know whether they will always be in the top level, whether you will always have Holdiday_pictures_greece/file1.rar or if you can also have Holdiday_pictures_greece/subDir1/file1.rar. Finally, I ask again, does it work if you just replace -exec with -execdir?
    – terdon
    May 11 '17 at 13:54










  • @terdon The rar files is nested in subfolders yes, and no execdir doesn't work
    – Adam
    May 11 '17 at 14:04








  • 1




    Using the wildcard *.r* is dangerous in that context though - at very least it should be a specific one that says that the characters after 'r' are digits (otherwise, e.g. for photography purposes, you'll end up destroying anything that's saved as .raw).
    – Jez W
    May 11 '17 at 15:52














0












0








0







I ran into a little problem when extracting rar files recursively. Im using this line find . -name '*.rar' -exec unrar e {} ; -exec rm {} ; && rm *.r* and it seems to work! But the problem is that the files that are being extracted, ends up in the current working folder. When I want them to be extracted in the folder were they are.



This is a example of the file structure, two folders, Holdiday_pictures_greece and Holdiday_pictures_malta. They contain rar files (or rar archives). Holdiday_pictures_greece.rar and the corresponding archive Holdiday_pictures_greece.r01 Holdiday_pictures_greece.r02 and so on.



~$ ls
Holdiday_pictures_greece
Holdiday_pictures_malta

~$ cd Holdiday_pictures_greece
Holdiday_pictures_greece $ ls
Holdiday_pictures_greece.rar
Holdiday_pictures_greece.r01
Holdiday_pictures_greece.r02

Holdiday_pictures_greece $ cd ..
~ $ find . -name '*.rar' -exec unrar e {} ; -exec rm {} ; && rm *.r*
~ $ ls
greece_01.jpg
greece_02.jpg
malta_01.jpg
malta_02.jpg


As you can see from my example above, all files from both archives (Malta and Greece) end up in the ~ directory, were I ran the script. The expected result would be that they ended up in the respective folder, Holdiday_pictures_malta and Holdiday_pictures_greece. There can also be sub folders in the folders. Example Holdiday_pictures_greece/2011 containing more rar files.



I have tried with -execdir, same result, files end up in current dir.



So the expected result would be to loop through all subfolder within the current dir, extract the rar files in the folder where they are found, and then delete the rar archive.










share|improve this question















I ran into a little problem when extracting rar files recursively. Im using this line find . -name '*.rar' -exec unrar e {} ; -exec rm {} ; && rm *.r* and it seems to work! But the problem is that the files that are being extracted, ends up in the current working folder. When I want them to be extracted in the folder were they are.



This is a example of the file structure, two folders, Holdiday_pictures_greece and Holdiday_pictures_malta. They contain rar files (or rar archives). Holdiday_pictures_greece.rar and the corresponding archive Holdiday_pictures_greece.r01 Holdiday_pictures_greece.r02 and so on.



~$ ls
Holdiday_pictures_greece
Holdiday_pictures_malta

~$ cd Holdiday_pictures_greece
Holdiday_pictures_greece $ ls
Holdiday_pictures_greece.rar
Holdiday_pictures_greece.r01
Holdiday_pictures_greece.r02

Holdiday_pictures_greece $ cd ..
~ $ find . -name '*.rar' -exec unrar e {} ; -exec rm {} ; && rm *.r*
~ $ ls
greece_01.jpg
greece_02.jpg
malta_01.jpg
malta_02.jpg


As you can see from my example above, all files from both archives (Malta and Greece) end up in the ~ directory, were I ran the script. The expected result would be that they ended up in the respective folder, Holdiday_pictures_malta and Holdiday_pictures_greece. There can also be sub folders in the folders. Example Holdiday_pictures_greece/2011 containing more rar files.



I have tried with -execdir, same result, files end up in current dir.



So the expected result would be to loop through all subfolder within the current dir, extract the rar files in the folder where they are found, and then delete the rar archive.







command-line bash extract rar decompress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 11 '17 at 14:34

























asked May 11 '17 at 13:31









Adam

221110




221110








  • 4




    Please edit your question and clarify. What folder? Holdiday_pictures_greece? Are all the rar files in the same directory or can they also be in subdirectories? Does it work if you just replace -exec with -execdir?
    – terdon
    May 11 '17 at 13:41










  • May I ask why you think the tags rar, extract irrelevant to the post?
    – Anwar
    May 11 '17 at 13:52












  • You still haven't explained where the rar files are. You seem to be implying you have more than one dir of rar files, if so, please tell us. And we also need to know whether they will always be in the top level, whether you will always have Holdiday_pictures_greece/file1.rar or if you can also have Holdiday_pictures_greece/subDir1/file1.rar. Finally, I ask again, does it work if you just replace -exec with -execdir?
    – terdon
    May 11 '17 at 13:54










  • @terdon The rar files is nested in subfolders yes, and no execdir doesn't work
    – Adam
    May 11 '17 at 14:04








  • 1




    Using the wildcard *.r* is dangerous in that context though - at very least it should be a specific one that says that the characters after 'r' are digits (otherwise, e.g. for photography purposes, you'll end up destroying anything that's saved as .raw).
    – Jez W
    May 11 '17 at 15:52














  • 4




    Please edit your question and clarify. What folder? Holdiday_pictures_greece? Are all the rar files in the same directory or can they also be in subdirectories? Does it work if you just replace -exec with -execdir?
    – terdon
    May 11 '17 at 13:41










  • May I ask why you think the tags rar, extract irrelevant to the post?
    – Anwar
    May 11 '17 at 13:52












  • You still haven't explained where the rar files are. You seem to be implying you have more than one dir of rar files, if so, please tell us. And we also need to know whether they will always be in the top level, whether you will always have Holdiday_pictures_greece/file1.rar or if you can also have Holdiday_pictures_greece/subDir1/file1.rar. Finally, I ask again, does it work if you just replace -exec with -execdir?
    – terdon
    May 11 '17 at 13:54










  • @terdon The rar files is nested in subfolders yes, and no execdir doesn't work
    – Adam
    May 11 '17 at 14:04








  • 1




    Using the wildcard *.r* is dangerous in that context though - at very least it should be a specific one that says that the characters after 'r' are digits (otherwise, e.g. for photography purposes, you'll end up destroying anything that's saved as .raw).
    – Jez W
    May 11 '17 at 15:52








4




4




Please edit your question and clarify. What folder? Holdiday_pictures_greece? Are all the rar files in the same directory or can they also be in subdirectories? Does it work if you just replace -exec with -execdir?
– terdon
May 11 '17 at 13:41




Please edit your question and clarify. What folder? Holdiday_pictures_greece? Are all the rar files in the same directory or can they also be in subdirectories? Does it work if you just replace -exec with -execdir?
– terdon
May 11 '17 at 13:41












May I ask why you think the tags rar, extract irrelevant to the post?
– Anwar
May 11 '17 at 13:52






May I ask why you think the tags rar, extract irrelevant to the post?
– Anwar
May 11 '17 at 13:52














You still haven't explained where the rar files are. You seem to be implying you have more than one dir of rar files, if so, please tell us. And we also need to know whether they will always be in the top level, whether you will always have Holdiday_pictures_greece/file1.rar or if you can also have Holdiday_pictures_greece/subDir1/file1.rar. Finally, I ask again, does it work if you just replace -exec with -execdir?
– terdon
May 11 '17 at 13:54




You still haven't explained where the rar files are. You seem to be implying you have more than one dir of rar files, if so, please tell us. And we also need to know whether they will always be in the top level, whether you will always have Holdiday_pictures_greece/file1.rar or if you can also have Holdiday_pictures_greece/subDir1/file1.rar. Finally, I ask again, does it work if you just replace -exec with -execdir?
– terdon
May 11 '17 at 13:54












@terdon The rar files is nested in subfolders yes, and no execdir doesn't work
– Adam
May 11 '17 at 14:04






@terdon The rar files is nested in subfolders yes, and no execdir doesn't work
– Adam
May 11 '17 at 14:04






1




1




Using the wildcard *.r* is dangerous in that context though - at very least it should be a specific one that says that the characters after 'r' are digits (otherwise, e.g. for photography purposes, you'll end up destroying anything that's saved as .raw).
– Jez W
May 11 '17 at 15:52




Using the wildcard *.r* is dangerous in that context though - at very least it should be a specific one that says that the characters after 'r' are digits (otherwise, e.g. for photography purposes, you'll end up destroying anything that's saved as .raw).
– Jez W
May 11 '17 at 15:52










2 Answers
2






active

oldest

votes


















1














This is probably simpler to do with a while loop instead of -exec:



find . -type f -name -name '*.rar' -print0 | 
while IFS= read -r -d '' file; do
dir=$(dirname "$file")
rar=$(basename "$file")
cd "$dir"
unrar -e "$rar" && rm "$rar"
cd -
done





share|improve this answer





























    0














    man unrar :



    NON-FREE UNRAR COMPATIBLE SYNOPSIS
    unrar-free [elvx] [-ep] [-o+] [-o-] [-ppassword] [-u] [--] AR‐
    CHIVE [FILE ...] [DESTINATION]



       This syntax should only be used in front-end programs which  are  using
    non-free unrar as a back-end. It is recommended to use this program by
    GNU command line syntax.

    e Extract files from archive without full path.

    l List files in archive.

    v Verbose list files in archive.


    x : Extract files from archive with full path.






    share|improve this answer





















      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',
      autoActivateHeartbeat: false,
      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%2faskubuntu.com%2fquestions%2f914125%2frecursively-extract-rar-files%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









      1














      This is probably simpler to do with a while loop instead of -exec:



      find . -type f -name -name '*.rar' -print0 | 
      while IFS= read -r -d '' file; do
      dir=$(dirname "$file")
      rar=$(basename "$file")
      cd "$dir"
      unrar -e "$rar" && rm "$rar"
      cd -
      done





      share|improve this answer


























        1














        This is probably simpler to do with a while loop instead of -exec:



        find . -type f -name -name '*.rar' -print0 | 
        while IFS= read -r -d '' file; do
        dir=$(dirname "$file")
        rar=$(basename "$file")
        cd "$dir"
        unrar -e "$rar" && rm "$rar"
        cd -
        done





        share|improve this answer
























          1












          1








          1






          This is probably simpler to do with a while loop instead of -exec:



          find . -type f -name -name '*.rar' -print0 | 
          while IFS= read -r -d '' file; do
          dir=$(dirname "$file")
          rar=$(basename "$file")
          cd "$dir"
          unrar -e "$rar" && rm "$rar"
          cd -
          done





          share|improve this answer












          This is probably simpler to do with a while loop instead of -exec:



          find . -type f -name -name '*.rar' -print0 | 
          while IFS= read -r -d '' file; do
          dir=$(dirname "$file")
          rar=$(basename "$file")
          cd "$dir"
          unrar -e "$rar" && rm "$rar"
          cd -
          done






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 11 '17 at 14:45









          terdon

          64.7k12137214




          64.7k12137214

























              0














              man unrar :



              NON-FREE UNRAR COMPATIBLE SYNOPSIS
              unrar-free [elvx] [-ep] [-o+] [-o-] [-ppassword] [-u] [--] AR‐
              CHIVE [FILE ...] [DESTINATION]



                 This syntax should only be used in front-end programs which  are  using
              non-free unrar as a back-end. It is recommended to use this program by
              GNU command line syntax.

              e Extract files from archive without full path.

              l List files in archive.

              v Verbose list files in archive.


              x : Extract files from archive with full path.






              share|improve this answer


























                0














                man unrar :



                NON-FREE UNRAR COMPATIBLE SYNOPSIS
                unrar-free [elvx] [-ep] [-o+] [-o-] [-ppassword] [-u] [--] AR‐
                CHIVE [FILE ...] [DESTINATION]



                   This syntax should only be used in front-end programs which  are  using
                non-free unrar as a back-end. It is recommended to use this program by
                GNU command line syntax.

                e Extract files from archive without full path.

                l List files in archive.

                v Verbose list files in archive.


                x : Extract files from archive with full path.






                share|improve this answer
























                  0












                  0








                  0






                  man unrar :



                  NON-FREE UNRAR COMPATIBLE SYNOPSIS
                  unrar-free [elvx] [-ep] [-o+] [-o-] [-ppassword] [-u] [--] AR‐
                  CHIVE [FILE ...] [DESTINATION]



                     This syntax should only be used in front-end programs which  are  using
                  non-free unrar as a back-end. It is recommended to use this program by
                  GNU command line syntax.

                  e Extract files from archive without full path.

                  l List files in archive.

                  v Verbose list files in archive.


                  x : Extract files from archive with full path.






                  share|improve this answer












                  man unrar :



                  NON-FREE UNRAR COMPATIBLE SYNOPSIS
                  unrar-free [elvx] [-ep] [-o+] [-o-] [-ppassword] [-u] [--] AR‐
                  CHIVE [FILE ...] [DESTINATION]



                     This syntax should only be used in front-end programs which  are  using
                  non-free unrar as a back-end. It is recommended to use this program by
                  GNU command line syntax.

                  e Extract files from archive without full path.

                  l List files in archive.

                  v Verbose list files in archive.


                  x : Extract files from archive with full path.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 22:56









                  ilias iliadis

                  11




                  11






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • 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%2faskubuntu.com%2fquestions%2f914125%2frecursively-extract-rar-files%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

                      數位音樂下載

                      When can things happen in Etherscan, such as the picture below?

                      格利澤436b