Find a file that contains a string

Discussion in 'Command Line' started by Karthik Rajashekaran, Jun 6, 2014.

  1. Karthik Rajashekaran

    Karthik Rajashekaran New Member

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Hi


    I am trying to find a command to find a string in a file in Linux /Unix

    Note : you dont know which directory the file is or which file does it have the string
  2. Jim Laughlan

    Jim Laughlan Active Member

    Messages:
    111
    Likes Received:
    63
    Trophy Points:
    28
  3. Karthik Rajashekaran

    Karthik Rajashekaran New Member

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    No this case you need to mention the file name ... Here I dont know file name and path .. So it has search the entire directory and file and get the matches
  4. Karthik Rajashekaran

    Karthik Rajashekaran New Member

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Please help me out ..
  5. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,216
    Likes Received:
    407
    Trophy Points:
    83
    Code:
    grep -r "text string to search” /*
  6. Karthik Rajashekaran

    Karthik Rajashekaran New Member

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    no it didnt help me ...
  7. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,216
    Likes Received:
    407
    Trophy Points:
    83
  8. arochester

    arochester Well-Known Member

    Messages:
    807
    Likes Received:
    259
    Trophy Points:
    63
    ( @ryanvade I saw that too.)

    @Karthik Rajashekaran If you don't specify a Directory it will take a VERY, VERY long time to come up with an answer because it is searching every file on the Hard Drive. You need to wait forever - and then some more.
    ryanvade likes this.
  9. GrumpyOldMan

    GrumpyOldMan Active Member

    Messages:
    117
    Likes Received:
    41
    Trophy Points:
    28
    Also, if you start your grep at "/", you'll have some issues with permissions, unless you are root.

    You can save yourself some time and aggravation if you can skip binaries, also.

    so: sudo grep -rI "text string to search” /*
    ryanvade likes this.
  10. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,216
    Likes Received:
    407
    Trophy Points:
    83
    Not sure if case matters. But yeah, use sudo or run the command as root.
  11. GrumpyOldMan

    GrumpyOldMan Active Member

    Messages:
    117
    Likes Received:
    41
    Trophy Points:
    28
    well for a case-insensitive grep, use -i (-I means ignore binary, of course).
    ryanvade likes this.
  12. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,216
    Likes Received:
    407
    Trophy Points:
    83
    ;)
  13. Pantek Support

    Pantek Support New Member

    Messages:
    6
    Likes Received:
    2
    Trophy Points:
    1
    I prefer to use grep in conjunction with find for this kind of effort. Ie:

    find /path -exec grep "Search String Here" {} \; -ls

Share This Page