Grep to return entire file content

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
1
down vote

favorite












I want to search recursively for a certain word, say MyWord, but I want to print the entire contents of all files that contain MyWord. How do I do this?



It turns out that I particularly care about binary sqlite files, both grepping them, and reading the result as a text file, rather than binary.










share|improve this question



















  • 1




    Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (-A, -B, -C) may be more useful
    – steeldriver
    Apr 18 at 1:58














up vote
1
down vote

favorite












I want to search recursively for a certain word, say MyWord, but I want to print the entire contents of all files that contain MyWord. How do I do this?



It turns out that I particularly care about binary sqlite files, both grepping them, and reading the result as a text file, rather than binary.










share|improve this question



















  • 1




    Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (-A, -B, -C) may be more useful
    – steeldriver
    Apr 18 at 1:58












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to search recursively for a certain word, say MyWord, but I want to print the entire contents of all files that contain MyWord. How do I do this?



It turns out that I particularly care about binary sqlite files, both grepping them, and reading the result as a text file, rather than binary.










share|improve this question















I want to search recursively for a certain word, say MyWord, but I want to print the entire contents of all files that contain MyWord. How do I do this?



It turns out that I particularly care about binary sqlite files, both grepping them, and reading the result as a text file, rather than binary.







command-line grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 18 at 1:05









muru

130k19273463




130k19273463










asked Apr 17 at 22:26









Baron Yugovich

11616




11616







  • 1




    Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (-A, -B, -C) may be more useful
    – steeldriver
    Apr 18 at 1:58












  • 1




    Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (-A, -B, -C) may be more useful
    – steeldriver
    Apr 18 at 1:58







1




1




Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (-A, -B, -C) may be more useful
– steeldriver
Apr 18 at 1:58




Are you sure you want to read the entire content of each matching file? judicious application of grep's context switches (-A, -B, -C) may be more useful
– steeldriver
Apr 18 at 1:58










3 Answers
3






active

oldest

votes

















up vote
2
down vote













I would use:



sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile


  • Drop sudo if not looking in system directories.

  • Change (root directory) '/' to current directory '.' or specific directory ie '/home/me/stuff'

  • Remove mnt to include Windows files mounted under mnt.

  • Remove lib to include Linux source files

  • Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time


  • --exclude=$HISTFILE##*/ is necessary if you run the command a second time or if the search string MyWord has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.


  • -r recursive, I skip binary files, lZ outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary for perl -0, sort -z and xargs used to cat (print out) the file contents.


  • > MyOutputFile sends output to file instead of screen. Leave this off for output to screen.

Note: Missing from output is the file name, just the file content is listed.






share|improve this answer






















  • How do I write it to an output file?
    – Baron Yugovich
    Apr 17 at 23:11










  • @BaronYugovich I've tested cat > MyOutputFile and excluded 5 thousand lines of bash terminal history file from output.
    – WinEunuuchs2Unix
    Apr 18 at 0:01










  • Excellent answer (+1)!
    – JJoao
    May 11 at 10:47

















up vote
1
down vote













You can also use find with grep:



find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;


This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.






share|improve this answer






















  • How do I write it to an output file?
    – Baron Yugovich
    Apr 17 at 23:01










  • Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
    – Baron Yugovich
    Apr 17 at 23:24










  • to reading a binary file as a text file, you can add -a option to the grep above if you have GNU grep.
    – Î±Ò“sнιη
    Apr 18 at 2:40

















up vote
0
down vote













If your folder are not so big try:



grep -rz '.*MyWord.*' myfolder > output


if necessary add -a






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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    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%2f1025951%2fgrep-to-return-entire-file-content%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    I would use:



    sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile


    • Drop sudo if not looking in system directories.

    • Change (root directory) '/' to current directory '.' or specific directory ie '/home/me/stuff'

    • Remove mnt to include Windows files mounted under mnt.

    • Remove lib to include Linux source files

    • Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time


    • --exclude=$HISTFILE##*/ is necessary if you run the command a second time or if the search string MyWord has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.


    • -r recursive, I skip binary files, lZ outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary for perl -0, sort -z and xargs used to cat (print out) the file contents.


    • > MyOutputFile sends output to file instead of screen. Leave this off for output to screen.

    Note: Missing from output is the file name, just the file content is listed.






    share|improve this answer






















    • How do I write it to an output file?
      – Baron Yugovich
      Apr 17 at 23:11










    • @BaronYugovich I've tested cat > MyOutputFile and excluded 5 thousand lines of bash terminal history file from output.
      – WinEunuuchs2Unix
      Apr 18 at 0:01










    • Excellent answer (+1)!
      – JJoao
      May 11 at 10:47














    up vote
    2
    down vote













    I would use:



    sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile


    • Drop sudo if not looking in system directories.

    • Change (root directory) '/' to current directory '.' or specific directory ie '/home/me/stuff'

    • Remove mnt to include Windows files mounted under mnt.

    • Remove lib to include Linux source files

    • Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time


    • --exclude=$HISTFILE##*/ is necessary if you run the command a second time or if the search string MyWord has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.


    • -r recursive, I skip binary files, lZ outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary for perl -0, sort -z and xargs used to cat (print out) the file contents.


    • > MyOutputFile sends output to file instead of screen. Leave this off for output to screen.

    Note: Missing from output is the file name, just the file content is listed.






    share|improve this answer






















    • How do I write it to an output file?
      – Baron Yugovich
      Apr 17 at 23:11










    • @BaronYugovich I've tested cat > MyOutputFile and excluded 5 thousand lines of bash terminal history file from output.
      – WinEunuuchs2Unix
      Apr 18 at 0:01










    • Excellent answer (+1)!
      – JJoao
      May 11 at 10:47












    up vote
    2
    down vote










    up vote
    2
    down vote









    I would use:



    sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile


    • Drop sudo if not looking in system directories.

    • Change (root directory) '/' to current directory '.' or specific directory ie '/home/me/stuff'

    • Remove mnt to include Windows files mounted under mnt.

    • Remove lib to include Linux source files

    • Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time


    • --exclude=$HISTFILE##*/ is necessary if you run the command a second time or if the search string MyWord has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.


    • -r recursive, I skip binary files, lZ outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary for perl -0, sort -z and xargs used to cat (print out) the file contents.


    • > MyOutputFile sends output to file instead of screen. Leave this off for output to screen.

    Note: Missing from output is the file name, just the file content is listed.






    share|improve this answer














    I would use:



    sudo grep -rIlZ --exclude=$HISTFILE##*/ --exclude-dir=boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var '/' -e 'MyWord' | xargs -0 cat > MyOutputFile


    • Drop sudo if not looking in system directories.

    • Change (root directory) '/' to current directory '.' or specific directory ie '/home/me/stuff'

    • Remove mnt to include Windows files mounted under mnt.

    • Remove lib to include Linux source files

    • Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time


    • --exclude=$HISTFILE##*/ is necessary if you run the command a second time or if the search string MyWord has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.


    • -r recursive, I skip binary files, lZ outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary for perl -0, sort -z and xargs used to cat (print out) the file contents.


    • > MyOutputFile sends output to file instead of screen. Leave this off for output to screen.

    Note: Missing from output is the file name, just the file content is listed.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 18 at 0:00

























    answered Apr 17 at 23:09









    WinEunuuchs2Unix

    35.7k759133




    35.7k759133











    • How do I write it to an output file?
      – Baron Yugovich
      Apr 17 at 23:11










    • @BaronYugovich I've tested cat > MyOutputFile and excluded 5 thousand lines of bash terminal history file from output.
      – WinEunuuchs2Unix
      Apr 18 at 0:01










    • Excellent answer (+1)!
      – JJoao
      May 11 at 10:47
















    • How do I write it to an output file?
      – Baron Yugovich
      Apr 17 at 23:11










    • @BaronYugovich I've tested cat > MyOutputFile and excluded 5 thousand lines of bash terminal history file from output.
      – WinEunuuchs2Unix
      Apr 18 at 0:01










    • Excellent answer (+1)!
      – JJoao
      May 11 at 10:47















    How do I write it to an output file?
    – Baron Yugovich
    Apr 17 at 23:11




    How do I write it to an output file?
    – Baron Yugovich
    Apr 17 at 23:11












    @BaronYugovich I've tested cat > MyOutputFile and excluded 5 thousand lines of bash terminal history file from output.
    – WinEunuuchs2Unix
    Apr 18 at 0:01




    @BaronYugovich I've tested cat > MyOutputFile and excluded 5 thousand lines of bash terminal history file from output.
    – WinEunuuchs2Unix
    Apr 18 at 0:01












    Excellent answer (+1)!
    – JJoao
    May 11 at 10:47




    Excellent answer (+1)!
    – JJoao
    May 11 at 10:47












    up vote
    1
    down vote













    You can also use find with grep:



    find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;


    This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.






    share|improve this answer






















    • How do I write it to an output file?
      – Baron Yugovich
      Apr 17 at 23:01










    • Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
      – Baron Yugovich
      Apr 17 at 23:24










    • to reading a binary file as a text file, you can add -a option to the grep above if you have GNU grep.
      – Î±Ò“sнιη
      Apr 18 at 2:40














    up vote
    1
    down vote













    You can also use find with grep:



    find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;


    This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.






    share|improve this answer






















    • How do I write it to an output file?
      – Baron Yugovich
      Apr 17 at 23:01










    • Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
      – Baron Yugovich
      Apr 17 at 23:24










    • to reading a binary file as a text file, you can add -a option to the grep above if you have GNU grep.
      – Î±Ò“sнιη
      Apr 18 at 2:40












    up vote
    1
    down vote










    up vote
    1
    down vote









    You can also use find with grep:



    find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;


    This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.






    share|improve this answer














    You can also use find with grep:



    find . -type f -exec grep -wq 'MyWord' ; -exec cat >> /path/to/outfile ;


    This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 17 at 23:13

























    answered Apr 17 at 22:44









    αғsнιη

    23.3k2191152




    23.3k2191152











    • How do I write it to an output file?
      – Baron Yugovich
      Apr 17 at 23:01










    • Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
      – Baron Yugovich
      Apr 17 at 23:24










    • to reading a binary file as a text file, you can add -a option to the grep above if you have GNU grep.
      – Î±Ò“sнιη
      Apr 18 at 2:40
















    • How do I write it to an output file?
      – Baron Yugovich
      Apr 17 at 23:01










    • Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
      – Baron Yugovich
      Apr 17 at 23:24










    • to reading a binary file as a text file, you can add -a option to the grep above if you have GNU grep.
      – Î±Ò“sнιη
      Apr 18 at 2:40















    How do I write it to an output file?
    – Baron Yugovich
    Apr 17 at 23:01




    How do I write it to an output file?
    – Baron Yugovich
    Apr 17 at 23:01












    Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
    – Baron Yugovich
    Apr 17 at 23:24




    Thanks. I do have one more follow-up: the resulting files are sometimes binary, what's the best way to view them as text? This is Mozilla Firefox cached files from browser history.
    – Baron Yugovich
    Apr 17 at 23:24












    to reading a binary file as a text file, you can add -a option to the grep above if you have GNU grep.
    – Î±Ò“sнιη
    Apr 18 at 2:40




    to reading a binary file as a text file, you can add -a option to the grep above if you have GNU grep.
    – Î±Ò“sнιη
    Apr 18 at 2:40










    up vote
    0
    down vote













    If your folder are not so big try:



    grep -rz '.*MyWord.*' myfolder > output


    if necessary add -a






    share|improve this answer
























      up vote
      0
      down vote













      If your folder are not so big try:



      grep -rz '.*MyWord.*' myfolder > output


      if necessary add -a






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        If your folder are not so big try:



        grep -rz '.*MyWord.*' myfolder > output


        if necessary add -a






        share|improve this answer












        If your folder are not so big try:



        grep -rz '.*MyWord.*' myfolder > output


        if necessary add -a







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 11 at 10:44









        JJoao

        1,24059




        1,24059



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1025951%2fgrep-to-return-entire-file-content%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Which professions warranted travel in Medieval times?

            How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

            Trouble downloading packages list due to a “Hash sum mismatch” error