Bash IF folder contains subdirectory named x










-1















I'm trying to make a bash script that adds folders to a zip.



Using this scenario, here is what the outcome I'm looking for something like:



IF /home/*/users EXISTS
ADD the folder containing the subdirectory users to the .zip
FI


I'm pretty new to it all, and I've searched for similar things around, but I haven't found anything.










share|improve this question

















  • 3





    Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, try if [[ -d "$dir" ]]. Syntax for zip can be found here.

    – Paul Hodges
    Nov 12 '18 at 16:48











  • Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *

    – Jordan
    Nov 12 '18 at 17:03











  • Look at Glenn's suggestion below. A for only processes directories that exist, and the name will be in the variable.

    – Paul Hodges
    Nov 12 '18 at 19:37















-1















I'm trying to make a bash script that adds folders to a zip.



Using this scenario, here is what the outcome I'm looking for something like:



IF /home/*/users EXISTS
ADD the folder containing the subdirectory users to the .zip
FI


I'm pretty new to it all, and I've searched for similar things around, but I haven't found anything.










share|improve this question

















  • 3





    Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, try if [[ -d "$dir" ]]. Syntax for zip can be found here.

    – Paul Hodges
    Nov 12 '18 at 16:48











  • Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *

    – Jordan
    Nov 12 '18 at 17:03











  • Look at Glenn's suggestion below. A for only processes directories that exist, and the name will be in the variable.

    – Paul Hodges
    Nov 12 '18 at 19:37













-1












-1








-1








I'm trying to make a bash script that adds folders to a zip.



Using this scenario, here is what the outcome I'm looking for something like:



IF /home/*/users EXISTS
ADD the folder containing the subdirectory users to the .zip
FI


I'm pretty new to it all, and I've searched for similar things around, but I haven't found anything.










share|improve this question














I'm trying to make a bash script that adds folders to a zip.



Using this scenario, here is what the outcome I'm looking for something like:



IF /home/*/users EXISTS
ADD the folder containing the subdirectory users to the .zip
FI


I'm pretty new to it all, and I've searched for similar things around, but I haven't found anything.







linux bash if-statement scripting sh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 16:40









JordanJordan

31




31







  • 3





    Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, try if [[ -d "$dir" ]]. Syntax for zip can be found here.

    – Paul Hodges
    Nov 12 '18 at 16:48











  • Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *

    – Jordan
    Nov 12 '18 at 17:03











  • Look at Glenn's suggestion below. A for only processes directories that exist, and the name will be in the variable.

    – Paul Hodges
    Nov 12 '18 at 19:37












  • 3





    Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, try if [[ -d "$dir" ]]. Syntax for zip can be found here.

    – Paul Hodges
    Nov 12 '18 at 16:48











  • Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *

    – Jordan
    Nov 12 '18 at 17:03











  • Look at Glenn's suggestion below. A for only processes directories that exist, and the name will be in the variable.

    – Paul Hodges
    Nov 12 '18 at 19:37







3




3





Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, try if [[ -d "$dir" ]]. Syntax for zip can be found here.

– Paul Hodges
Nov 12 '18 at 16:48





Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, try if [[ -d "$dir" ]]. Syntax for zip can be found here.

– Paul Hodges
Nov 12 '18 at 16:48













Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *

– Jordan
Nov 12 '18 at 17:03





Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *

– Jordan
Nov 12 '18 at 17:03













Look at Glenn's suggestion below. A for only processes directories that exist, and the name will be in the variable.

– Paul Hodges
Nov 12 '18 at 19:37





Look at Glenn's suggestion below. A for only processes directories that exist, and the name will be in the variable.

– Paul Hodges
Nov 12 '18 at 19:37












3 Answers
3






active

oldest

votes


















0














I would use a for loop and an inline test.



for dir in /home/*/users
do [[ -d "$dir" ]] && : your zip command
done





share|improve this answer






























    0














    You'll want to iterate over the expansion of the wildcard. It sounds like you want:



    for dir in /home/*/; do
    if [[ -d "$dir/users" ]]; then
    add "$dir" to the zip
    fi
    done


    The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile exists, it will not be included in the loop.






    share|improve this answer






























      0














      Consider passing all of the directories to a single zip call. No loop needed.



      zip -r users.zip /home/*/users/


      If you want to zip up the parent directory then:



      zip -r users.zip /home/*/users/..





      share|improve this answer

























      • I like that best, as long as there will certainly be at least one.

        – Paul Hodges
        Nov 12 '18 at 20:05











      • Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.

        – Jordan
        Nov 12 '18 at 23:53










      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      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%2fstackoverflow.com%2fquestions%2f53266512%2fbash-if-folder-contains-subdirectory-named-x%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I would use a for loop and an inline test.



      for dir in /home/*/users
      do [[ -d "$dir" ]] && : your zip command
      done





      share|improve this answer



























        0














        I would use a for loop and an inline test.



        for dir in /home/*/users
        do [[ -d "$dir" ]] && : your zip command
        done





        share|improve this answer

























          0












          0








          0







          I would use a for loop and an inline test.



          for dir in /home/*/users
          do [[ -d "$dir" ]] && : your zip command
          done





          share|improve this answer













          I would use a for loop and an inline test.



          for dir in /home/*/users
          do [[ -d "$dir" ]] && : your zip command
          done






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 19:39









          Paul HodgesPaul Hodges

          3,1311322




          3,1311322























              0














              You'll want to iterate over the expansion of the wildcard. It sounds like you want:



              for dir in /home/*/; do
              if [[ -d "$dir/users" ]]; then
              add "$dir" to the zip
              fi
              done


              The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile exists, it will not be included in the loop.






              share|improve this answer



























                0














                You'll want to iterate over the expansion of the wildcard. It sounds like you want:



                for dir in /home/*/; do
                if [[ -d "$dir/users" ]]; then
                add "$dir" to the zip
                fi
                done


                The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile exists, it will not be included in the loop.






                share|improve this answer

























                  0












                  0








                  0







                  You'll want to iterate over the expansion of the wildcard. It sounds like you want:



                  for dir in /home/*/; do
                  if [[ -d "$dir/users" ]]; then
                  add "$dir" to the zip
                  fi
                  done


                  The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile exists, it will not be included in the loop.






                  share|improve this answer













                  You'll want to iterate over the expansion of the wildcard. It sounds like you want:



                  for dir in /home/*/; do
                  if [[ -d "$dir/users" ]]; then
                  add "$dir" to the zip
                  fi
                  done


                  The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile exists, it will not be included in the loop.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 17:58









                  glenn jackmanglenn jackman

                  166k26143237




                  166k26143237





















                      0














                      Consider passing all of the directories to a single zip call. No loop needed.



                      zip -r users.zip /home/*/users/


                      If you want to zip up the parent directory then:



                      zip -r users.zip /home/*/users/..





                      share|improve this answer

























                      • I like that best, as long as there will certainly be at least one.

                        – Paul Hodges
                        Nov 12 '18 at 20:05











                      • Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.

                        – Jordan
                        Nov 12 '18 at 23:53















                      0














                      Consider passing all of the directories to a single zip call. No loop needed.



                      zip -r users.zip /home/*/users/


                      If you want to zip up the parent directory then:



                      zip -r users.zip /home/*/users/..





                      share|improve this answer

























                      • I like that best, as long as there will certainly be at least one.

                        – Paul Hodges
                        Nov 12 '18 at 20:05











                      • Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.

                        – Jordan
                        Nov 12 '18 at 23:53













                      0












                      0








                      0







                      Consider passing all of the directories to a single zip call. No loop needed.



                      zip -r users.zip /home/*/users/


                      If you want to zip up the parent directory then:



                      zip -r users.zip /home/*/users/..





                      share|improve this answer















                      Consider passing all of the directories to a single zip call. No loop needed.



                      zip -r users.zip /home/*/users/


                      If you want to zip up the parent directory then:



                      zip -r users.zip /home/*/users/..






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 13 '18 at 1:16

























                      answered Nov 12 '18 at 19:44









                      John KugelmanJohn Kugelman

                      241k53400454




                      241k53400454












                      • I like that best, as long as there will certainly be at least one.

                        – Paul Hodges
                        Nov 12 '18 at 20:05











                      • Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.

                        – Jordan
                        Nov 12 '18 at 23:53

















                      • I like that best, as long as there will certainly be at least one.

                        – Paul Hodges
                        Nov 12 '18 at 20:05











                      • Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.

                        – Jordan
                        Nov 12 '18 at 23:53
















                      I like that best, as long as there will certainly be at least one.

                      – Paul Hodges
                      Nov 12 '18 at 20:05





                      I like that best, as long as there will certainly be at least one.

                      – Paul Hodges
                      Nov 12 '18 at 20:05













                      Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.

                      – Jordan
                      Nov 12 '18 at 23:53





                      Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.

                      – Jordan
                      Nov 12 '18 at 23:53

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f53266512%2fbash-if-folder-contains-subdirectory-named-x%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

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo