how to list all python test scripts that contain “def test” [duplicate]
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"
grep find
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.
add a comment |
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"
grep find
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
Perhapsfind -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 whendef 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
add a comment |
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"
grep find
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
grep find
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
Perhapsfind -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 whendef 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
add a comment |
1
Perhapsfind -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 whendef 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
add a comment |
4 Answers
4
active
oldest
votes
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.
1
... if yourgrep
supports--include
like GNU grep does.
– Bodo
Feb 12 at 19:12
add a comment |
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.
add a comment |
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
add a comment |
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"
)
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
1
... if yourgrep
supports--include
like GNU grep does.
– Bodo
Feb 12 at 19:12
add a comment |
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.
1
... if yourgrep
supports--include
like GNU grep does.
– Bodo
Feb 12 at 19:12
add a comment |
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.
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.
edited Feb 12 at 22:30
answered Feb 12 at 19:07
John1024John1024
47.6k5110126
47.6k5110126
1
... if yourgrep
supports--include
like GNU grep does.
– Bodo
Feb 12 at 19:12
add a comment |
1
... if yourgrep
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Feb 13 at 9:26
answered Feb 12 at 19:04
BodoBodo
2,231518
2,231518
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Feb 13 at 8:15
Adhithyan VijayakumarAdhithyan Vijayakumar
111
111
add a comment |
add a comment |
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"
)
add a comment |
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"
)
add a comment |
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"
)
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"
)
answered Feb 13 at 10:39
Roel SchroevenRoel Schroeven
14528
14528
add a comment |
add a comment |
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