{"id":6764,"date":"2023-03-10T10:45:30","date_gmt":"2023-03-10T15:45:30","guid":{"rendered":"https:\/\/baselines.com\/?p=6764"},"modified":"2023-03-10T10:45:35","modified_gmt":"2023-03-10T15:45:35","slug":"how-can-i-grep-recursively-but-only-in-files-with-certain-extensions","status":"publish","type":"post","link":"https:\/\/baselines.com\/?p=6764","title":{"rendered":"<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions\">How can I grep recursively, but only in files with certain extensions?<\/a>"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Using grep recursively<\/h1>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/posts\/12517022\/timeline\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>\n\n\n\n<p>Just use the&nbsp;<code>--include<\/code>&nbsp;parameter, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -inr --include \\*.h --include \\*.cpp CP_Image ~\/path&#91;12345] | mailx -s GREP email@domain.example\n<\/code><\/pre>\n\n\n\n<p>That should do what you want.<\/p>\n\n\n\n<p>To take the explanation from&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions\/35280826#35280826\" target=\"_blank\" rel=\"noopener\">HoldOffHunger&#8217;s answer<\/a>&nbsp;below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>grep<\/code>: command<\/li>\n\n\n\n<li><code>-r<\/code>: recursively<\/li>\n\n\n\n<li><code>-i<\/code>: ignore-case<\/li>\n\n\n\n<li><code>-n<\/code>: each output line is preceded by its relative line number in the file<\/li>\n\n\n\n<li><code>--include \\*.cpp<\/code>: all *.cpp: C++ files (escape with \\ just in case you have a directory with asterisks in the filenames)<\/li>\n\n\n\n<li><code>.\/<\/code>: Start at current directory.<\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/a\/12517022\" target=\"_blank\" rel=\"noopener\">Share<\/a>Follow<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/posts\/12517022\/revisions\" target=\"_blank\" rel=\"noopener\">edited&nbsp;Jun 20, 2022 at 9:27<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/users\/1145388\/stephen-ostermiller\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/stackoverflow.com\/users\/1145388\/stephen-ostermiller\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/i.stack.imgur.com\/HZ9CL.png?s=64&amp;g=1\" alt=\"Stephen Ostermiller's user avatar\"\/><\/a><\/figure>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/users\/1145388\/stephen-ostermiller\" target=\"_blank\" rel=\"noopener\">Stephen Ostermiller<\/a><\/p>\n\n\n\n<p><strong>22.8k<\/strong>1313 gold badges8686 silver badges105105 bronze badges<\/p>\n\n\n\n<p>answered&nbsp;Sep 20, 2012 at 16:35<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/users\/352672\/nelson\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/stackoverflow.com\/users\/352672\/nelson\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/www.gravatar.com\/avatar\/0a1a2b83e01719764e44982e7fd3f102?s=64&amp;d=identicon&amp;r=PG\" alt=\"Nelson's user avatar\"\/><\/a><\/figure>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/users\/352672\/nelson\" target=\"_blank\" rel=\"noopener\">Nelson<\/a><\/p>\n\n\n\n<p><strong>48.4k<\/strong>88 gold badges6565 silver badges8181 bronze badges<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1@Hong where is the documentation that -R is for symbolic links?&nbsp;\u2013&nbsp;<a href=\"https:\/\/stackoverflow.com\/users\/478525\/titus\" target=\"_blank\" rel=\"noopener\">titus<\/a>&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#comment57539507_12517022\" target=\"_blank\" rel=\"noopener\">Jan 20, 2016 at 13:46<\/a><\/li>\n\n\n\n<li>Can someone explain why this syntax works as opposed to what&#8217;s described in the man page&nbsp;<code>--include=GLOB<\/code>&nbsp;\u2013&nbsp;<a href=\"https:\/\/stackoverflow.com\/users\/1767412\/eaten-by-a-grue\" target=\"_blank\" rel=\"noopener\">Eaten by a Grue<\/a>&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#comment57590795_12517022\" target=\"_blank\" rel=\"noopener\">Jan 21, 2016 at 16:15<\/a><\/li>\n\n\n\n<li>11This example seems to have a high score because it covers such a wide range of possibilites but the answer given below of grep -r &#8211;include=*.txt &#8216;searchterm&#8217; .\/ really explains the essence of the answer&nbsp;\u2013&nbsp;<a href=\"https:\/\/stackoverflow.com\/users\/1499323\/david-casper\" target=\"_blank\" rel=\"noopener\">David Casper<\/a>&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#comment70954154_12517022\" target=\"_blank\" rel=\"noopener\">Jan 27, 2017 at 1:44<\/a>&nbsp;<\/li>\n\n\n\n<li>16why not use double quotes instead of backslash? e.g:&nbsp;<code>grep -r -i --include=\"*.h\" --include=\"*.cpp\" CP_Image <\/code>&nbsp;\u2013&nbsp;<a href=\"https:\/\/stackoverflow.com\/users\/2467424\/pambda\" target=\"_blank\" rel=\"noopener\">pambda<\/a>&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#comment73738170_12517022\" target=\"_blank\" rel=\"noopener\">Apr 11, 2017 at 5:15<\/a><\/li>\n\n\n\n<li>@nelson How would you change this to just return all file extensions&nbsp;\u2013&nbsp;<a href=\"https:\/\/stackoverflow.com\/users\/1419500\/daniel-kaplan\" target=\"_blank\" rel=\"noopener\">Daniel Kaplan<\/a>&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#comment100495541_12517022\" target=\"_blank\" rel=\"noopener\">Jul 10, 2019 at 23:35<\/a><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#\" target=\"_blank\" rel=\"noopener\">Show&nbsp;<strong>1<\/strong>&nbsp;more comment<\/a><\/p>\n\n\n\n<p>433<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/posts\/35280826\/timeline\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>\n\n\n\n<p>Some of these answers seemed too syntax-heavy, or they produced issues on my Debian Server. This worked perfectly for me:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -r --include=\\*.txt 'searchterm' .\/\n<\/code><\/pre>\n\n\n\n<p>&#8230;or case-insensitive version&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -r -i --include=\\*.txt 'searchterm' .\/\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>grep<\/code>: command<\/li>\n\n\n\n<li><code>-r<\/code>: recursively<\/li>\n\n\n\n<li><code>-i<\/code>: ignore-case<\/li>\n\n\n\n<li><code>--include<\/code>: all *.txt: text files (escape with \\ just in case you have a directory with asterisks in the filenames)<\/li>\n\n\n\n<li><code>'searchterm'<\/code>: What to search<\/li>\n\n\n\n<li><code>.\/<\/code>: Start at current directory.<\/li>\n<\/ul>\n\n\n\n<p>Source:&nbsp;<a href=\"https:\/\/phprevolution.blogspot.com\/2018\/08\/how-to-grep-files-in-linux-but-only.html\" target=\"_blank\" rel=\"noopener\">PHP Revolution: How to Grep files in Linux, but only certain file extensions?<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/a\/35280826\" target=\"_blank\" rel=\"noopener\">Share<\/a>Follow<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/posts\/35280826\/revisions\" target=\"_blank\" rel=\"noopener\">edited&nbsp;Jun 17, 2020 at 4:04<\/a><\/p>\n\n\n\n<p>answered&nbsp;Feb 8, 2016 at 22:46<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/users\/2430549\/holdoffhunger\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/stackoverflow.com\/users\/2430549\/holdoffhunger\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/www.gravatar.com\/avatar\/7bfc7728cf3e556a8b9da88e662f4fd3?s=64&amp;d=identicon&amp;r=PG\" alt=\"HoldOffHunger's user avatar\"\/><\/a><\/figure>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/users\/2430549\/holdoffhunger\" target=\"_blank\" rel=\"noopener\">HoldOffHunger<\/a><\/p>\n\n\n\n<p><strong>17.8k<\/strong>88 gold badges9494 silver badges127127 bronze badges<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>9You should escape the&nbsp;<code>*<\/code>&nbsp;using&nbsp;<code>\\*.cpp<\/code>&nbsp;or&nbsp;<code>'*.cpp'<\/code>. Otherwise it won\u2019t give the expected result when the working directory contains some&nbsp;<code>*.txt<\/code>&nbsp;files.&nbsp;\u2013&nbsp;<a href=\"https:\/\/stackoverflow.com\/users\/711006\/melebius\" target=\"_blank\" rel=\"noopener\">Melebius<\/a>&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#comment70050882_35280826\" target=\"_blank\" rel=\"noopener\">Jan 2, 2017 at 7:17<\/a><\/li>\n\n\n\n<li>@Melebius can you explain why it needs escaping &#8211; does it have anything to do with the CPP or TXT extensions you mentioned? Or did you just use those as examples?&nbsp;\u2013&nbsp;<a href=\"https:\/\/stackoverflow.com\/users\/195835\/simon-east\" target=\"_blank\" rel=\"noopener\">Simon East<\/a>&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#comment74389010_35280826\" target=\"_blank\" rel=\"noopener\">Apr 28, 2017 at 3:05<\/a>&nbsp;<\/li>\n\n\n\n<li>2@SimonEast These extensions are those used in this question and answer, nothing special otherwise. It would probably work without escaping when using&nbsp;<code>--include=&lt;pattern&gt;<\/code>&nbsp;but it is important to escape&nbsp;<code>*<\/code>&nbsp;with&nbsp;<code>--include &lt;pattern&gt;<\/code>&nbsp;(a space instead of&nbsp;<code>=<\/code>) which feels very similar otherwise.&nbsp;\u2013&nbsp;<a href=\"https:\/\/stackoverflow.com\/users\/711006\/melebius\" target=\"_blank\" rel=\"noopener\">Melebius<\/a>&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#comment74393666_35280826\" target=\"_blank\" rel=\"noopener\">Apr 28, 2017 at 6:55<\/a><\/li>\n\n\n\n<li>@Melebius adding to what you wrote, it does work with&nbsp;<code>--include=&lt;pattern&gt;<\/code>. It also works with&nbsp;<code>--include&lt;pattern&gt;<\/code>, so long as there are no files matching the pattern in the current directory. I.e., it&#8217;s safest to escape the pattern when you&#8217;re not using the&nbsp;<code>=<\/code>&nbsp;syntax, but you can live dangerously if you assume there are no files matching the pattern in the current directory.&nbsp;\u2013&nbsp;<a href=\"https:\/\/stackoverflow.com\/users\/834521\/tootone\" target=\"_blank\" rel=\"noopener\">TooTone<\/a>&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/12516937\/how-can-i-grep-recursively-but-only-in-files-with-certain-extensions#comment123464949_35280826\" target=\"_blank\" rel=\"noopener\">Nov 4, 2021 at 22:02<\/a><\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n<script>\nvar zbPregResult = '0';\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Using grep recursively Just use the&nbsp;&#8211;include&nbsp;parameter, like this: That should do what you want. To take the explanation from&nbsp;HoldOffHunger&#8217;s answer&nbsp;below: ShareFollow edited&nbsp;Jun 20, 2022 at 9:27 Stephen Ostermiller 22.8k1313 gold badges8686 silver badges105105 bronze badges answered&nbsp;Sep 20, 2012 at 16:35 Nelson 48.4k88 gold badges6565 silver badges8181 bronze badges Show&nbsp;1&nbsp;more comment 433 Some of these answers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[35],"tags":[],"class_list":["post-6764","post","type-post","status-publish","format-standard","hentry","category-music"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/posts\/6764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/baselines.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6764"}],"version-history":[{"count":1,"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/posts\/6764\/revisions"}],"predecessor-version":[{"id":6765,"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/posts\/6764\/revisions\/6765"}],"wp:attachment":[{"href":"https:\/\/baselines.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baselines.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baselines.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}