Conditional statements: finding folders that don't contain a particular file












1














I needed to find all folders, whose names have a digit in them, that don't contain a file ending with .counts.txt. By combining several suggestions I found online, I was able to come up with the following command (for bash), which does what I want.



find . maxdepth 1 -type d -name "*[0-9]*" ! -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ; -print


However, I don't understand some of its features:




  1. What does the exclamation mark do here? Does it somehow negate the condition statement that follows?

  2. Why is it necessary to open a new shell using sh rather than directly piping to ls?










share|improve this question





























    1














    I needed to find all folders, whose names have a digit in them, that don't contain a file ending with .counts.txt. By combining several suggestions I found online, I was able to come up with the following command (for bash), which does what I want.



    find . maxdepth 1 -type d -name "*[0-9]*" ! -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ; -print


    However, I don't understand some of its features:




    1. What does the exclamation mark do here? Does it somehow negate the condition statement that follows?

    2. Why is it necessary to open a new shell using sh rather than directly piping to ls?










    share|improve this question



























      1












      1








      1


      1





      I needed to find all folders, whose names have a digit in them, that don't contain a file ending with .counts.txt. By combining several suggestions I found online, I was able to come up with the following command (for bash), which does what I want.



      find . maxdepth 1 -type d -name "*[0-9]*" ! -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ; -print


      However, I don't understand some of its features:




      1. What does the exclamation mark do here? Does it somehow negate the condition statement that follows?

      2. Why is it necessary to open a new shell using sh rather than directly piping to ls?










      share|improve this question















      I needed to find all folders, whose names have a digit in them, that don't contain a file ending with .counts.txt. By combining several suggestions I found online, I was able to come up with the following command (for bash), which does what I want.



      find . maxdepth 1 -type d -name "*[0-9]*" ! -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ; -print


      However, I don't understand some of its features:




      1. What does the exclamation mark do here? Does it somehow negate the condition statement that follows?

      2. Why is it necessary to open a new shell using sh rather than directly piping to ls?







      files find directory ls






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 16 at 4:24









      Rui F Ribeiro

      38.9k1479129




      38.9k1479129










      asked Jul 15 '17 at 10:19









      GingerBadger

      1083




      1083






















          1 Answer
          1






          active

          oldest

          votes


















          1















          1. Yes the ! negates the next condition, that is the -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;. As this is an -exec condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0).

          2. The arguments after -exec are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.


          See man find for the docs on the ! and -exec args for find.






          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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f378624%2fconditional-statements-finding-folders-that-dont-contain-a-particular-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1















            1. Yes the ! negates the next condition, that is the -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;. As this is an -exec condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0).

            2. The arguments after -exec are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.


            See man find for the docs on the ! and -exec args for find.






            share|improve this answer


























              1















              1. Yes the ! negates the next condition, that is the -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;. As this is an -exec condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0).

              2. The arguments after -exec are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.


              See man find for the docs on the ! and -exec args for find.






              share|improve this answer
























                1












                1








                1







                1. Yes the ! negates the next condition, that is the -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;. As this is an -exec condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0).

                2. The arguments after -exec are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.


                See man find for the docs on the ! and -exec args for find.






                share|improve this answer













                1. Yes the ! negates the next condition, that is the -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;. As this is an -exec condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0).

                2. The arguments after -exec are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.


                See man find for the docs on the ! and -exec args for find.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 15 '17 at 11:23









                Lucas

                1,988717




                1,988717






























                    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%2f378624%2fconditional-statements-finding-folders-that-dont-contain-a-particular-file%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

                    Morgemoulin

                    Scott Moir

                    Souastre