Find the answer to your Linux question:
Results 1 to 4 of 4
I want to search files in linux containing special text, for example: I want to find any files in /etc containing text "10.0.0.3". I just using command line (not the ...
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    3

    Search files containing text

    I want to search files in linux containing special text, for example:
    I want to find any files in /etc containing text "10.0.0.3".
    I just using command line (not the GUI interface),
    how to solve this ?

    thanks

  2. #2
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    Code:
    find /etc -type f -iname '*' -exec grep -Hni "10.0.0.3" {} \;
    should do the trick.

    You could do like i did and put this in a bash script. Create a new file and in it put:
    Code:
    #!/bin/bash
    find "$1" -type f -iname '*' -exec grep -Hni "$2" {} \;
    Save it, make it executable, then put it in /bin or somewhere in $PATH. Then when you run it use something like:

    Code:
    findin /etc 10.0.0.3
    assuming you called it "findin" and it's in your $PATH somewhere, and you want to find instances of that IP in /etc.

    Also being that it uses find, it is also recursive. So it will search all subdirectories in /etc. Also with the grep -Hni it will return the filename AND ilne number it found the text.

    The format of the output is:

    Code:
    <filename>:<line number within file>:<text>
    <text> is the whole text from that line of that file.

    HTH
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  3. #3
    Linux Engineer Zelmo's Avatar
    Join Date
    Jan 2006
    Location
    Riverton, UT, USA
    Posts
    1,001
    You can also do it just using grep.
    Code:
    grep "10.0.0.3" /etc/*
    will only check the files in /etc/itself;
    Code:
    grep -R "10.0.0.3" /etc/*
    will check the files in /etc and all directories under /etc.
    Stand up and be counted as a Linux user!

  4. #4
    Just Joined!
    Join Date
    Mar 2007
    Posts
    3

    Thanks

    Great, thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...