How to find in different kinds of files

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








up vote
0
down vote

favorite












I'm working on a Ubuntu Windows app on my Windows-10 PC, the result of uname -a is the following:



Linux DOMINIQUEDS 4.4.0-17134-Microsoft #48-Microsoft Fri Apr 27 18:06:00 PST 2018 x86_64 x86_64 x86_64 GNU/Linux


I'm doing some C++ development, and I'd like to know which source files (*.cpp or *.h) are including the file Sample.h, so I launched following command:



find ./ -name "*.cpp" -or -name "*.h" -exec grep -i "include" /dev/null ; | grep "Sample.h"


This seems not to be working: only the *.h files, containing include and Sample.h on the same line, are given.



However I know for sure that the -o construction for finding different kinds of files is correct:



find ./ -name "*.cpp" -or -name "*.h"


=> here I get a list of *.cpp and *.h files.



This leaves me with two possibilities:



  • Either the behaviour is correct: the -exec parameter is only used on the last find result. In that case, can somebody tell me how I can perform the -exec on all find results?

  • Either the behaviour is wrong. In that case, does somebody know if this is a general Ubuntu problem/Windows-10 Ubuntu app problem/... and if any solution can be expected and when?

Thanks in advance







share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm working on a Ubuntu Windows app on my Windows-10 PC, the result of uname -a is the following:



    Linux DOMINIQUEDS 4.4.0-17134-Microsoft #48-Microsoft Fri Apr 27 18:06:00 PST 2018 x86_64 x86_64 x86_64 GNU/Linux


    I'm doing some C++ development, and I'd like to know which source files (*.cpp or *.h) are including the file Sample.h, so I launched following command:



    find ./ -name "*.cpp" -or -name "*.h" -exec grep -i "include" /dev/null ; | grep "Sample.h"


    This seems not to be working: only the *.h files, containing include and Sample.h on the same line, are given.



    However I know for sure that the -o construction for finding different kinds of files is correct:



    find ./ -name "*.cpp" -or -name "*.h"


    => here I get a list of *.cpp and *.h files.



    This leaves me with two possibilities:



    • Either the behaviour is correct: the -exec parameter is only used on the last find result. In that case, can somebody tell me how I can perform the -exec on all find results?

    • Either the behaviour is wrong. In that case, does somebody know if this is a general Ubuntu problem/Windows-10 Ubuntu app problem/... and if any solution can be expected and when?

    Thanks in advance







    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm working on a Ubuntu Windows app on my Windows-10 PC, the result of uname -a is the following:



      Linux DOMINIQUEDS 4.4.0-17134-Microsoft #48-Microsoft Fri Apr 27 18:06:00 PST 2018 x86_64 x86_64 x86_64 GNU/Linux


      I'm doing some C++ development, and I'd like to know which source files (*.cpp or *.h) are including the file Sample.h, so I launched following command:



      find ./ -name "*.cpp" -or -name "*.h" -exec grep -i "include" /dev/null ; | grep "Sample.h"


      This seems not to be working: only the *.h files, containing include and Sample.h on the same line, are given.



      However I know for sure that the -o construction for finding different kinds of files is correct:



      find ./ -name "*.cpp" -or -name "*.h"


      => here I get a list of *.cpp and *.h files.



      This leaves me with two possibilities:



      • Either the behaviour is correct: the -exec parameter is only used on the last find result. In that case, can somebody tell me how I can perform the -exec on all find results?

      • Either the behaviour is wrong. In that case, does somebody know if this is a general Ubuntu problem/Windows-10 Ubuntu app problem/... and if any solution can be expected and when?

      Thanks in advance







      share|improve this question














      I'm working on a Ubuntu Windows app on my Windows-10 PC, the result of uname -a is the following:



      Linux DOMINIQUEDS 4.4.0-17134-Microsoft #48-Microsoft Fri Apr 27 18:06:00 PST 2018 x86_64 x86_64 x86_64 GNU/Linux


      I'm doing some C++ development, and I'd like to know which source files (*.cpp or *.h) are including the file Sample.h, so I launched following command:



      find ./ -name "*.cpp" -or -name "*.h" -exec grep -i "include" /dev/null ; | grep "Sample.h"


      This seems not to be working: only the *.h files, containing include and Sample.h on the same line, are given.



      However I know for sure that the -o construction for finding different kinds of files is correct:



      find ./ -name "*.cpp" -or -name "*.h"


      => here I get a list of *.cpp and *.h files.



      This leaves me with two possibilities:



      • Either the behaviour is correct: the -exec parameter is only used on the last find result. In that case, can somebody tell me how I can perform the -exec on all find results?

      • Either the behaviour is wrong. In that case, does somebody know if this is a general Ubuntu problem/Windows-10 Ubuntu app problem/... and if any solution can be expected and when?

      Thanks in advance









      share|improve this question













      share|improve this question




      share|improve this question








      edited May 15 at 9:12









      muru

      129k19271461




      129k19271461










      asked May 15 at 9:08









      Dominique

      1106




      1106




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted











          However I know for sure that the -o construction for finding different
          kinds of files is correct:



          find ./ -name "*.cpp" -or -name "*.h"



          True, but the precedence of -or is not that high. From man find:



           Please note that -a when specified implicitly (for example by two tests
          appearing without an explicit operator between them) or explicitly has
          higher precedence than -o. This means that find . -name afile -o -name
          bfile -print will never print afile.


          So:



          -name "*.cpp" -or -name "*.h" -exec grep ...


          Is like:



          -name "*.cpp" -or ( -name "*.h" -exec grep ... )


          And not like:



          ( -name "*.cpp" -or -name "*.h" ) -exec grep ...


          You need:



          find . ( -name '*.cpp' -o -name '*.h' ) -exec grep -H '#include.*Sample.h' +


          (I assume you used /dev/null to make grep print the filename? The -H option does that.)






          share|improve this answer




















          • indeed, the /dev/null is an old trick for showing the filename.
            – Dominique
            May 15 at 9:19

















          up vote
          2
          down vote













          With modern grep, you don't need find at all e.g.



          grep -r --include='*.cpp' --include='*.h' 'include' . | grep 'Sample.h'


          or - better (given that the order of the search terms is unambiguous)



          grep -r --include='*.cpp' --include='*.h' 'include.*Sample.h' .





          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',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            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%2f1036433%2fhow-to-find-in-different-kinds-of-files%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted











            However I know for sure that the -o construction for finding different
            kinds of files is correct:



            find ./ -name "*.cpp" -or -name "*.h"



            True, but the precedence of -or is not that high. From man find:



             Please note that -a when specified implicitly (for example by two tests
            appearing without an explicit operator between them) or explicitly has
            higher precedence than -o. This means that find . -name afile -o -name
            bfile -print will never print afile.


            So:



            -name "*.cpp" -or -name "*.h" -exec grep ...


            Is like:



            -name "*.cpp" -or ( -name "*.h" -exec grep ... )


            And not like:



            ( -name "*.cpp" -or -name "*.h" ) -exec grep ...


            You need:



            find . ( -name '*.cpp' -o -name '*.h' ) -exec grep -H '#include.*Sample.h' +


            (I assume you used /dev/null to make grep print the filename? The -H option does that.)






            share|improve this answer




















            • indeed, the /dev/null is an old trick for showing the filename.
              – Dominique
              May 15 at 9:19














            up vote
            2
            down vote



            accepted











            However I know for sure that the -o construction for finding different
            kinds of files is correct:



            find ./ -name "*.cpp" -or -name "*.h"



            True, but the precedence of -or is not that high. From man find:



             Please note that -a when specified implicitly (for example by two tests
            appearing without an explicit operator between them) or explicitly has
            higher precedence than -o. This means that find . -name afile -o -name
            bfile -print will never print afile.


            So:



            -name "*.cpp" -or -name "*.h" -exec grep ...


            Is like:



            -name "*.cpp" -or ( -name "*.h" -exec grep ... )


            And not like:



            ( -name "*.cpp" -or -name "*.h" ) -exec grep ...


            You need:



            find . ( -name '*.cpp' -o -name '*.h' ) -exec grep -H '#include.*Sample.h' +


            (I assume you used /dev/null to make grep print the filename? The -H option does that.)






            share|improve this answer




















            • indeed, the /dev/null is an old trick for showing the filename.
              – Dominique
              May 15 at 9:19












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted







            However I know for sure that the -o construction for finding different
            kinds of files is correct:



            find ./ -name "*.cpp" -or -name "*.h"



            True, but the precedence of -or is not that high. From man find:



             Please note that -a when specified implicitly (for example by two tests
            appearing without an explicit operator between them) or explicitly has
            higher precedence than -o. This means that find . -name afile -o -name
            bfile -print will never print afile.


            So:



            -name "*.cpp" -or -name "*.h" -exec grep ...


            Is like:



            -name "*.cpp" -or ( -name "*.h" -exec grep ... )


            And not like:



            ( -name "*.cpp" -or -name "*.h" ) -exec grep ...


            You need:



            find . ( -name '*.cpp' -o -name '*.h' ) -exec grep -H '#include.*Sample.h' +


            (I assume you used /dev/null to make grep print the filename? The -H option does that.)






            share|improve this answer













            However I know for sure that the -o construction for finding different
            kinds of files is correct:



            find ./ -name "*.cpp" -or -name "*.h"



            True, but the precedence of -or is not that high. From man find:



             Please note that -a when specified implicitly (for example by two tests
            appearing without an explicit operator between them) or explicitly has
            higher precedence than -o. This means that find . -name afile -o -name
            bfile -print will never print afile.


            So:



            -name "*.cpp" -or -name "*.h" -exec grep ...


            Is like:



            -name "*.cpp" -or ( -name "*.h" -exec grep ... )


            And not like:



            ( -name "*.cpp" -or -name "*.h" ) -exec grep ...


            You need:



            find . ( -name '*.cpp' -o -name '*.h' ) -exec grep -H '#include.*Sample.h' +


            (I assume you used /dev/null to make grep print the filename? The -H option does that.)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 15 at 9:17









            muru

            129k19271461




            129k19271461











            • indeed, the /dev/null is an old trick for showing the filename.
              – Dominique
              May 15 at 9:19
















            • indeed, the /dev/null is an old trick for showing the filename.
              – Dominique
              May 15 at 9:19















            indeed, the /dev/null is an old trick for showing the filename.
            – Dominique
            May 15 at 9:19




            indeed, the /dev/null is an old trick for showing the filename.
            – Dominique
            May 15 at 9:19












            up vote
            2
            down vote













            With modern grep, you don't need find at all e.g.



            grep -r --include='*.cpp' --include='*.h' 'include' . | grep 'Sample.h'


            or - better (given that the order of the search terms is unambiguous)



            grep -r --include='*.cpp' --include='*.h' 'include.*Sample.h' .





            share|improve this answer
























              up vote
              2
              down vote













              With modern grep, you don't need find at all e.g.



              grep -r --include='*.cpp' --include='*.h' 'include' . | grep 'Sample.h'


              or - better (given that the order of the search terms is unambiguous)



              grep -r --include='*.cpp' --include='*.h' 'include.*Sample.h' .





              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                With modern grep, you don't need find at all e.g.



                grep -r --include='*.cpp' --include='*.h' 'include' . | grep 'Sample.h'


                or - better (given that the order of the search terms is unambiguous)



                grep -r --include='*.cpp' --include='*.h' 'include.*Sample.h' .





                share|improve this answer












                With modern grep, you don't need find at all e.g.



                grep -r --include='*.cpp' --include='*.h' 'include' . | grep 'Sample.h'


                or - better (given that the order of the search terms is unambiguous)



                grep -r --include='*.cpp' --include='*.h' 'include.*Sample.h' .






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 15 at 10:36









                steeldriver

                62.2k1196164




                62.2k1196164






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1036433%2fhow-to-find-in-different-kinds-of-files%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

                    Trouble downloading packages list due to a “Hash sum mismatch” error

                    How do I move numbers in filenames, in a batch renaming operation?