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:
    129
    Likes Received:
    70
    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,395
    Likes Received:
    454
    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,395
    Likes Received:
    454
    Trophy Points:
    83
  8. arochester

    arochester Super Moderator Staff Member

    Messages:
    908
    Likes Received:
    300
    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:
    158
    Likes Received:
    50
    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,395
    Likes Received:
    454
    Trophy Points:
    83
    Not sure if case matters. But yeah, use sudo or run the command as root.
  11. GrumpyOldMan

    GrumpyOldMan Active Member

    Messages:
    158
    Likes Received:
    50
    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,395
    Likes Received:
    454
    Trophy Points:
    83
    ;)
  13. Pantek Support

    Pantek Support New Member

    Messages:
    6
    Likes Received:
    3
    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
    icehouse15 likes this.
  14. Jefff

    Jefff New Member

    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    1
    Use this for color coded results back:
    Code:
    grep -iIHrn --color=always yourtexthere . | less -r
    You can also make a function for it in your .bashrc file:
    Code:
    # Searches for text in all files in the current folder
    ftext ()
    {
       # -i case-insensitive
       # -I ignore binary files
       # -H causes filename to be printed
       # -r recursive search
       # -n causes line number to be printed
       # optional: -F treat search term as a literal, not a regular expression
       # optional: -l only print filenames and not the matching lines ex. grep -irl "$1" *
       grep -iIHrn --color=always "$1" . | less -r
    }

Share This Page