How can I find out how many files ending in .c or .cpp inside a directory contain the string: string?
How can I find out how many files ending in .c or .cpp inside a directory contain the string: string?
The string can occur anywhere in the entire file, including comments or as part of a larger string.
16.04 command-line files
add a comment |
How can I find out how many files ending in .c or .cpp inside a directory contain the string: string?
The string can occur anywhere in the entire file, including comments or as part of a larger string.
16.04 command-line files
add a comment |
How can I find out how many files ending in .c or .cpp inside a directory contain the string: string?
The string can occur anywhere in the entire file, including comments or as part of a larger string.
16.04 command-line files
How can I find out how many files ending in .c or .cpp inside a directory contain the string: string?
The string can occur anywhere in the entire file, including comments or as part of a larger string.
16.04 command-line files
16.04 command-line files
edited Jan 12 at 18:40
Zanna
50.7k13136241
50.7k13136241
asked Jan 11 at 20:58
DwayneTheRockJohnsonOfficialDwayneTheRockJohnsonOfficial
6
6
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
If you know that your filenames cannot contain newlines, then
grep -rFl --include='*.c' --include='*.cpp' string . | wc -l
Otherwise
find . -type f ( -name '*.c' -o -name '*.cpp' ) -exec grep -Fq string {} ; -printf x | wc -c
2
Completely forgotgrep
has--include
flag. +1 for that
– Sergiy Kolodyazhnyy
Jan 11 at 21:11
add a comment |
grep -l string *.c *.cpp | wc -l
- If any filenames contain newlines, the count will be too high.
- If the globs fail to match, you will get an error like
grep: *.c: No such file or directory
, but the count will still be correct.
This is like a quick and dirty version of steeldriver's answer.
1
Consider alsoshopt -s dotglob
or adding.*.c .*.cpp
to account for filenames with leading dot. Also consider2>/dev/null
if you don't wanna see the error message.
– Sergiy Kolodyazhnyy
Jan 11 at 21:24
1
@Serg All good points.grep -s
will also hide error messages, like2>/dev/null
.
– wjandrea
Jan 11 at 21:26
add a comment |
Use the grep command to find out:
find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp" ) -exec grep "string" {} ; | wc -l
add a comment |
Simple way would be via bash
recursive glob combined with single match parameter in grep
(one file - one match) :
shopt -s globstar
grep -m 1 'string' */**.{c,cpp} | wc -l
Why not grep -R
? Because -R
walks through all files, and doesn't filter .c
or .cpp
files, so we use bash's globbing to do that job.
Otherwise, for shell-agnostic way you can use find
:
find -type f ( -iname "*.c" -or -iname "*.cpp" ) -exec grep -q 'string' {} ; -and -print | wc -l
add a comment |
I would recommend using the tool ripgrep (snap) for these kinds of task (grepping in source code repositories):
$ rg -g '*.c' -g '*.cpp' -l string | wc -l
The following command may also be useful:
$ rg -t c -t cpp -l string | wc -l
Which searches files with the following extensions:
$ rg --type-list | grep -E '^c:|^cpp:'
c: *.H, *.c, *.h
cpp: *.C, *.H, *.cc, *.cpp, *.cxx, *.h, *.hh, *.hpp, *.hxx, *.inl
The flags used are:
-l, --files-with-matches
Only print the paths with at least one match.
This overrides --files-without-match.
-g, --glob <GLOB>...
Include or exclude files and directories for searching that match the given
glob. This always overrides any other ignore logic. Multiple glob flags may be
used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude
it.
-t, --type <TYPE>...
Only search files matching TYPE. Multiple type flags may be provided. Use the
--type-list flag to list all available types.
add a comment |
You should be able to find out how many files end in .c
or .cpp
that contains the word "string" by executing:
cat *.c* | grep -i "string" | wc -l
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1108979%2fhow-can-i-find-out-how-many-files-ending-in-c-or-cpp-inside-a-directory-contai%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you know that your filenames cannot contain newlines, then
grep -rFl --include='*.c' --include='*.cpp' string . | wc -l
Otherwise
find . -type f ( -name '*.c' -o -name '*.cpp' ) -exec grep -Fq string {} ; -printf x | wc -c
2
Completely forgotgrep
has--include
flag. +1 for that
– Sergiy Kolodyazhnyy
Jan 11 at 21:11
add a comment |
If you know that your filenames cannot contain newlines, then
grep -rFl --include='*.c' --include='*.cpp' string . | wc -l
Otherwise
find . -type f ( -name '*.c' -o -name '*.cpp' ) -exec grep -Fq string {} ; -printf x | wc -c
2
Completely forgotgrep
has--include
flag. +1 for that
– Sergiy Kolodyazhnyy
Jan 11 at 21:11
add a comment |
If you know that your filenames cannot contain newlines, then
grep -rFl --include='*.c' --include='*.cpp' string . | wc -l
Otherwise
find . -type f ( -name '*.c' -o -name '*.cpp' ) -exec grep -Fq string {} ; -printf x | wc -c
If you know that your filenames cannot contain newlines, then
grep -rFl --include='*.c' --include='*.cpp' string . | wc -l
Otherwise
find . -type f ( -name '*.c' -o -name '*.cpp' ) -exec grep -Fq string {} ; -printf x | wc -c
answered Jan 11 at 21:07
steeldriversteeldriver
68k11111182
68k11111182
2
Completely forgotgrep
has--include
flag. +1 for that
– Sergiy Kolodyazhnyy
Jan 11 at 21:11
add a comment |
2
Completely forgotgrep
has--include
flag. +1 for that
– Sergiy Kolodyazhnyy
Jan 11 at 21:11
2
2
Completely forgot
grep
has --include
flag. +1 for that– Sergiy Kolodyazhnyy
Jan 11 at 21:11
Completely forgot
grep
has --include
flag. +1 for that– Sergiy Kolodyazhnyy
Jan 11 at 21:11
add a comment |
grep -l string *.c *.cpp | wc -l
- If any filenames contain newlines, the count will be too high.
- If the globs fail to match, you will get an error like
grep: *.c: No such file or directory
, but the count will still be correct.
This is like a quick and dirty version of steeldriver's answer.
1
Consider alsoshopt -s dotglob
or adding.*.c .*.cpp
to account for filenames with leading dot. Also consider2>/dev/null
if you don't wanna see the error message.
– Sergiy Kolodyazhnyy
Jan 11 at 21:24
1
@Serg All good points.grep -s
will also hide error messages, like2>/dev/null
.
– wjandrea
Jan 11 at 21:26
add a comment |
grep -l string *.c *.cpp | wc -l
- If any filenames contain newlines, the count will be too high.
- If the globs fail to match, you will get an error like
grep: *.c: No such file or directory
, but the count will still be correct.
This is like a quick and dirty version of steeldriver's answer.
1
Consider alsoshopt -s dotglob
or adding.*.c .*.cpp
to account for filenames with leading dot. Also consider2>/dev/null
if you don't wanna see the error message.
– Sergiy Kolodyazhnyy
Jan 11 at 21:24
1
@Serg All good points.grep -s
will also hide error messages, like2>/dev/null
.
– wjandrea
Jan 11 at 21:26
add a comment |
grep -l string *.c *.cpp | wc -l
- If any filenames contain newlines, the count will be too high.
- If the globs fail to match, you will get an error like
grep: *.c: No such file or directory
, but the count will still be correct.
This is like a quick and dirty version of steeldriver's answer.
grep -l string *.c *.cpp | wc -l
- If any filenames contain newlines, the count will be too high.
- If the globs fail to match, you will get an error like
grep: *.c: No such file or directory
, but the count will still be correct.
This is like a quick and dirty version of steeldriver's answer.
answered Jan 11 at 21:16
wjandreawjandrea
9,21442363
9,21442363
1
Consider alsoshopt -s dotglob
or adding.*.c .*.cpp
to account for filenames with leading dot. Also consider2>/dev/null
if you don't wanna see the error message.
– Sergiy Kolodyazhnyy
Jan 11 at 21:24
1
@Serg All good points.grep -s
will also hide error messages, like2>/dev/null
.
– wjandrea
Jan 11 at 21:26
add a comment |
1
Consider alsoshopt -s dotglob
or adding.*.c .*.cpp
to account for filenames with leading dot. Also consider2>/dev/null
if you don't wanna see the error message.
– Sergiy Kolodyazhnyy
Jan 11 at 21:24
1
@Serg All good points.grep -s
will also hide error messages, like2>/dev/null
.
– wjandrea
Jan 11 at 21:26
1
1
Consider also
shopt -s dotglob
or adding .*.c .*.cpp
to account for filenames with leading dot. Also consider 2>/dev/null
if you don't wanna see the error message.– Sergiy Kolodyazhnyy
Jan 11 at 21:24
Consider also
shopt -s dotglob
or adding .*.c .*.cpp
to account for filenames with leading dot. Also consider 2>/dev/null
if you don't wanna see the error message.– Sergiy Kolodyazhnyy
Jan 11 at 21:24
1
1
@Serg All good points.
grep -s
will also hide error messages, like 2>/dev/null
.– wjandrea
Jan 11 at 21:26
@Serg All good points.
grep -s
will also hide error messages, like 2>/dev/null
.– wjandrea
Jan 11 at 21:26
add a comment |
Use the grep command to find out:
find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp" ) -exec grep "string" {} ; | wc -l
add a comment |
Use the grep command to find out:
find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp" ) -exec grep "string" {} ; | wc -l
add a comment |
Use the grep command to find out:
find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp" ) -exec grep "string" {} ; | wc -l
Use the grep command to find out:
find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp" ) -exec grep "string" {} ; | wc -l
edited Jan 12 at 0:25
answered Jan 11 at 21:03
George UdosenGeorge Udosen
21k94569
21k94569
add a comment |
add a comment |
Simple way would be via bash
recursive glob combined with single match parameter in grep
(one file - one match) :
shopt -s globstar
grep -m 1 'string' */**.{c,cpp} | wc -l
Why not grep -R
? Because -R
walks through all files, and doesn't filter .c
or .cpp
files, so we use bash's globbing to do that job.
Otherwise, for shell-agnostic way you can use find
:
find -type f ( -iname "*.c" -or -iname "*.cpp" ) -exec grep -q 'string' {} ; -and -print | wc -l
add a comment |
Simple way would be via bash
recursive glob combined with single match parameter in grep
(one file - one match) :
shopt -s globstar
grep -m 1 'string' */**.{c,cpp} | wc -l
Why not grep -R
? Because -R
walks through all files, and doesn't filter .c
or .cpp
files, so we use bash's globbing to do that job.
Otherwise, for shell-agnostic way you can use find
:
find -type f ( -iname "*.c" -or -iname "*.cpp" ) -exec grep -q 'string' {} ; -and -print | wc -l
add a comment |
Simple way would be via bash
recursive glob combined with single match parameter in grep
(one file - one match) :
shopt -s globstar
grep -m 1 'string' */**.{c,cpp} | wc -l
Why not grep -R
? Because -R
walks through all files, and doesn't filter .c
or .cpp
files, so we use bash's globbing to do that job.
Otherwise, for shell-agnostic way you can use find
:
find -type f ( -iname "*.c" -or -iname "*.cpp" ) -exec grep -q 'string' {} ; -and -print | wc -l
Simple way would be via bash
recursive glob combined with single match parameter in grep
(one file - one match) :
shopt -s globstar
grep -m 1 'string' */**.{c,cpp} | wc -l
Why not grep -R
? Because -R
walks through all files, and doesn't filter .c
or .cpp
files, so we use bash's globbing to do that job.
Otherwise, for shell-agnostic way you can use find
:
find -type f ( -iname "*.c" -or -iname "*.cpp" ) -exec grep -q 'string' {} ; -and -print | wc -l
edited Jan 11 at 21:28
answered Jan 11 at 21:08
Sergiy KolodyazhnyySergiy Kolodyazhnyy
72.6k9152316
72.6k9152316
add a comment |
add a comment |
I would recommend using the tool ripgrep (snap) for these kinds of task (grepping in source code repositories):
$ rg -g '*.c' -g '*.cpp' -l string | wc -l
The following command may also be useful:
$ rg -t c -t cpp -l string | wc -l
Which searches files with the following extensions:
$ rg --type-list | grep -E '^c:|^cpp:'
c: *.H, *.c, *.h
cpp: *.C, *.H, *.cc, *.cpp, *.cxx, *.h, *.hh, *.hpp, *.hxx, *.inl
The flags used are:
-l, --files-with-matches
Only print the paths with at least one match.
This overrides --files-without-match.
-g, --glob <GLOB>...
Include or exclude files and directories for searching that match the given
glob. This always overrides any other ignore logic. Multiple glob flags may be
used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude
it.
-t, --type <TYPE>...
Only search files matching TYPE. Multiple type flags may be provided. Use the
--type-list flag to list all available types.
add a comment |
I would recommend using the tool ripgrep (snap) for these kinds of task (grepping in source code repositories):
$ rg -g '*.c' -g '*.cpp' -l string | wc -l
The following command may also be useful:
$ rg -t c -t cpp -l string | wc -l
Which searches files with the following extensions:
$ rg --type-list | grep -E '^c:|^cpp:'
c: *.H, *.c, *.h
cpp: *.C, *.H, *.cc, *.cpp, *.cxx, *.h, *.hh, *.hpp, *.hxx, *.inl
The flags used are:
-l, --files-with-matches
Only print the paths with at least one match.
This overrides --files-without-match.
-g, --glob <GLOB>...
Include or exclude files and directories for searching that match the given
glob. This always overrides any other ignore logic. Multiple glob flags may be
used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude
it.
-t, --type <TYPE>...
Only search files matching TYPE. Multiple type flags may be provided. Use the
--type-list flag to list all available types.
add a comment |
I would recommend using the tool ripgrep (snap) for these kinds of task (grepping in source code repositories):
$ rg -g '*.c' -g '*.cpp' -l string | wc -l
The following command may also be useful:
$ rg -t c -t cpp -l string | wc -l
Which searches files with the following extensions:
$ rg --type-list | grep -E '^c:|^cpp:'
c: *.H, *.c, *.h
cpp: *.C, *.H, *.cc, *.cpp, *.cxx, *.h, *.hh, *.hpp, *.hxx, *.inl
The flags used are:
-l, --files-with-matches
Only print the paths with at least one match.
This overrides --files-without-match.
-g, --glob <GLOB>...
Include or exclude files and directories for searching that match the given
glob. This always overrides any other ignore logic. Multiple glob flags may be
used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude
it.
-t, --type <TYPE>...
Only search files matching TYPE. Multiple type flags may be provided. Use the
--type-list flag to list all available types.
I would recommend using the tool ripgrep (snap) for these kinds of task (grepping in source code repositories):
$ rg -g '*.c' -g '*.cpp' -l string | wc -l
The following command may also be useful:
$ rg -t c -t cpp -l string | wc -l
Which searches files with the following extensions:
$ rg --type-list | grep -E '^c:|^cpp:'
c: *.H, *.c, *.h
cpp: *.C, *.H, *.cc, *.cpp, *.cxx, *.h, *.hh, *.hpp, *.hxx, *.inl
The flags used are:
-l, --files-with-matches
Only print the paths with at least one match.
This overrides --files-without-match.
-g, --glob <GLOB>...
Include or exclude files and directories for searching that match the given
glob. This always overrides any other ignore logic. Multiple glob flags may be
used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude
it.
-t, --type <TYPE>...
Only search files matching TYPE. Multiple type flags may be provided. Use the
--type-list flag to list all available types.
answered Jan 12 at 3:57
htaccesshtaccess
90767
90767
add a comment |
add a comment |
You should be able to find out how many files end in .c
or .cpp
that contains the word "string" by executing:
cat *.c* | grep -i "string" | wc -l
add a comment |
You should be able to find out how many files end in .c
or .cpp
that contains the word "string" by executing:
cat *.c* | grep -i "string" | wc -l
add a comment |
You should be able to find out how many files end in .c
or .cpp
that contains the word "string" by executing:
cat *.c* | grep -i "string" | wc -l
You should be able to find out how many files end in .c
or .cpp
that contains the word "string" by executing:
cat *.c* | grep -i "string" | wc -l
edited Jan 12 at 15:00
answered Jan 12 at 14:54
NETcrypt0rNETcrypt0r
13
13
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1108979%2fhow-can-i-find-out-how-many-files-ending-in-c-or-cpp-inside-a-directory-contai%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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