Finding a string in a file on a file system using Grep

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











up vote
1
down vote

favorite












I need to do a search on a number of files in a file system to look for a string "RUSH2112". I have completed the following command however I am not sure if this is correct. I stand in the mounted file system.



find . -type f -exec grep -i1 "Rush2112" ; 









share|improve this question



















  • 1




    What Unix are you on and what does the -1 option do with your grep?
    – Kusalananda
    1 hour ago










  • What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
    – Jeff Schaller
    1 hour ago














up vote
1
down vote

favorite












I need to do a search on a number of files in a file system to look for a string "RUSH2112". I have completed the following command however I am not sure if this is correct. I stand in the mounted file system.



find . -type f -exec grep -i1 "Rush2112" ; 









share|improve this question



















  • 1




    What Unix are you on and what does the -1 option do with your grep?
    – Kusalananda
    1 hour ago










  • What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
    – Jeff Schaller
    1 hour ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I need to do a search on a number of files in a file system to look for a string "RUSH2112". I have completed the following command however I am not sure if this is correct. I stand in the mounted file system.



find . -type f -exec grep -i1 "Rush2112" ; 









share|improve this question















I need to do a search on a number of files in a file system to look for a string "RUSH2112". I have completed the following command however I am not sure if this is correct. I stand in the mounted file system.



find . -type f -exec grep -i1 "Rush2112" ; 






grep find






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









Jeff Schaller

34.9k952115




34.9k952115










asked 1 hour ago









Deirdre

315




315







  • 1




    What Unix are you on and what does the -1 option do with your grep?
    – Kusalananda
    1 hour ago










  • What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
    – Jeff Schaller
    1 hour ago












  • 1




    What Unix are you on and what does the -1 option do with your grep?
    – Kusalananda
    1 hour ago










  • What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
    – Jeff Schaller
    1 hour ago







1




1




What Unix are you on and what does the -1 option do with your grep?
– Kusalananda
1 hour ago




What Unix are you on and what does the -1 option do with your grep?
– Kusalananda
1 hour ago












What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
– Jeff Schaller
1 hour ago




What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
– Jeff Schaller
1 hour ago










1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










To look for a string (not a regular expression) without caring for case, use grep -iF STRING (where STRING should be the quoted string you are looking for).



To recurse over all regular files in a directory hierarchy, do



find . -type f -exec grep -iF STRING /dev/null +


The main difference between this and your command is that this will execute grep for groups of found files. Your command would run grep once for each found file which may be slower if there are many files.



The other difference is that since grep is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null is there to force this behaviour in case grep happens to be invoked with only a single file. You may use grep's -H option instead of including /dev/null if your implementation of grep has that non-standard (but common) option.



Some implementation of grep also has the option to recurse directories by itself, without the help of find. This is often done with an -R option:



grep -R -iF STRING .


Note that GNU grep also has a -r option that is slightly different from its -R option (with -r, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep via find . -type f).




If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.




  • Use grep -q as a test on each file:



    find . -type f -exec grep -qiF STRING ';' -print



  • Use grep -l across groups of files found by find:



    find . -type f -exec grep -ilF STRING +



  • Use recursive grep -l:



    grep -R -ilF STRING .


If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:



find . -type f -exec grep -qiF STRING ';' -exec sh -c '
for pathname do
# Process "$pathname" here
done' sh +


Related:



  • Understanding the -exec option of `find`





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%2f479113%2ffinding-a-string-in-a-file-on-a-file-system-using-grep%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    To look for a string (not a regular expression) without caring for case, use grep -iF STRING (where STRING should be the quoted string you are looking for).



    To recurse over all regular files in a directory hierarchy, do



    find . -type f -exec grep -iF STRING /dev/null +


    The main difference between this and your command is that this will execute grep for groups of found files. Your command would run grep once for each found file which may be slower if there are many files.



    The other difference is that since grep is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null is there to force this behaviour in case grep happens to be invoked with only a single file. You may use grep's -H option instead of including /dev/null if your implementation of grep has that non-standard (but common) option.



    Some implementation of grep also has the option to recurse directories by itself, without the help of find. This is often done with an -R option:



    grep -R -iF STRING .


    Note that GNU grep also has a -r option that is slightly different from its -R option (with -r, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep via find . -type f).




    If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.




    • Use grep -q as a test on each file:



      find . -type f -exec grep -qiF STRING ';' -print



    • Use grep -l across groups of files found by find:



      find . -type f -exec grep -ilF STRING +



    • Use recursive grep -l:



      grep -R -ilF STRING .


    If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:



    find . -type f -exec grep -qiF STRING ';' -exec sh -c '
    for pathname do
    # Process "$pathname" here
    done' sh +


    Related:



    • Understanding the -exec option of `find`





    share|improve this answer


























      up vote
      5
      down vote



      accepted










      To look for a string (not a regular expression) without caring for case, use grep -iF STRING (where STRING should be the quoted string you are looking for).



      To recurse over all regular files in a directory hierarchy, do



      find . -type f -exec grep -iF STRING /dev/null +


      The main difference between this and your command is that this will execute grep for groups of found files. Your command would run grep once for each found file which may be slower if there are many files.



      The other difference is that since grep is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null is there to force this behaviour in case grep happens to be invoked with only a single file. You may use grep's -H option instead of including /dev/null if your implementation of grep has that non-standard (but common) option.



      Some implementation of grep also has the option to recurse directories by itself, without the help of find. This is often done with an -R option:



      grep -R -iF STRING .


      Note that GNU grep also has a -r option that is slightly different from its -R option (with -r, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep via find . -type f).




      If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.




      • Use grep -q as a test on each file:



        find . -type f -exec grep -qiF STRING ';' -print



      • Use grep -l across groups of files found by find:



        find . -type f -exec grep -ilF STRING +



      • Use recursive grep -l:



        grep -R -ilF STRING .


      If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:



      find . -type f -exec grep -qiF STRING ';' -exec sh -c '
      for pathname do
      # Process "$pathname" here
      done' sh +


      Related:



      • Understanding the -exec option of `find`





      share|improve this answer
























        up vote
        5
        down vote



        accepted







        up vote
        5
        down vote



        accepted






        To look for a string (not a regular expression) without caring for case, use grep -iF STRING (where STRING should be the quoted string you are looking for).



        To recurse over all regular files in a directory hierarchy, do



        find . -type f -exec grep -iF STRING /dev/null +


        The main difference between this and your command is that this will execute grep for groups of found files. Your command would run grep once for each found file which may be slower if there are many files.



        The other difference is that since grep is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null is there to force this behaviour in case grep happens to be invoked with only a single file. You may use grep's -H option instead of including /dev/null if your implementation of grep has that non-standard (but common) option.



        Some implementation of grep also has the option to recurse directories by itself, without the help of find. This is often done with an -R option:



        grep -R -iF STRING .


        Note that GNU grep also has a -r option that is slightly different from its -R option (with -r, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep via find . -type f).




        If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.




        • Use grep -q as a test on each file:



          find . -type f -exec grep -qiF STRING ';' -print



        • Use grep -l across groups of files found by find:



          find . -type f -exec grep -ilF STRING +



        • Use recursive grep -l:



          grep -R -ilF STRING .


        If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:



        find . -type f -exec grep -qiF STRING ';' -exec sh -c '
        for pathname do
        # Process "$pathname" here
        done' sh +


        Related:



        • Understanding the -exec option of `find`





        share|improve this answer














        To look for a string (not a regular expression) without caring for case, use grep -iF STRING (where STRING should be the quoted string you are looking for).



        To recurse over all regular files in a directory hierarchy, do



        find . -type f -exec grep -iF STRING /dev/null +


        The main difference between this and your command is that this will execute grep for groups of found files. Your command would run grep once for each found file which may be slower if there are many files.



        The other difference is that since grep is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null is there to force this behaviour in case grep happens to be invoked with only a single file. You may use grep's -H option instead of including /dev/null if your implementation of grep has that non-standard (but common) option.



        Some implementation of grep also has the option to recurse directories by itself, without the help of find. This is often done with an -R option:



        grep -R -iF STRING .


        Note that GNU grep also has a -r option that is slightly different from its -R option (with -r, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep via find . -type f).




        If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.




        • Use grep -q as a test on each file:



          find . -type f -exec grep -qiF STRING ';' -print



        • Use grep -l across groups of files found by find:



          find . -type f -exec grep -ilF STRING +



        • Use recursive grep -l:



          grep -R -ilF STRING .


        If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:



        find . -type f -exec grep -qiF STRING ';' -exec sh -c '
        for pathname do
        # Process "$pathname" here
        done' sh +


        Related:



        • Understanding the -exec option of `find`






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 42 mins ago

























        answered 1 hour ago









        Kusalananda

        113k15216344




        113k15216344



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479113%2ffinding-a-string-in-a-file-on-a-file-system-using-grep%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery