Checking for the existence of multiple directoriesWhat are the shell's control and redirection operators?Why...

Disable the ">" operator in Rstudio linux terminal

Word or phrase for showing great skill at something without formal training in it

Solubility of a tribasic weak acid

How to deal with an incendiary email that was recalled

Would these multi-classing house rules cause unintended problems?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

"Free" Hopf algebra

Find x angle in triangle

Is there a standard way to treat events with unknown times (missing time data)?

Can you combine War Caster, whip, and Warlock Features to EB enemies with reach?

Why Normality assumption in linear regression

How does Arcane Armament interact with the Artillerist Artificer's Wand Prototype feature?

Guns against regular people

What is this metal M-shaped device for?

Caruana vs Carlsen game 10 (WCC) why not 18...Nxb6?

Contest math problem about crossing out numbers in the table

How should I handle players who ignore the session zero agreement?

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

Breaking a Loop in Tikz

Blindfold battle as a gladiatorial spectacle - what are the tactics and communication methods?

Why would the Pakistan airspace closure cancel flights not headed to Pakistan itself?

Is a debit card dangerous in my situation?

Am I a Rude Number?

Explain the objections to these measures against human trafficking



Checking for the existence of multiple directories


What are the shell's control and redirection operators?Why is /dev/null a file? Why isn't its function implemented as a simple program?What does '>/dev/null 2>&1' mean in this article of crontab basics?Checking for the existence of files against a listFind latest versions of multiple files in multiple directoriesTest for existence of multiple files, given by pipeRunning command on multiple files within multiple directoriesScript that asks for four words, then tells the user the word they chose. Output error?How can I separate directories to different partitions when installing OpenSUSE Leap (and other Linux distros)?shell script continues before file unzippedBash script to recursively obtain file and directory permissions, compare them to desired permissions and return if correct or notMove multiple files from multiple directories to source-relative destinationWhich quoting style GNU Bash variable definitions (mostly for paths)?













1















I want to check for the existence of multiple directories, say, dir1, dir2 and dir3, in the working directory.



I have the following



if [ -d "$PWD/dir1" ] && [ -d "$PWD/dir2" ] && [ -d "$PWD/dir3" ]; then
echo True
else
echo False
fi


But I suspect there is a more elegant way of doing this. Do not assume that there is a rule in the names of the directories.



The goal is to check for the existence of a few directories and for the nonexistence of others.



I'm using Bash, but portable code is preferred.










share|improve this question









New contributor




Elegance is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    1















    I want to check for the existence of multiple directories, say, dir1, dir2 and dir3, in the working directory.



    I have the following



    if [ -d "$PWD/dir1" ] && [ -d "$PWD/dir2" ] && [ -d "$PWD/dir3" ]; then
    echo True
    else
    echo False
    fi


    But I suspect there is a more elegant way of doing this. Do not assume that there is a rule in the names of the directories.



    The goal is to check for the existence of a few directories and for the nonexistence of others.



    I'm using Bash, but portable code is preferred.










    share|improve this question









    New contributor




    Elegance is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1








      I want to check for the existence of multiple directories, say, dir1, dir2 and dir3, in the working directory.



      I have the following



      if [ -d "$PWD/dir1" ] && [ -d "$PWD/dir2" ] && [ -d "$PWD/dir3" ]; then
      echo True
      else
      echo False
      fi


      But I suspect there is a more elegant way of doing this. Do not assume that there is a rule in the names of the directories.



      The goal is to check for the existence of a few directories and for the nonexistence of others.



      I'm using Bash, but portable code is preferred.










      share|improve this question









      New contributor




      Elegance is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I want to check for the existence of multiple directories, say, dir1, dir2 and dir3, in the working directory.



      I have the following



      if [ -d "$PWD/dir1" ] && [ -d "$PWD/dir2" ] && [ -d "$PWD/dir3" ]; then
      echo True
      else
      echo False
      fi


      But I suspect there is a more elegant way of doing this. Do not assume that there is a rule in the names of the directories.



      The goal is to check for the existence of a few directories and for the nonexistence of others.



      I'm using Bash, but portable code is preferred.







      shell-script shell files directory control-flow






      share|improve this question









      New contributor




      Elegance is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Elegance is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 35 mins ago







      Elegance













      New contributor




      Elegance is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      EleganceElegance

      83




      83




      New contributor




      Elegance is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Elegance is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Elegance is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          3 Answers
          3






          active

          oldest

          votes


















          2














          If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":



          ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" >/dev/null 2>&1 && echo All there


          I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears -- it is not written to your terminal.






          share|improve this answer


























          • Can you help me understand this command? I know what file descriptors are. I know 1 is stdout, 2 is stderr and I know what redirecting is. I don't understand the significance of /dev/null, and I do not know how to parse the command.

            – Elegance
            1 hour ago











          • @Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…

            – Jeff Schaller
            1 hour ago











          • Still trying to figure out how the syntax works. I read that &>filename redirects both stdout and stderr to filename. So couldn't the command be simplified (at least to me it is more simple) as ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" &>/dev/null && echo All there?

            – Elegance
            56 mins ago













          • It could, but not portably -- plain sh does not understand &>; it would misinterpret that as "run me in the background and send stdout to the redirection". I spelled it out from habit and kept it there because of the "portable code" preference.

            – Jeff Schaller
            44 mins ago






          • 1





            I got it finally. Thanks.

            – Elegance
            31 mins ago



















          2














          I would loop:



          result=True
          for dir in
          "$PWD/dir1"
          "$PWD/dir2"
          "$PWD/dir3"
          do
          if ! [ -d "$dir" ]; then
          result=False
          break
          fi
          done
          echo "$result"


          The break causes the loop to short-circuit, just like your chain of &&






          share|improve this answer































            1














            A loop might be more elegant:



            arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
            for d in "${arr[@]}"; do
            if [ -d "$d"]; then
            echo True
            else
            echo False
            fi
            done


            This is Bash. A more portable one is Sh. There you can use the positional array:



            set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"


            Then to loop over it use "$@".






            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',
              autoActivateHeartbeat: false,
              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
              });


              }
              });






              Elegance is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503830%2fchecking-for-the-existence-of-multiple-directories%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









              2














              If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":



              ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" >/dev/null 2>&1 && echo All there


              I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears -- it is not written to your terminal.






              share|improve this answer


























              • Can you help me understand this command? I know what file descriptors are. I know 1 is stdout, 2 is stderr and I know what redirecting is. I don't understand the significance of /dev/null, and I do not know how to parse the command.

                – Elegance
                1 hour ago











              • @Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…

                – Jeff Schaller
                1 hour ago











              • Still trying to figure out how the syntax works. I read that &>filename redirects both stdout and stderr to filename. So couldn't the command be simplified (at least to me it is more simple) as ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" &>/dev/null && echo All there?

                – Elegance
                56 mins ago













              • It could, but not portably -- plain sh does not understand &>; it would misinterpret that as "run me in the background and send stdout to the redirection". I spelled it out from habit and kept it there because of the "portable code" preference.

                – Jeff Schaller
                44 mins ago






              • 1





                I got it finally. Thanks.

                – Elegance
                31 mins ago
















              2














              If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":



              ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" >/dev/null 2>&1 && echo All there


              I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears -- it is not written to your terminal.






              share|improve this answer


























              • Can you help me understand this command? I know what file descriptors are. I know 1 is stdout, 2 is stderr and I know what redirecting is. I don't understand the significance of /dev/null, and I do not know how to parse the command.

                – Elegance
                1 hour ago











              • @Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…

                – Jeff Schaller
                1 hour ago











              • Still trying to figure out how the syntax works. I read that &>filename redirects both stdout and stderr to filename. So couldn't the command be simplified (at least to me it is more simple) as ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" &>/dev/null && echo All there?

                – Elegance
                56 mins ago













              • It could, but not portably -- plain sh does not understand &>; it would misinterpret that as "run me in the background and send stdout to the redirection". I spelled it out from habit and kept it there because of the "portable code" preference.

                – Jeff Schaller
                44 mins ago






              • 1





                I got it finally. Thanks.

                – Elegance
                31 mins ago














              2












              2








              2







              If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":



              ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" >/dev/null 2>&1 && echo All there


              I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears -- it is not written to your terminal.






              share|improve this answer















              If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":



              ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" >/dev/null 2>&1 && echo All there


              I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears -- it is not written to your terminal.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 1 hour ago

























              answered 2 hours ago









              Jeff SchallerJeff Schaller

              42.7k1159136




              42.7k1159136













              • Can you help me understand this command? I know what file descriptors are. I know 1 is stdout, 2 is stderr and I know what redirecting is. I don't understand the significance of /dev/null, and I do not know how to parse the command.

                – Elegance
                1 hour ago











              • @Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…

                – Jeff Schaller
                1 hour ago











              • Still trying to figure out how the syntax works. I read that &>filename redirects both stdout and stderr to filename. So couldn't the command be simplified (at least to me it is more simple) as ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" &>/dev/null && echo All there?

                – Elegance
                56 mins ago













              • It could, but not portably -- plain sh does not understand &>; it would misinterpret that as "run me in the background and send stdout to the redirection". I spelled it out from habit and kept it there because of the "portable code" preference.

                – Jeff Schaller
                44 mins ago






              • 1





                I got it finally. Thanks.

                – Elegance
                31 mins ago



















              • Can you help me understand this command? I know what file descriptors are. I know 1 is stdout, 2 is stderr and I know what redirecting is. I don't understand the significance of /dev/null, and I do not know how to parse the command.

                – Elegance
                1 hour ago











              • @Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…

                – Jeff Schaller
                1 hour ago











              • Still trying to figure out how the syntax works. I read that &>filename redirects both stdout and stderr to filename. So couldn't the command be simplified (at least to me it is more simple) as ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" &>/dev/null && echo All there?

                – Elegance
                56 mins ago













              • It could, but not portably -- plain sh does not understand &>; it would misinterpret that as "run me in the background and send stdout to the redirection". I spelled it out from habit and kept it there because of the "portable code" preference.

                – Jeff Schaller
                44 mins ago






              • 1





                I got it finally. Thanks.

                – Elegance
                31 mins ago

















              Can you help me understand this command? I know what file descriptors are. I know 1 is stdout, 2 is stderr and I know what redirecting is. I don't understand the significance of /dev/null, and I do not know how to parse the command.

              – Elegance
              1 hour ago





              Can you help me understand this command? I know what file descriptors are. I know 1 is stdout, 2 is stderr and I know what redirecting is. I don't understand the significance of /dev/null, and I do not know how to parse the command.

              – Elegance
              1 hour ago













              @Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…

              – Jeff Schaller
              1 hour ago





              @Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…

              – Jeff Schaller
              1 hour ago













              Still trying to figure out how the syntax works. I read that &>filename redirects both stdout and stderr to filename. So couldn't the command be simplified (at least to me it is more simple) as ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" &>/dev/null && echo All there?

              – Elegance
              56 mins ago







              Still trying to figure out how the syntax works. I read that &>filename redirects both stdout and stderr to filename. So couldn't the command be simplified (at least to me it is more simple) as ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir2" &>/dev/null && echo All there?

              – Elegance
              56 mins ago















              It could, but not portably -- plain sh does not understand &>; it would misinterpret that as "run me in the background and send stdout to the redirection". I spelled it out from habit and kept it there because of the "portable code" preference.

              – Jeff Schaller
              44 mins ago





              It could, but not portably -- plain sh does not understand &>; it would misinterpret that as "run me in the background and send stdout to the redirection". I spelled it out from habit and kept it there because of the "portable code" preference.

              – Jeff Schaller
              44 mins ago




              1




              1





              I got it finally. Thanks.

              – Elegance
              31 mins ago





              I got it finally. Thanks.

              – Elegance
              31 mins ago













              2














              I would loop:



              result=True
              for dir in
              "$PWD/dir1"
              "$PWD/dir2"
              "$PWD/dir3"
              do
              if ! [ -d "$dir" ]; then
              result=False
              break
              fi
              done
              echo "$result"


              The break causes the loop to short-circuit, just like your chain of &&






              share|improve this answer




























                2














                I would loop:



                result=True
                for dir in
                "$PWD/dir1"
                "$PWD/dir2"
                "$PWD/dir3"
                do
                if ! [ -d "$dir" ]; then
                result=False
                break
                fi
                done
                echo "$result"


                The break causes the loop to short-circuit, just like your chain of &&






                share|improve this answer


























                  2












                  2








                  2







                  I would loop:



                  result=True
                  for dir in
                  "$PWD/dir1"
                  "$PWD/dir2"
                  "$PWD/dir3"
                  do
                  if ! [ -d "$dir" ]; then
                  result=False
                  break
                  fi
                  done
                  echo "$result"


                  The break causes the loop to short-circuit, just like your chain of &&






                  share|improve this answer













                  I would loop:



                  result=True
                  for dir in
                  "$PWD/dir1"
                  "$PWD/dir2"
                  "$PWD/dir3"
                  do
                  if ! [ -d "$dir" ]; then
                  result=False
                  break
                  fi
                  done
                  echo "$result"


                  The break causes the loop to short-circuit, just like your chain of &&







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  glenn jackmanglenn jackman

                  52k572112




                  52k572112























                      1














                      A loop might be more elegant:



                      arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
                      for d in "${arr[@]}"; do
                      if [ -d "$d"]; then
                      echo True
                      else
                      echo False
                      fi
                      done


                      This is Bash. A more portable one is Sh. There you can use the positional array:



                      set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"


                      Then to loop over it use "$@".






                      share|improve this answer






























                        1














                        A loop might be more elegant:



                        arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
                        for d in "${arr[@]}"; do
                        if [ -d "$d"]; then
                        echo True
                        else
                        echo False
                        fi
                        done


                        This is Bash. A more portable one is Sh. There you can use the positional array:



                        set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"


                        Then to loop over it use "$@".






                        share|improve this answer




























                          1












                          1








                          1







                          A loop might be more elegant:



                          arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
                          for d in "${arr[@]}"; do
                          if [ -d "$d"]; then
                          echo True
                          else
                          echo False
                          fi
                          done


                          This is Bash. A more portable one is Sh. There you can use the positional array:



                          set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"


                          Then to loop over it use "$@".






                          share|improve this answer















                          A loop might be more elegant:



                          arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
                          for d in "${arr[@]}"; do
                          if [ -d "$d"]; then
                          echo True
                          else
                          echo False
                          fi
                          done


                          This is Bash. A more portable one is Sh. There you can use the positional array:



                          set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"


                          Then to loop over it use "$@".







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 2 hours ago

























                          answered 2 hours ago









                          TomaszTomasz

                          9,80152965




                          9,80152965






















                              Elegance is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              Elegance is a new contributor. Be nice, and check out our Code of Conduct.













                              Elegance is a new contributor. Be nice, and check out our Code of Conduct.












                              Elegance is a new contributor. Be nice, and check out our Code of Conduct.
















                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503830%2fchecking-for-the-existence-of-multiple-directories%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

                              Benedict Cumberbatch Contingut Inicis Debut professional Premis Filmografia bàsica Premis i...

                              Monticle de plataforma Contingut Est de Nord Amèrica Interpretacions Altres cultures Vegeu...

                              Escacs Janus Enllaços externs Menú de navegacióEscacs JanusJanusschachBrainKing.comChessV