{"id":466531,"date":"2022-06-22T16:00:53","date_gmt":"2022-06-22T13:00:53","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-exclude-patterns-files-and-directories-with-grep\/"},"modified":"2022-06-22T16:00:53","modified_gmt":"2022-06-22T13:00:53","slug":"how-to-exclude-patterns-files-and-directories-with-grep","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-exclude-patterns-files-and-directories-with-grep\/","title":{"rendered":"#How to Exclude Patterns, Files, and Directories With grep"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2670646cf51\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2670646cf51\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-exclude-patterns-files-and-directories-with-grep\/#%E2%80%9CHow_to_Exclude_Patterns_Files_and_Directories_With_grep%E2%80%9D\" >&#8220;How to Exclude Patterns, Files, and Directories With grep&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-exclude-patterns-files-and-directories-with-grep\/#The_grep_Command\" >The grep Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-exclude-patterns-files-and-directories-with-grep\/#Excluding_Patterns\" >Excluding Patterns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-exclude-patterns-files-and-directories-with-grep\/#Excluding_Files\" >Excluding Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-exclude-patterns-files-and-directories-with-grep\/#Excluding_Directories\" >Excluding Directories<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-exclude-patterns-files-and-directories-with-grep\/#Sometimes_Its_What_You_Leave_Out\" >Sometimes It\u2019s What You Leave Out<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Exclude_Patterns_Files_and_Directories_With_grep%E2%80%9D\"><\/span>&#8220;How to Exclude Patterns, Files, and Directories With grep&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-791996\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/03\/shutterstock_321627716.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Linux laptop showing a bash prompt\" width=\"1200\" height=\"675\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/linux-interface-screen-notebook-world-map-321627716\">fatmawati achmad zaenuri\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>Since 1974, the Linux <code>grep<\/code> command has been helping people find strings in files. But sometimes <code>grep<\/code> is just too thorough. Here are several ways to tell <code>grep<\/code> to ignore different things.<\/p>\n<h2 id=\"the-grep-command\"><span class=\"ez-toc-section\" id=\"The_grep_Command\"><\/span>The grep Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>grep<\/code> command searches text files looking for strings that match the search patterns you provide on the command line. The power of <code>grep<\/code> lies in its use of regular expressions. These let you describe what you\u2019re looking for, rather than have to explicitly define it.<\/p>\n<p>The birth of <code>grep<\/code> pre-dates Linux. it was developed in the early 1970s on Unix. It takes its name from the g\/re\/p key sequence in the <code>ed<\/code> line editor (incidentally, pronounced \u201cee-dee\u201d). This stood for\u00a0<strong>g<\/strong>lobal,\u00a0<strong>re<\/strong>gular express search,\u00a0<strong>p<\/strong>rint matching lines.<\/p>\n<p><code>grep<\/code> is famously\u2014perhaps, notoriously\u2014thorough and single-minded. Sometimes it\u2019ll search files or directories you\u2019d rather it didn\u2019t waste its time on, because the results can leave you unable to see the wood for the trees.<\/p>\n<p>Of course, there are ways to reign grep in. You can tell it to ignore patterns, files, and directories so that grep completes its searches faster, and you\u2019re not swamped with meaningless false positives.<\/p>\n<h2 id=\"excluding-patterns\"><span class=\"ez-toc-section\" id=\"Excluding_Patterns\"><\/span>Excluding Patterns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To search with <code>grep<\/code> you can pipe input to it from some other process such as <code>cat<\/code> , or you can provide a filename as the last command line parameter.<\/p>\n<p>We\u2019re using a short file that contains the text of the poem\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Jabberwocky\"><em>Jabberwocky<\/em><\/a>, by Lewis Carroll. In these two examples, we\u2019re searching for lines that match the search term \u201cJabberwock.\u201d<\/p>\n<pre>cat jabberwocky.txt | grep \"Jabberwock\"<\/pre>\n<pre>grep \"Jabberwock\" jabberwocky.text<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805401\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/1-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Two different ways to search through the same text file with grep\" width=\"644\" height=\"250\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The lines that contain matches to the search clue are listed for us, with the matching element in each line highlighted in red. That\u2019s straightforward searching. But what if we want to exclude lines that contain the word \u201cJabberwock\u201d and print the rest?<\/p>\n<p>We can accomplish that with the <code>-v<\/code> (invert match) option. This lists the lines that don\u2019t match the search term.<\/p>\n<pre>grep -v \"Jabberwock\" jabberwocky.text<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805402\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/2-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the -v inverted search option with grep\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The lines that don\u2019t contain \u201cJabberwock\u201d are listed to the terminal window.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805403\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/3-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"All of the lines that don't contain the word jabberwock\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can exclude as many terms as we wish. Let\u2019s filter out any lines that contain \u201cJabberwock\u201d and any lines that contain \u201cand.\u201d To achieve this we\u2019ll use the <code>-e<\/code> (expression) option. We need to use it for each search pattern we\u2019re using.<\/p>\n<pre>grep -v -e \"Jabberwock\" -e \"and\" jabberwocky.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805404\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/4-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using multiple search clauses with grep\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>There\u2019s a corresponding drop in the number of lines in the output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805405\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/5-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The lines from the text that do not match either search term\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If we use the <code>-E<\/code> (extended regexes) option, we can combine the search patterns with \u201c<code>|<\/code>\u201c, which in this context doesn\u2019t indicate a pipe, it\u2019s the logical <code>OR<\/code> operator.<\/p>\n<pre>grep -Ev \"Jabberwock|and\" jabberwocky.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805406\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/6-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the logical OR operator with grep\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We get exactly the same output as we did with the previous, longer-winded command.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805407\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/7-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The lines from the text that do not match either search term\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The format of the command is the same if you want to use a regex pattern instead of an explicit search clue. This command will exclude all lines that start with any letter in the set of \u201cACHT.\u201d<\/p>\n<pre>grep -Ev \"^ACHT\" jabberwocky.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805408\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/8-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Excluding files that start with particular letters\" width=\"644\" height=\"365\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To see lines that contain a pattern but which also don\u2019t contain another pattern, we can pipe <code>grep<\/code> into <code>grep<\/code> . We\u2019ll search for all lines that contain the word \u201cJabberwock\u201d and then filter out any lines that\u00a0<em>also<\/em>\u00a0contain the word \u201cslain.\u201d<\/p>\n<pre>grep \"Jabberwock\" jabberwocky.txt | grep -v \"slain\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805409\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/9-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Piping grep into grep to filter twice\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"excluding-files\"><span class=\"ez-toc-section\" id=\"Excluding_Files\"><\/span>Excluding Files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We can ask <code>grep<\/code> to look for a string or pattern in a collection of files. You could list each file on the command line, but with many files that <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roach doesn\u2019t scale.<\/p>\n<pre>grep \"vorpal\" verse-1.txt verse-2.txt verse-3.txt verse-4.txt verse-5.txt verse-6.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805410\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/10-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Searching through a list of named files\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Note that the name of the file containing the matching line is displayed at the start of each line of output.<\/p>\n<p>To reduce typing we can use wildcards. But that can be counterintuitive. This appears to work.<\/p>\n<pre>grep \"vorpal\" *.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805411\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/11-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using wildcards to search a collection of files\" width=\"644\" height=\"110\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>However, in this directory there are other TXT files, with nothing to do with the poem. If we search for the word \u201csword\u201d with the same command structure, we get a lot of false positives.<\/p>\n<pre>grep \"sword\" *.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805412\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/12-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Searching for &quot;sword&quot; through a collection of TXT files\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The results we want are masked by the deluge of false results from the other files that have the TXT extension.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805413\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/13-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"A large results set of false positives\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The word \u201cvorpal\u201d didn\u2019t match anything, but \u201csword\u201d is included in the word \u201cpassword\u201d so it was found many times in some pseudo-logfiles.<\/p>\n<p>We need to exclude these files. To do that we\u2019ll use the <code>--exclude<\/code> option. To exclude a single file called \u201cvol-log-1.txt\u201d we\u2019d use this command:<\/p>\n<pre>grep --exclude=vol-log-1.txt \"sword\" *.txt<\/pre>\n<p>In this instance, we want to exclude multiple log files with names that start with \u201cvol.\u201d The syntax we need is:<\/p>\n<pre>grep --exclude=vol*.txt \"sword\" *.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805414\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/14-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Excluding files with wildcards \" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>When we use the <code>-R<\/code> (dereference-recursive) option <code>grep<\/code> will search entire directory trees for us. By default, it will search through all files in those locations. There may well be multiple types of files we wish to exclude.<\/p>\n<p>Beneath the current directory on this test machine, there are nested directories containing logfiles, CSV files, and MD files. These are all types of text files that we want to exclude. We could use an <code>--exclude<\/code> option for each file type, but we can achieve what we want more efficiently by grouping the file types.<\/p>\n<p>This command excludes all files that have CSV or MD extensions, and all TXT files whose names start with either \u201cvol\u201d or \u201clog.\u201d<\/p>\n<pre>grep -R --exclude=*.{csv,md} --exclude={vol*,log*}.txt \"sword\" \/home\/dave\/data\/<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805415\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/15-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using multiple --exclude clauses and filename groupings\" width=\"644\" height=\"190\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"excluding-directories\"><span class=\"ez-toc-section\" id=\"Excluding_Directories\"><\/span>Excluding Directories<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If the files we want to ignore are contained in directories and there are no files in those directories that we do want to search, we can exclude those entire directories.<\/p>\n<p>The concept is very similar to that of excluding files, except we use the <code>--exclude-dir<\/code> option and name the directories to ignore.<\/p>\n<pre>grep -R --exclude-dir=backup \"vorpal\" \/home\/dave\/data<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805417\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/16-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Excluding a directory from the search\" width=\"644\" height=\"190\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We\u2019ve excluded the \u201cbackup\u201d directory, but we\u2019re still searching through another directory called \u201cbackup2.\u201d<\/p>\n<p>It\u2019ll come as no surprise that we can use the <code>--exclude-dir<\/code> option multiple times in a single command. Note that the path to excluded directories should be given relative to the directory the search will start in. Don\u2019t use the absolute path from the root of the file system.<\/p>\n<pre>grep -R --exclude-dir=backup --exclude-dir=backup2 \"vorpal\" \/home\/dave\/data<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805418\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/16-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Excluding two directories from the search\" width=\"644\" height=\"190\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can use groupings too. We can achieve the same thing more succinctly with:<\/p>\n<pre>grep -R --exclude-dir={backup,backup2} \"vorpal\" \/home\/dave\/data<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805419\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/18-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Excluding directories with grouping\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can combine file and directory exclusions in the same command. If you want to exclude all files from a directory and exclude certain file types from the directories that <em>are<\/em> searched, use this syntax:<\/p>\n<pre>grep -R --exclude=*.{csv,md} --exclude-dir=backup\/archive \"frumious\" \/home\/dave\/data<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805420\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/19-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Excluding file types and directories in the same command\" width=\"644\" height=\"170\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"wrap-up\"><span class=\"ez-toc-section\" id=\"Sometimes_Its_What_You_Leave_Out\"><\/span>Sometimes It\u2019s What You Leave Out<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sometimes with <code>grep<\/code> it can feel like you\u2019re trying to find a needle in a haystack. it makes a big difference to remove the haystack.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Use Regular Expressions (regexes) on Linux<\/em><\/strong><\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/805398\/how-to-exclude-patterns-files-and-directories-with-grep\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Exclude Patterns, Files, and Directories With grep&#8221; fatmawati achmad zaenuri\/Shutterstock.com Since 1974, the Linux grep command has been helping people find strings in files. But sometimes grep is just too thorough. Here are several ways to tell grep to ignore different things. The grep Command The grep command searches text files looking for&#8230;<\/p>\n","protected":false},"author":1,"featured_media":466532,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/03\/shutterstock_321627716.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-466531","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/466531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=466531"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/466531\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/466532"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=466531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=466531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=466531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}