How to exclude some files not matching certain extensions with grep?











up vote
7
down vote

favorite
1












I want to output all lines containing the word OK recursively from a directory. But there are a few extensions which I need to exclude from the result:



*~
*.map
*.js except *.debug.js


I tried:



grep -r --exclude={*~,*.map} "OK" /some/dir


Except that I don't know how to remove from the result all those non-debug .js files.










share|improve this question




























    up vote
    7
    down vote

    favorite
    1












    I want to output all lines containing the word OK recursively from a directory. But there are a few extensions which I need to exclude from the result:



    *~
    *.map
    *.js except *.debug.js


    I tried:



    grep -r --exclude={*~,*.map} "OK" /some/dir


    Except that I don't know how to remove from the result all those non-debug .js files.










    share|improve this question


























      up vote
      7
      down vote

      favorite
      1









      up vote
      7
      down vote

      favorite
      1






      1





      I want to output all lines containing the word OK recursively from a directory. But there are a few extensions which I need to exclude from the result:



      *~
      *.map
      *.js except *.debug.js


      I tried:



      grep -r --exclude={*~,*.map} "OK" /some/dir


      Except that I don't know how to remove from the result all those non-debug .js files.










      share|improve this question















      I want to output all lines containing the word OK recursively from a directory. But there are a few extensions which I need to exclude from the result:



      *~
      *.map
      *.js except *.debug.js


      I tried:



      grep -r --exclude={*~,*.map} "OK" /some/dir


      Except that I don't know how to remove from the result all those non-debug .js files.







      grep wildcards






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 21:02









      Rui F Ribeiro

      38.2k1475125




      38.2k1475125










      asked Apr 16 '14 at 8:47









      Question Overflow

      1,808114067




      1,808114067






















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          I would just pass that through a second grep to remove them:



          grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js'


          The -v reverses the match, printing lines that don't match the pattern and the -P enables Perl Compatible Regular Expressions which let us use negative lookbehinds. This particular regex, will match .js that is not prececeded by debug which means (since we are inverting the matches) that only those .js files will be printed.



          However, as @QuestionOverflow pointed out int the comments, that could have the unintended side effect of filtering out lines that contain OK and js since the grep -v is applied to the entire output, not only the file name. To avoid that, just add a colon (that's what grep uses to separate file names from file contents):



          grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js:'


          That will still fail if your input line contains foo.js: or if your file name contains :. So, to be sure, use a different approach:



          grep -Tr --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).jst'


          The -T causes grep to print a tab between the file name and the file contents. So, if we simply add a t to the end of the regex, it will only match against file names, and not the contents of the line.



          Still, using find might make more sense regardless.






          share|improve this answer



















          • 1




            Would I be inadvertently excluding lines in those files that I want, but containing both OK and .js on the same line?
            – Question Overflow
            Apr 16 '14 at 9:06










          • @QuestionOverflow ah, yes indeed, good catch. See updated answer.
            – terdon
            Apr 16 '14 at 9:20










          • Fantastic answer. Have to accept yours since I ask specifically for grep. Thanks.
            – Question Overflow
            Apr 16 '14 at 9:26










          • @QuestionOverflow you're very welcome. In general though, find is probably better for this kind of thing. Getting the right grep can be tricky as you pointed out :).
            – terdon
            Apr 16 '14 at 9:27












          • Your solutions fail if one has the failglob option set in the shell: bash: no match: --exclude=*~ You need to quote your GLOB pattern arguments to --exclude to hide them from shell expansion, e.g. --exclude={*~,*.map}
            – Ian D. Allen
            Apr 16 '14 at 15:28


















          up vote
          7
          down vote













          I'd use find to locate the files and pipe the result through xargs:



          $ find . -type f ! -name "*~" 
          ! -name "*.map"
          ! ( -name "*.js" -and ! -name "*.debug.js" )
          -print0 | xargs -0 grep "OK"


          This searches for every file not matching "*~", "*.map" or "*.js but not *.debug.js".



          Using find you can easily search for rather complex rules and this approach saves you from accidentally removing false positives as could happen with double grep.






          share|improve this answer





















          • Nice answer too :)
            – Question Overflow
            Apr 16 '14 at 9:27






          • 3




            Yes, this is probably the best way, +1. You could also use -exec grep OK {} + instead of xargs and avoid an extra program.
            – terdon
            Apr 16 '14 at 9:33






          • 2




            @IDAllen no, note that I suggested -exec + not -exec ;, that will run as few commands as possible, much like xargs.
            – terdon
            Apr 16 '14 at 17:20


















          up vote
          4
          down vote













          With zsh you can do:



          setopt extendedglob
          grep OK some/dir/**/^(*~|*.map|(^*debug).js)


          Provided of course the argument list isn't too long, in which case you can always do:



          printf '%s' some/dir/**/^(*~|*.map|(^*debug).js) | xargs -0 grep OK





          share|improve this answer























          • Also, you could make the last one zsh-only: autoload zargs and zargs some/dir/**/^(*~|*.map|(^*debug).js) -- grep OK
            – don_crissti
            Dec 12 '15 at 1:11


















          up vote
          2
          down vote













          If you don't mind seeing the output slightly out of order (if you do, you can sort it):



          grep -r --exclude={*~,*.map,*.js} "OK" /some/dir **/*.debug.js


          This requires that your shell supports ** for recursive globbing: zsh does out of the box, bash does after you run shopt -s globstar, ksh93 does after you run set -o globstar.



          Without ** support in the shell, you can use two grep commands:



          grep -r --exclude={*~,*.map,*.js} "OK" /some/dir
          grep -r --include=*.debug.js "OK" /some/dir





          share|improve this answer























          • My shell supports **, but there seem to be something wrong with the extra argument **/*.debug.js, causing grep to interpret OK as a directory. Have you tried running it?
            – Question Overflow
            Apr 17 '14 at 3:50










          • @QuestionOverflow My mistake, I'd swapped the order of the arguments.
            – Gilles
            Apr 17 '14 at 7:36


















          up vote
          1
          down vote













          You can use ripgrep. By default it ignores hidden files and respects your .gitignore file.



          You can specify the inclusion or exclusion rules by using the following parameters:




          -g/--glob GLOB Include or exclude files and directories for searching that match the given glob.



          -t/--type TYPE Only search files matching TYPE. Multiple type flags may be provided.



          -T/--type-not TYPE Do not search files matching TYPE.



          Use the --type-list flag to list all available types.




          Here are few simple examples:



          rg -Tjs "OK"                              # Excludes *.js, *.jsx, *.vue files.
          rg -tpy "OK" # Includes Python files.
          rg --type-add 'map:*.map' -tmap PATTERN # Excludes *.map files.
          rg -g '!*.js' -g '*.debug.js' PATTERN # Excludes *.js apart of *.debug.js.


          Here is the complete solution to exclude *.~, *.map, *.js, but not *.debug.js:



          rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' "OK"




          Testing:



          $ touch file.~ file.map file.js file.debug.js file.txt file.md
          $ rg --files
          file.debug.js
          file.js
          file.map
          file.md
          file.txt
          $ rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' --files
          file.debug.js
          file.md
          file.txt





          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            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',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f125026%2fhow-to-exclude-some-files-not-matching-certain-extensions-with-grep%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            6
            down vote



            accepted










            I would just pass that through a second grep to remove them:



            grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js'


            The -v reverses the match, printing lines that don't match the pattern and the -P enables Perl Compatible Regular Expressions which let us use negative lookbehinds. This particular regex, will match .js that is not prececeded by debug which means (since we are inverting the matches) that only those .js files will be printed.



            However, as @QuestionOverflow pointed out int the comments, that could have the unintended side effect of filtering out lines that contain OK and js since the grep -v is applied to the entire output, not only the file name. To avoid that, just add a colon (that's what grep uses to separate file names from file contents):



            grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js:'


            That will still fail if your input line contains foo.js: or if your file name contains :. So, to be sure, use a different approach:



            grep -Tr --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).jst'


            The -T causes grep to print a tab between the file name and the file contents. So, if we simply add a t to the end of the regex, it will only match against file names, and not the contents of the line.



            Still, using find might make more sense regardless.






            share|improve this answer



















            • 1




              Would I be inadvertently excluding lines in those files that I want, but containing both OK and .js on the same line?
              – Question Overflow
              Apr 16 '14 at 9:06










            • @QuestionOverflow ah, yes indeed, good catch. See updated answer.
              – terdon
              Apr 16 '14 at 9:20










            • Fantastic answer. Have to accept yours since I ask specifically for grep. Thanks.
              – Question Overflow
              Apr 16 '14 at 9:26










            • @QuestionOverflow you're very welcome. In general though, find is probably better for this kind of thing. Getting the right grep can be tricky as you pointed out :).
              – terdon
              Apr 16 '14 at 9:27












            • Your solutions fail if one has the failglob option set in the shell: bash: no match: --exclude=*~ You need to quote your GLOB pattern arguments to --exclude to hide them from shell expansion, e.g. --exclude={*~,*.map}
              – Ian D. Allen
              Apr 16 '14 at 15:28















            up vote
            6
            down vote



            accepted










            I would just pass that through a second grep to remove them:



            grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js'


            The -v reverses the match, printing lines that don't match the pattern and the -P enables Perl Compatible Regular Expressions which let us use negative lookbehinds. This particular regex, will match .js that is not prececeded by debug which means (since we are inverting the matches) that only those .js files will be printed.



            However, as @QuestionOverflow pointed out int the comments, that could have the unintended side effect of filtering out lines that contain OK and js since the grep -v is applied to the entire output, not only the file name. To avoid that, just add a colon (that's what grep uses to separate file names from file contents):



            grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js:'


            That will still fail if your input line contains foo.js: or if your file name contains :. So, to be sure, use a different approach:



            grep -Tr --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).jst'


            The -T causes grep to print a tab between the file name and the file contents. So, if we simply add a t to the end of the regex, it will only match against file names, and not the contents of the line.



            Still, using find might make more sense regardless.






            share|improve this answer



















            • 1




              Would I be inadvertently excluding lines in those files that I want, but containing both OK and .js on the same line?
              – Question Overflow
              Apr 16 '14 at 9:06










            • @QuestionOverflow ah, yes indeed, good catch. See updated answer.
              – terdon
              Apr 16 '14 at 9:20










            • Fantastic answer. Have to accept yours since I ask specifically for grep. Thanks.
              – Question Overflow
              Apr 16 '14 at 9:26










            • @QuestionOverflow you're very welcome. In general though, find is probably better for this kind of thing. Getting the right grep can be tricky as you pointed out :).
              – terdon
              Apr 16 '14 at 9:27












            • Your solutions fail if one has the failglob option set in the shell: bash: no match: --exclude=*~ You need to quote your GLOB pattern arguments to --exclude to hide them from shell expansion, e.g. --exclude={*~,*.map}
              – Ian D. Allen
              Apr 16 '14 at 15:28













            up vote
            6
            down vote



            accepted







            up vote
            6
            down vote



            accepted






            I would just pass that through a second grep to remove them:



            grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js'


            The -v reverses the match, printing lines that don't match the pattern and the -P enables Perl Compatible Regular Expressions which let us use negative lookbehinds. This particular regex, will match .js that is not prececeded by debug which means (since we are inverting the matches) that only those .js files will be printed.



            However, as @QuestionOverflow pointed out int the comments, that could have the unintended side effect of filtering out lines that contain OK and js since the grep -v is applied to the entire output, not only the file name. To avoid that, just add a colon (that's what grep uses to separate file names from file contents):



            grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js:'


            That will still fail if your input line contains foo.js: or if your file name contains :. So, to be sure, use a different approach:



            grep -Tr --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).jst'


            The -T causes grep to print a tab between the file name and the file contents. So, if we simply add a t to the end of the regex, it will only match against file names, and not the contents of the line.



            Still, using find might make more sense regardless.






            share|improve this answer














            I would just pass that through a second grep to remove them:



            grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js'


            The -v reverses the match, printing lines that don't match the pattern and the -P enables Perl Compatible Regular Expressions which let us use negative lookbehinds. This particular regex, will match .js that is not prececeded by debug which means (since we are inverting the matches) that only those .js files will be printed.



            However, as @QuestionOverflow pointed out int the comments, that could have the unintended side effect of filtering out lines that contain OK and js since the grep -v is applied to the entire output, not only the file name. To avoid that, just add a colon (that's what grep uses to separate file names from file contents):



            grep -r --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).js:'


            That will still fail if your input line contains foo.js: or if your file name contains :. So, to be sure, use a different approach:



            grep -Tr --exclude={*~,*.map} "OK" bar/ | grep -vP '(?<!debug).jst'


            The -T causes grep to print a tab between the file name and the file contents. So, if we simply add a t to the end of the regex, it will only match against file names, and not the contents of the line.



            Still, using find might make more sense regardless.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 13 '17 at 12:37









            Community

            1




            1










            answered Apr 16 '14 at 9:00









            terdon

            126k31243418




            126k31243418








            • 1




              Would I be inadvertently excluding lines in those files that I want, but containing both OK and .js on the same line?
              – Question Overflow
              Apr 16 '14 at 9:06










            • @QuestionOverflow ah, yes indeed, good catch. See updated answer.
              – terdon
              Apr 16 '14 at 9:20










            • Fantastic answer. Have to accept yours since I ask specifically for grep. Thanks.
              – Question Overflow
              Apr 16 '14 at 9:26










            • @QuestionOverflow you're very welcome. In general though, find is probably better for this kind of thing. Getting the right grep can be tricky as you pointed out :).
              – terdon
              Apr 16 '14 at 9:27












            • Your solutions fail if one has the failglob option set in the shell: bash: no match: --exclude=*~ You need to quote your GLOB pattern arguments to --exclude to hide them from shell expansion, e.g. --exclude={*~,*.map}
              – Ian D. Allen
              Apr 16 '14 at 15:28














            • 1




              Would I be inadvertently excluding lines in those files that I want, but containing both OK and .js on the same line?
              – Question Overflow
              Apr 16 '14 at 9:06










            • @QuestionOverflow ah, yes indeed, good catch. See updated answer.
              – terdon
              Apr 16 '14 at 9:20










            • Fantastic answer. Have to accept yours since I ask specifically for grep. Thanks.
              – Question Overflow
              Apr 16 '14 at 9:26










            • @QuestionOverflow you're very welcome. In general though, find is probably better for this kind of thing. Getting the right grep can be tricky as you pointed out :).
              – terdon
              Apr 16 '14 at 9:27












            • Your solutions fail if one has the failglob option set in the shell: bash: no match: --exclude=*~ You need to quote your GLOB pattern arguments to --exclude to hide them from shell expansion, e.g. --exclude={*~,*.map}
              – Ian D. Allen
              Apr 16 '14 at 15:28








            1




            1




            Would I be inadvertently excluding lines in those files that I want, but containing both OK and .js on the same line?
            – Question Overflow
            Apr 16 '14 at 9:06




            Would I be inadvertently excluding lines in those files that I want, but containing both OK and .js on the same line?
            – Question Overflow
            Apr 16 '14 at 9:06












            @QuestionOverflow ah, yes indeed, good catch. See updated answer.
            – terdon
            Apr 16 '14 at 9:20




            @QuestionOverflow ah, yes indeed, good catch. See updated answer.
            – terdon
            Apr 16 '14 at 9:20












            Fantastic answer. Have to accept yours since I ask specifically for grep. Thanks.
            – Question Overflow
            Apr 16 '14 at 9:26




            Fantastic answer. Have to accept yours since I ask specifically for grep. Thanks.
            – Question Overflow
            Apr 16 '14 at 9:26












            @QuestionOverflow you're very welcome. In general though, find is probably better for this kind of thing. Getting the right grep can be tricky as you pointed out :).
            – terdon
            Apr 16 '14 at 9:27






            @QuestionOverflow you're very welcome. In general though, find is probably better for this kind of thing. Getting the right grep can be tricky as you pointed out :).
            – terdon
            Apr 16 '14 at 9:27














            Your solutions fail if one has the failglob option set in the shell: bash: no match: --exclude=*~ You need to quote your GLOB pattern arguments to --exclude to hide them from shell expansion, e.g. --exclude={*~,*.map}
            – Ian D. Allen
            Apr 16 '14 at 15:28




            Your solutions fail if one has the failglob option set in the shell: bash: no match: --exclude=*~ You need to quote your GLOB pattern arguments to --exclude to hide them from shell expansion, e.g. --exclude={*~,*.map}
            – Ian D. Allen
            Apr 16 '14 at 15:28












            up vote
            7
            down vote













            I'd use find to locate the files and pipe the result through xargs:



            $ find . -type f ! -name "*~" 
            ! -name "*.map"
            ! ( -name "*.js" -and ! -name "*.debug.js" )
            -print0 | xargs -0 grep "OK"


            This searches for every file not matching "*~", "*.map" or "*.js but not *.debug.js".



            Using find you can easily search for rather complex rules and this approach saves you from accidentally removing false positives as could happen with double grep.






            share|improve this answer





















            • Nice answer too :)
              – Question Overflow
              Apr 16 '14 at 9:27






            • 3




              Yes, this is probably the best way, +1. You could also use -exec grep OK {} + instead of xargs and avoid an extra program.
              – terdon
              Apr 16 '14 at 9:33






            • 2




              @IDAllen no, note that I suggested -exec + not -exec ;, that will run as few commands as possible, much like xargs.
              – terdon
              Apr 16 '14 at 17:20















            up vote
            7
            down vote













            I'd use find to locate the files and pipe the result through xargs:



            $ find . -type f ! -name "*~" 
            ! -name "*.map"
            ! ( -name "*.js" -and ! -name "*.debug.js" )
            -print0 | xargs -0 grep "OK"


            This searches for every file not matching "*~", "*.map" or "*.js but not *.debug.js".



            Using find you can easily search for rather complex rules and this approach saves you from accidentally removing false positives as could happen with double grep.






            share|improve this answer





















            • Nice answer too :)
              – Question Overflow
              Apr 16 '14 at 9:27






            • 3




              Yes, this is probably the best way, +1. You could also use -exec grep OK {} + instead of xargs and avoid an extra program.
              – terdon
              Apr 16 '14 at 9:33






            • 2




              @IDAllen no, note that I suggested -exec + not -exec ;, that will run as few commands as possible, much like xargs.
              – terdon
              Apr 16 '14 at 17:20













            up vote
            7
            down vote










            up vote
            7
            down vote









            I'd use find to locate the files and pipe the result through xargs:



            $ find . -type f ! -name "*~" 
            ! -name "*.map"
            ! ( -name "*.js" -and ! -name "*.debug.js" )
            -print0 | xargs -0 grep "OK"


            This searches for every file not matching "*~", "*.map" or "*.js but not *.debug.js".



            Using find you can easily search for rather complex rules and this approach saves you from accidentally removing false positives as could happen with double grep.






            share|improve this answer












            I'd use find to locate the files and pipe the result through xargs:



            $ find . -type f ! -name "*~" 
            ! -name "*.map"
            ! ( -name "*.js" -and ! -name "*.debug.js" )
            -print0 | xargs -0 grep "OK"


            This searches for every file not matching "*~", "*.map" or "*.js but not *.debug.js".



            Using find you can easily search for rather complex rules and this approach saves you from accidentally removing false positives as could happen with double grep.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 16 '14 at 9:13









            Andreas Wiese

            7,5302132




            7,5302132












            • Nice answer too :)
              – Question Overflow
              Apr 16 '14 at 9:27






            • 3




              Yes, this is probably the best way, +1. You could also use -exec grep OK {} + instead of xargs and avoid an extra program.
              – terdon
              Apr 16 '14 at 9:33






            • 2




              @IDAllen no, note that I suggested -exec + not -exec ;, that will run as few commands as possible, much like xargs.
              – terdon
              Apr 16 '14 at 17:20


















            • Nice answer too :)
              – Question Overflow
              Apr 16 '14 at 9:27






            • 3




              Yes, this is probably the best way, +1. You could also use -exec grep OK {} + instead of xargs and avoid an extra program.
              – terdon
              Apr 16 '14 at 9:33






            • 2




              @IDAllen no, note that I suggested -exec + not -exec ;, that will run as few commands as possible, much like xargs.
              – terdon
              Apr 16 '14 at 17:20
















            Nice answer too :)
            – Question Overflow
            Apr 16 '14 at 9:27




            Nice answer too :)
            – Question Overflow
            Apr 16 '14 at 9:27




            3




            3




            Yes, this is probably the best way, +1. You could also use -exec grep OK {} + instead of xargs and avoid an extra program.
            – terdon
            Apr 16 '14 at 9:33




            Yes, this is probably the best way, +1. You could also use -exec grep OK {} + instead of xargs and avoid an extra program.
            – terdon
            Apr 16 '14 at 9:33




            2




            2




            @IDAllen no, note that I suggested -exec + not -exec ;, that will run as few commands as possible, much like xargs.
            – terdon
            Apr 16 '14 at 17:20




            @IDAllen no, note that I suggested -exec + not -exec ;, that will run as few commands as possible, much like xargs.
            – terdon
            Apr 16 '14 at 17:20










            up vote
            4
            down vote













            With zsh you can do:



            setopt extendedglob
            grep OK some/dir/**/^(*~|*.map|(^*debug).js)


            Provided of course the argument list isn't too long, in which case you can always do:



            printf '%s' some/dir/**/^(*~|*.map|(^*debug).js) | xargs -0 grep OK





            share|improve this answer























            • Also, you could make the last one zsh-only: autoload zargs and zargs some/dir/**/^(*~|*.map|(^*debug).js) -- grep OK
              – don_crissti
              Dec 12 '15 at 1:11















            up vote
            4
            down vote













            With zsh you can do:



            setopt extendedglob
            grep OK some/dir/**/^(*~|*.map|(^*debug).js)


            Provided of course the argument list isn't too long, in which case you can always do:



            printf '%s' some/dir/**/^(*~|*.map|(^*debug).js) | xargs -0 grep OK





            share|improve this answer























            • Also, you could make the last one zsh-only: autoload zargs and zargs some/dir/**/^(*~|*.map|(^*debug).js) -- grep OK
              – don_crissti
              Dec 12 '15 at 1:11













            up vote
            4
            down vote










            up vote
            4
            down vote









            With zsh you can do:



            setopt extendedglob
            grep OK some/dir/**/^(*~|*.map|(^*debug).js)


            Provided of course the argument list isn't too long, in which case you can always do:



            printf '%s' some/dir/**/^(*~|*.map|(^*debug).js) | xargs -0 grep OK





            share|improve this answer














            With zsh you can do:



            setopt extendedglob
            grep OK some/dir/**/^(*~|*.map|(^*debug).js)


            Provided of course the argument list isn't too long, in which case you can always do:



            printf '%s' some/dir/**/^(*~|*.map|(^*debug).js) | xargs -0 grep OK






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 12 '15 at 1:11









            don_crissti

            48.8k15129157




            48.8k15129157










            answered Apr 16 '14 at 11:34









            Graeme

            24.7k46296




            24.7k46296












            • Also, you could make the last one zsh-only: autoload zargs and zargs some/dir/**/^(*~|*.map|(^*debug).js) -- grep OK
              – don_crissti
              Dec 12 '15 at 1:11


















            • Also, you could make the last one zsh-only: autoload zargs and zargs some/dir/**/^(*~|*.map|(^*debug).js) -- grep OK
              – don_crissti
              Dec 12 '15 at 1:11
















            Also, you could make the last one zsh-only: autoload zargs and zargs some/dir/**/^(*~|*.map|(^*debug).js) -- grep OK
            – don_crissti
            Dec 12 '15 at 1:11




            Also, you could make the last one zsh-only: autoload zargs and zargs some/dir/**/^(*~|*.map|(^*debug).js) -- grep OK
            – don_crissti
            Dec 12 '15 at 1:11










            up vote
            2
            down vote













            If you don't mind seeing the output slightly out of order (if you do, you can sort it):



            grep -r --exclude={*~,*.map,*.js} "OK" /some/dir **/*.debug.js


            This requires that your shell supports ** for recursive globbing: zsh does out of the box, bash does after you run shopt -s globstar, ksh93 does after you run set -o globstar.



            Without ** support in the shell, you can use two grep commands:



            grep -r --exclude={*~,*.map,*.js} "OK" /some/dir
            grep -r --include=*.debug.js "OK" /some/dir





            share|improve this answer























            • My shell supports **, but there seem to be something wrong with the extra argument **/*.debug.js, causing grep to interpret OK as a directory. Have you tried running it?
              – Question Overflow
              Apr 17 '14 at 3:50










            • @QuestionOverflow My mistake, I'd swapped the order of the arguments.
              – Gilles
              Apr 17 '14 at 7:36















            up vote
            2
            down vote













            If you don't mind seeing the output slightly out of order (if you do, you can sort it):



            grep -r --exclude={*~,*.map,*.js} "OK" /some/dir **/*.debug.js


            This requires that your shell supports ** for recursive globbing: zsh does out of the box, bash does after you run shopt -s globstar, ksh93 does after you run set -o globstar.



            Without ** support in the shell, you can use two grep commands:



            grep -r --exclude={*~,*.map,*.js} "OK" /some/dir
            grep -r --include=*.debug.js "OK" /some/dir





            share|improve this answer























            • My shell supports **, but there seem to be something wrong with the extra argument **/*.debug.js, causing grep to interpret OK as a directory. Have you tried running it?
              – Question Overflow
              Apr 17 '14 at 3:50










            • @QuestionOverflow My mistake, I'd swapped the order of the arguments.
              – Gilles
              Apr 17 '14 at 7:36













            up vote
            2
            down vote










            up vote
            2
            down vote









            If you don't mind seeing the output slightly out of order (if you do, you can sort it):



            grep -r --exclude={*~,*.map,*.js} "OK" /some/dir **/*.debug.js


            This requires that your shell supports ** for recursive globbing: zsh does out of the box, bash does after you run shopt -s globstar, ksh93 does after you run set -o globstar.



            Without ** support in the shell, you can use two grep commands:



            grep -r --exclude={*~,*.map,*.js} "OK" /some/dir
            grep -r --include=*.debug.js "OK" /some/dir





            share|improve this answer














            If you don't mind seeing the output slightly out of order (if you do, you can sort it):



            grep -r --exclude={*~,*.map,*.js} "OK" /some/dir **/*.debug.js


            This requires that your shell supports ** for recursive globbing: zsh does out of the box, bash does after you run shopt -s globstar, ksh93 does after you run set -o globstar.



            Without ** support in the shell, you can use two grep commands:



            grep -r --exclude={*~,*.map,*.js} "OK" /some/dir
            grep -r --include=*.debug.js "OK" /some/dir






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 17 '14 at 7:36

























            answered Apr 16 '14 at 23:43









            Gilles

            522k12610401570




            522k12610401570












            • My shell supports **, but there seem to be something wrong with the extra argument **/*.debug.js, causing grep to interpret OK as a directory. Have you tried running it?
              – Question Overflow
              Apr 17 '14 at 3:50










            • @QuestionOverflow My mistake, I'd swapped the order of the arguments.
              – Gilles
              Apr 17 '14 at 7:36


















            • My shell supports **, but there seem to be something wrong with the extra argument **/*.debug.js, causing grep to interpret OK as a directory. Have you tried running it?
              – Question Overflow
              Apr 17 '14 at 3:50










            • @QuestionOverflow My mistake, I'd swapped the order of the arguments.
              – Gilles
              Apr 17 '14 at 7:36
















            My shell supports **, but there seem to be something wrong with the extra argument **/*.debug.js, causing grep to interpret OK as a directory. Have you tried running it?
            – Question Overflow
            Apr 17 '14 at 3:50




            My shell supports **, but there seem to be something wrong with the extra argument **/*.debug.js, causing grep to interpret OK as a directory. Have you tried running it?
            – Question Overflow
            Apr 17 '14 at 3:50












            @QuestionOverflow My mistake, I'd swapped the order of the arguments.
            – Gilles
            Apr 17 '14 at 7:36




            @QuestionOverflow My mistake, I'd swapped the order of the arguments.
            – Gilles
            Apr 17 '14 at 7:36










            up vote
            1
            down vote













            You can use ripgrep. By default it ignores hidden files and respects your .gitignore file.



            You can specify the inclusion or exclusion rules by using the following parameters:




            -g/--glob GLOB Include or exclude files and directories for searching that match the given glob.



            -t/--type TYPE Only search files matching TYPE. Multiple type flags may be provided.



            -T/--type-not TYPE Do not search files matching TYPE.



            Use the --type-list flag to list all available types.




            Here are few simple examples:



            rg -Tjs "OK"                              # Excludes *.js, *.jsx, *.vue files.
            rg -tpy "OK" # Includes Python files.
            rg --type-add 'map:*.map' -tmap PATTERN # Excludes *.map files.
            rg -g '!*.js' -g '*.debug.js' PATTERN # Excludes *.js apart of *.debug.js.


            Here is the complete solution to exclude *.~, *.map, *.js, but not *.debug.js:



            rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' "OK"




            Testing:



            $ touch file.~ file.map file.js file.debug.js file.txt file.md
            $ rg --files
            file.debug.js
            file.js
            file.map
            file.md
            file.txt
            $ rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' --files
            file.debug.js
            file.md
            file.txt





            share|improve this answer

























              up vote
              1
              down vote













              You can use ripgrep. By default it ignores hidden files and respects your .gitignore file.



              You can specify the inclusion or exclusion rules by using the following parameters:




              -g/--glob GLOB Include or exclude files and directories for searching that match the given glob.



              -t/--type TYPE Only search files matching TYPE. Multiple type flags may be provided.



              -T/--type-not TYPE Do not search files matching TYPE.



              Use the --type-list flag to list all available types.




              Here are few simple examples:



              rg -Tjs "OK"                              # Excludes *.js, *.jsx, *.vue files.
              rg -tpy "OK" # Includes Python files.
              rg --type-add 'map:*.map' -tmap PATTERN # Excludes *.map files.
              rg -g '!*.js' -g '*.debug.js' PATTERN # Excludes *.js apart of *.debug.js.


              Here is the complete solution to exclude *.~, *.map, *.js, but not *.debug.js:



              rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' "OK"




              Testing:



              $ touch file.~ file.map file.js file.debug.js file.txt file.md
              $ rg --files
              file.debug.js
              file.js
              file.map
              file.md
              file.txt
              $ rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' --files
              file.debug.js
              file.md
              file.txt





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                You can use ripgrep. By default it ignores hidden files and respects your .gitignore file.



                You can specify the inclusion or exclusion rules by using the following parameters:




                -g/--glob GLOB Include or exclude files and directories for searching that match the given glob.



                -t/--type TYPE Only search files matching TYPE. Multiple type flags may be provided.



                -T/--type-not TYPE Do not search files matching TYPE.



                Use the --type-list flag to list all available types.




                Here are few simple examples:



                rg -Tjs "OK"                              # Excludes *.js, *.jsx, *.vue files.
                rg -tpy "OK" # Includes Python files.
                rg --type-add 'map:*.map' -tmap PATTERN # Excludes *.map files.
                rg -g '!*.js' -g '*.debug.js' PATTERN # Excludes *.js apart of *.debug.js.


                Here is the complete solution to exclude *.~, *.map, *.js, but not *.debug.js:



                rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' "OK"




                Testing:



                $ touch file.~ file.map file.js file.debug.js file.txt file.md
                $ rg --files
                file.debug.js
                file.js
                file.map
                file.md
                file.txt
                $ rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' --files
                file.debug.js
                file.md
                file.txt





                share|improve this answer












                You can use ripgrep. By default it ignores hidden files and respects your .gitignore file.



                You can specify the inclusion or exclusion rules by using the following parameters:




                -g/--glob GLOB Include or exclude files and directories for searching that match the given glob.



                -t/--type TYPE Only search files matching TYPE. Multiple type flags may be provided.



                -T/--type-not TYPE Do not search files matching TYPE.



                Use the --type-list flag to list all available types.




                Here are few simple examples:



                rg -Tjs "OK"                              # Excludes *.js, *.jsx, *.vue files.
                rg -tpy "OK" # Includes Python files.
                rg --type-add 'map:*.map' -tmap PATTERN # Excludes *.map files.
                rg -g '!*.js' -g '*.debug.js' PATTERN # Excludes *.js apart of *.debug.js.


                Here is the complete solution to exclude *.~, *.map, *.js, but not *.debug.js:



                rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' "OK"




                Testing:



                $ touch file.~ file.map file.js file.debug.js file.txt file.md
                $ rg --files
                file.debug.js
                file.js
                file.map
                file.md
                file.txt
                $ rg -g '*.*' -g '!*.~' -g '!*.map' -g '!*.js' -g '*.debug.js' --files
                file.debug.js
                file.md
                file.txt






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 3 at 14:32









                kenorb

                8,131366105




                8,131366105






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f125026%2fhow-to-exclude-some-files-not-matching-certain-extensions-with-grep%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

                    Accessing regular linux commands in Huawei's Dopra Linux

                    Can't connect RFCOMM socket: Host is down

                    Kernel panic - not syncing: Fatal Exception in Interrupt