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












2















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









New contributor




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
















  • 1





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

    – Doug O'Neal
    9 hours ago
















2















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









New contributor




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
















  • 1





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

    – Doug O'Neal
    9 hours ago














2












2








2








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









New contributor




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












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"






grep find






share|improve this question









New contributor




Kam Mok 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




Kam Mok 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 11 hours ago









John1024

46.7k4110124




46.7k4110124






New contributor




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









asked 11 hours ago









Kam MokKam Mok

111




111




New contributor




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





New contributor





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






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








  • 1





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

    – Doug O'Neal
    9 hours ago














  • 1





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

    – Doug O'Neal
    9 hours ago








1




1





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

– Doug O'Neal
9 hours ago





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

– Doug O'Neal
9 hours ago










2 Answers
2






active

oldest

votes


















8














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


























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

    – Bodo
    11 hours ago



















7














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





share|improve this answer



















  • 5





    you may want to consider find . -name '*.py' -exec grep -l 'def test' {} + to reduce the number of grep invocations

    – iruvar
    10 hours ago











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


}
});






Kam Mok 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%2f500243%2fhow-to-list-all-python-test-scripts-that-contain-def-test%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









8














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


























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

    – Bodo
    11 hours ago
















8














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


























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

    – Bodo
    11 hours ago














8












8








8







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 7 hours ago

























answered 11 hours ago









John1024John1024

46.7k4110124




46.7k4110124













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

    – Bodo
    11 hours ago



















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

    – Bodo
    11 hours ago

















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

– Bodo
11 hours ago





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

– Bodo
11 hours ago













7














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





share|improve this answer



















  • 5





    you may want to consider find . -name '*.py' -exec grep -l 'def test' {} + to reduce the number of grep invocations

    – iruvar
    10 hours ago
















7














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





share|improve this answer



















  • 5





    you may want to consider find . -name '*.py' -exec grep -l 'def test' {} + to reduce the number of grep invocations

    – iruvar
    10 hours ago














7












7








7







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered 11 hours ago









BodoBodo

1,03219




1,03219








  • 5





    you may want to consider find . -name '*.py' -exec grep -l 'def test' {} + to reduce the number of grep invocations

    – iruvar
    10 hours ago














  • 5





    you may want to consider find . -name '*.py' -exec grep -l 'def test' {} + to reduce the number of grep invocations

    – iruvar
    10 hours ago








5




5





you may want to consider find . -name '*.py' -exec grep -l 'def test' {} + to reduce the number of grep invocations

– iruvar
10 hours ago





you may want to consider find . -name '*.py' -exec grep -l 'def test' {} + to reduce the number of grep invocations

– iruvar
10 hours ago










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










draft saved

draft discarded


















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













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












Kam Mok 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%2f500243%2fhow-to-list-all-python-test-scripts-that-contain-def-test%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy