how to list all python test scripts that contain “def test” [duplicate]












6
















This question already has an answer here:




  • find and echo file names only with pattern found

    6 answers




Want to list all python test scripts that contain "def test"



This command line did not work while individual command works



find . -name "*.py" | grep "def test"









share|improve this question















marked as duplicate by don_crissti, jimmij, Sparhawk, Christopher, Kusalananda Feb 14 at 20:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Perhaps find -name *.py -print0 | xargs -0 grep 'def test?

    – Doug O'Neal
    Feb 12 at 21:03






  • 1





    All the answers so far will find hits when def test is used inside a string or comment. Is this really what you want? Avoiding it is possible but substantially more complex (it essentially requires parsing the Python files, for which Python has a dedicated module).

    – Konrad Rudolph
    Feb 13 at 12:33








  • 4





    Possible duplicate of find and echo file names only with pattern found, How to list .txt files with specific content? and about a dozen others...

    – don_crissti
    Feb 13 at 18:12


















6
















This question already has an answer here:




  • find and echo file names only with pattern found

    6 answers




Want to list all python test scripts that contain "def test"



This command line did not work while individual command works



find . -name "*.py" | grep "def test"









share|improve this question















marked as duplicate by don_crissti, jimmij, Sparhawk, Christopher, Kusalananda Feb 14 at 20:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Perhaps find -name *.py -print0 | xargs -0 grep 'def test?

    – Doug O'Neal
    Feb 12 at 21:03






  • 1





    All the answers so far will find hits when def test is used inside a string or comment. Is this really what you want? Avoiding it is possible but substantially more complex (it essentially requires parsing the Python files, for which Python has a dedicated module).

    – Konrad Rudolph
    Feb 13 at 12:33








  • 4





    Possible duplicate of find and echo file names only with pattern found, How to list .txt files with specific content? and about a dozen others...

    – don_crissti
    Feb 13 at 18:12
















6












6








6









This question already has an answer here:




  • find and echo file names only with pattern found

    6 answers




Want to list all python test scripts that contain "def test"



This command line did not work while individual command works



find . -name "*.py" | grep "def test"









share|improve this question

















This question already has an answer here:




  • find and echo file names only with pattern found

    6 answers




Want to list all python test scripts that contain "def test"



This command line did not work while individual command works



find . -name "*.py" | grep "def test"




This question already has an answer here:




  • find and echo file names only with pattern found

    6 answers








grep find






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 12 at 19:09









John1024

47.6k5110126




47.6k5110126










asked Feb 12 at 19:01









Kam MokKam Mok

311




311




marked as duplicate by don_crissti, jimmij, Sparhawk, Christopher, Kusalananda Feb 14 at 20:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by don_crissti, jimmij, Sparhawk, Christopher, Kusalananda Feb 14 at 20:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Perhaps find -name *.py -print0 | xargs -0 grep 'def test?

    – Doug O'Neal
    Feb 12 at 21:03






  • 1





    All the answers so far will find hits when def test is used inside a string or comment. Is this really what you want? Avoiding it is possible but substantially more complex (it essentially requires parsing the Python files, for which Python has a dedicated module).

    – Konrad Rudolph
    Feb 13 at 12:33








  • 4





    Possible duplicate of find and echo file names only with pattern found, How to list .txt files with specific content? and about a dozen others...

    – don_crissti
    Feb 13 at 18:12
















  • 1





    Perhaps find -name *.py -print0 | xargs -0 grep 'def test?

    – Doug O'Neal
    Feb 12 at 21:03






  • 1





    All the answers so far will find hits when def test is used inside a string or comment. Is this really what you want? Avoiding it is possible but substantially more complex (it essentially requires parsing the Python files, for which Python has a dedicated module).

    – Konrad Rudolph
    Feb 13 at 12:33








  • 4





    Possible duplicate of find and echo file names only with pattern found, How to list .txt files with specific content? and about a dozen others...

    – don_crissti
    Feb 13 at 18:12










1




1





Perhaps find -name *.py -print0 | xargs -0 grep 'def test?

– Doug O'Neal
Feb 12 at 21:03





Perhaps find -name *.py -print0 | xargs -0 grep 'def test?

– Doug O'Neal
Feb 12 at 21:03




1




1





All the answers so far will find hits when def test is used inside a string or comment. Is this really what you want? Avoiding it is possible but substantially more complex (it essentially requires parsing the Python files, for which Python has a dedicated module).

– Konrad Rudolph
Feb 13 at 12:33







All the answers so far will find hits when def test is used inside a string or comment. Is this really what you want? Avoiding it is possible but substantially more complex (it essentially requires parsing the Python files, for which Python has a dedicated module).

– Konrad Rudolph
Feb 13 at 12:33






4




4





Possible duplicate of find and echo file names only with pattern found, How to list .txt files with specific content? and about a dozen others...

– don_crissti
Feb 13 at 18:12







Possible duplicate of find and echo file names only with pattern found, How to list .txt files with specific content? and about a dozen others...

– don_crissti
Feb 13 at 18:12












4 Answers
4






active

oldest

votes


















15














Try:



grep -r --include '*.py' 'def test'



  • -r tells grep to search for files recursively


  • --include '*.py' tells grep to only examine files whose names end in .py.



The --include option is supported by both GNU (Linux) grep and MacOS grep



Discussion of pipeline approach



In the following command, find passes the names of the files found to standard input of grep:



find . -name "*.py" | grep "def test"


The issue here is that grep treats its standard input as the text to search. Consequently, the only output will be those files whose name (as opposed to contents) contains def test.



For example, let's create an empty file:



$ touch 'def test.py'


And run the pipeline command:



$ find . -name "*.py" | grep "def test"
./def test.py


The command finds this file because of its name. Its contents are never examined.






share|improve this answer





















  • 1





    ... if your grep supports --include like GNU grep does.

    – Bodo
    Feb 12 at 19:12



















11














find . -name '*.py' -exec grep -l 'def test' {} ;


or



find . -name '*.py' -exec grep -l 'def test' {} +


The second version will result in fewer invocations of grep by specifying sets of files as arguments.






share|improve this answer

































    1














    grep -ril 'def test' .


    The above command would list what you are looking.



    In the command
    - the options ril refer to recursive(r) case insensitive(i) search and list(l) only file names






    share|improve this answer































      0














      The standard way to execute a command on files found by find is using xargs:



      find . -name "*.py" | xargs grep "def test"


      In case of grep, you can use recursive grep instead of find + grep as other answers have explained, but xargs is good to know because it's a general approach that can be used for other use cases too.



      (And as Doug O'Neal commented: if there are filenames with spaces, you have to tell find and xargs to use null characters as terminators: find . -name "*.py" -print0 | xargs -0 grep "def test")






      share|improve this answer






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        15














        Try:



        grep -r --include '*.py' 'def test'



        • -r tells grep to search for files recursively


        • --include '*.py' tells grep to only examine files whose names end in .py.



        The --include option is supported by both GNU (Linux) grep and MacOS grep



        Discussion of pipeline approach



        In the following command, find passes the names of the files found to standard input of grep:



        find . -name "*.py" | grep "def test"


        The issue here is that grep treats its standard input as the text to search. Consequently, the only output will be those files whose name (as opposed to contents) contains def test.



        For example, let's create an empty file:



        $ touch 'def test.py'


        And run the pipeline command:



        $ find . -name "*.py" | grep "def test"
        ./def test.py


        The command finds this file because of its name. Its contents are never examined.






        share|improve this answer





















        • 1





          ... if your grep supports --include like GNU grep does.

          – Bodo
          Feb 12 at 19:12
















        15














        Try:



        grep -r --include '*.py' 'def test'



        • -r tells grep to search for files recursively


        • --include '*.py' tells grep to only examine files whose names end in .py.



        The --include option is supported by both GNU (Linux) grep and MacOS grep



        Discussion of pipeline approach



        In the following command, find passes the names of the files found to standard input of grep:



        find . -name "*.py" | grep "def test"


        The issue here is that grep treats its standard input as the text to search. Consequently, the only output will be those files whose name (as opposed to contents) contains def test.



        For example, let's create an empty file:



        $ touch 'def test.py'


        And run the pipeline command:



        $ find . -name "*.py" | grep "def test"
        ./def test.py


        The command finds this file because of its name. Its contents are never examined.






        share|improve this answer





















        • 1





          ... if your grep supports --include like GNU grep does.

          – Bodo
          Feb 12 at 19:12














        15












        15








        15







        Try:



        grep -r --include '*.py' 'def test'



        • -r tells grep to search for files recursively


        • --include '*.py' tells grep to only examine files whose names end in .py.



        The --include option is supported by both GNU (Linux) grep and MacOS grep



        Discussion of pipeline approach



        In the following command, find passes the names of the files found to standard input of grep:



        find . -name "*.py" | grep "def test"


        The issue here is that grep treats its standard input as the text to search. Consequently, the only output will be those files whose name (as opposed to contents) contains def test.



        For example, let's create an empty file:



        $ touch 'def test.py'


        And run the pipeline command:



        $ find . -name "*.py" | grep "def test"
        ./def test.py


        The command finds this file because of its name. Its contents are never examined.






        share|improve this answer















        Try:



        grep -r --include '*.py' 'def test'



        • -r tells grep to search for files recursively


        • --include '*.py' tells grep to only examine files whose names end in .py.



        The --include option is supported by both GNU (Linux) grep and MacOS grep



        Discussion of pipeline approach



        In the following command, find passes the names of the files found to standard input of grep:



        find . -name "*.py" | grep "def test"


        The issue here is that grep treats its standard input as the text to search. Consequently, the only output will be those files whose name (as opposed to contents) contains def test.



        For example, let's create an empty file:



        $ touch 'def test.py'


        And run the pipeline command:



        $ find . -name "*.py" | grep "def test"
        ./def test.py


        The command finds this file because of its name. Its contents are never examined.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 12 at 22:30

























        answered Feb 12 at 19:07









        John1024John1024

        47.6k5110126




        47.6k5110126








        • 1





          ... if your grep supports --include like GNU grep does.

          – Bodo
          Feb 12 at 19:12














        • 1





          ... if your grep supports --include like GNU grep does.

          – Bodo
          Feb 12 at 19:12








        1




        1





        ... if your grep supports --include like GNU grep does.

        – Bodo
        Feb 12 at 19:12





        ... if your grep supports --include like GNU grep does.

        – Bodo
        Feb 12 at 19:12













        11














        find . -name '*.py' -exec grep -l 'def test' {} ;


        or



        find . -name '*.py' -exec grep -l 'def test' {} +


        The second version will result in fewer invocations of grep by specifying sets of files as arguments.






        share|improve this answer






























          11














          find . -name '*.py' -exec grep -l 'def test' {} ;


          or



          find . -name '*.py' -exec grep -l 'def test' {} +


          The second version will result in fewer invocations of grep by specifying sets of files as arguments.






          share|improve this answer




























            11












            11








            11







            find . -name '*.py' -exec grep -l 'def test' {} ;


            or



            find . -name '*.py' -exec grep -l 'def test' {} +


            The second version will result in fewer invocations of grep by specifying sets of files as arguments.






            share|improve this answer















            find . -name '*.py' -exec grep -l 'def test' {} ;


            or



            find . -name '*.py' -exec grep -l 'def test' {} +


            The second version will result in fewer invocations of grep by specifying sets of files as arguments.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 13 at 9:26

























            answered Feb 12 at 19:04









            BodoBodo

            2,231518




            2,231518























                1














                grep -ril 'def test' .


                The above command would list what you are looking.



                In the command
                - the options ril refer to recursive(r) case insensitive(i) search and list(l) only file names






                share|improve this answer




























                  1














                  grep -ril 'def test' .


                  The above command would list what you are looking.



                  In the command
                  - the options ril refer to recursive(r) case insensitive(i) search and list(l) only file names






                  share|improve this answer


























                    1












                    1








                    1







                    grep -ril 'def test' .


                    The above command would list what you are looking.



                    In the command
                    - the options ril refer to recursive(r) case insensitive(i) search and list(l) only file names






                    share|improve this answer













                    grep -ril 'def test' .


                    The above command would list what you are looking.



                    In the command
                    - the options ril refer to recursive(r) case insensitive(i) search and list(l) only file names







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 13 at 8:15









                    Adhithyan VijayakumarAdhithyan Vijayakumar

                    111




                    111























                        0














                        The standard way to execute a command on files found by find is using xargs:



                        find . -name "*.py" | xargs grep "def test"


                        In case of grep, you can use recursive grep instead of find + grep as other answers have explained, but xargs is good to know because it's a general approach that can be used for other use cases too.



                        (And as Doug O'Neal commented: if there are filenames with spaces, you have to tell find and xargs to use null characters as terminators: find . -name "*.py" -print0 | xargs -0 grep "def test")






                        share|improve this answer




























                          0














                          The standard way to execute a command on files found by find is using xargs:



                          find . -name "*.py" | xargs grep "def test"


                          In case of grep, you can use recursive grep instead of find + grep as other answers have explained, but xargs is good to know because it's a general approach that can be used for other use cases too.



                          (And as Doug O'Neal commented: if there are filenames with spaces, you have to tell find and xargs to use null characters as terminators: find . -name "*.py" -print0 | xargs -0 grep "def test")






                          share|improve this answer


























                            0












                            0








                            0







                            The standard way to execute a command on files found by find is using xargs:



                            find . -name "*.py" | xargs grep "def test"


                            In case of grep, you can use recursive grep instead of find + grep as other answers have explained, but xargs is good to know because it's a general approach that can be used for other use cases too.



                            (And as Doug O'Neal commented: if there are filenames with spaces, you have to tell find and xargs to use null characters as terminators: find . -name "*.py" -print0 | xargs -0 grep "def test")






                            share|improve this answer













                            The standard way to execute a command on files found by find is using xargs:



                            find . -name "*.py" | xargs grep "def test"


                            In case of grep, you can use recursive grep instead of find + grep as other answers have explained, but xargs is good to know because it's a general approach that can be used for other use cases too.



                            (And as Doug O'Neal commented: if there are filenames with spaces, you have to tell find and xargs to use null characters as terminators: find . -name "*.py" -print0 | xargs -0 grep "def test")







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 13 at 10:39









                            Roel SchroevenRoel Schroeven

                            14528




                            14528















                                Popular posts from this blog

                                Probability when a professor distributes a quiz and homework assignment to a class of n students.

                                Aardman Animations

                                Are they similar matrix