How can I find out how many files ending in .c or .cpp inside a directory contain the string: string?












0















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.










share|improve this question





























    0















    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.










    share|improve this question



























      0












      0








      0








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 12 at 18:40









      Zanna

      50.7k13136241




      50.7k13136241










      asked Jan 11 at 20:58









      DwayneTheRockJohnsonOfficialDwayneTheRockJohnsonOfficial

      6




      6






















          6 Answers
          6






          active

          oldest

          votes


















          3














          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





          share|improve this answer



















          • 2





            Completely forgot grep has --include flag. +1 for that

            – Sergiy Kolodyazhnyy
            Jan 11 at 21:11



















          2














          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.






          share|improve this answer



















          • 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






          • 1





            @Serg All good points. grep -s will also hide error messages, like 2>/dev/null.

            – wjandrea
            Jan 11 at 21:26



















          2














          Use the grep command to find out:



          find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp"  ) -exec grep "string" {} ; | wc -l





          share|improve this answer

































            1














            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





            share|improve this answer

































              0














              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.





              share|improve this answer































                0














                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






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


                  }
                  });














                  draft saved

                  draft discarded


















                  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









                  3














                  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





                  share|improve this answer



















                  • 2





                    Completely forgot grep has --include flag. +1 for that

                    – Sergiy Kolodyazhnyy
                    Jan 11 at 21:11
















                  3














                  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





                  share|improve this answer



















                  • 2





                    Completely forgot grep has --include flag. +1 for that

                    – Sergiy Kolodyazhnyy
                    Jan 11 at 21:11














                  3












                  3








                  3







                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 11 at 21:07









                  steeldriversteeldriver

                  68k11111182




                  68k11111182








                  • 2





                    Completely forgot grep has --include flag. +1 for that

                    – Sergiy Kolodyazhnyy
                    Jan 11 at 21:11














                  • 2





                    Completely forgot grep 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













                  2














                  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.






                  share|improve this answer



















                  • 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






                  • 1





                    @Serg All good points. grep -s will also hide error messages, like 2>/dev/null.

                    – wjandrea
                    Jan 11 at 21:26
















                  2














                  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.






                  share|improve this answer



















                  • 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






                  • 1





                    @Serg All good points. grep -s will also hide error messages, like 2>/dev/null.

                    – wjandrea
                    Jan 11 at 21:26














                  2












                  2








                  2







                  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.






                  share|improve this 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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 11 at 21:16









                  wjandreawjandrea

                  9,21442363




                  9,21442363








                  • 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






                  • 1





                    @Serg All good points. grep -s will also hide error messages, like 2>/dev/null.

                    – wjandrea
                    Jan 11 at 21:26














                  • 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






                  • 1





                    @Serg All good points. grep -s will also hide error messages, like 2>/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











                  2














                  Use the grep command to find out:



                  find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp"  ) -exec grep "string" {} ; | wc -l





                  share|improve this answer






























                    2














                    Use the grep command to find out:



                    find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp"  ) -exec grep "string" {} ; | wc -l





                    share|improve this answer




























                      2












                      2








                      2







                      Use the grep command to find out:



                      find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp"  ) -exec grep "string" {} ; | wc -l





                      share|improve this answer















                      Use the grep command to find out:



                      find /path/to/directory -type f ( -name "*.c" -o -name "*.cpp"  ) -exec grep "string" {} ; | wc -l






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 12 at 0:25

























                      answered Jan 11 at 21:03









                      George UdosenGeorge Udosen

                      21k94569




                      21k94569























                          1














                          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





                          share|improve this answer






























                            1














                            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





                            share|improve this answer




























                              1












                              1








                              1







                              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





                              share|improve this answer















                              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






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 11 at 21:28

























                              answered Jan 11 at 21:08









                              Sergiy KolodyazhnyySergiy Kolodyazhnyy

                              72.6k9152316




                              72.6k9152316























                                  0














                                  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.





                                  share|improve this answer




























                                    0














                                    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.





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      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.





                                      share|improve this answer













                                      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.






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 12 at 3:57









                                      htaccesshtaccess

                                      90767




                                      90767























                                          0














                                          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






                                          share|improve this answer






























                                            0














                                            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






                                            share|improve this answer




























                                              0












                                              0








                                              0







                                              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






                                              share|improve this answer















                                              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







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Jan 12 at 15:00

























                                              answered Jan 12 at 14:54









                                              NETcrypt0rNETcrypt0r

                                              13




                                              13






























                                                  draft saved

                                                  draft discarded




















































                                                  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.




                                                  draft saved


                                                  draft discarded














                                                  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





















































                                                  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

                                                  Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                                                  ComboBox Display Member on multiple fields

                                                  Is it possible to collect Nectar points via Trainline?