Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I'm trying to find some text in all file. I changed the cd to / and I'm using grep -R "string" * I was wondering if I did something ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    4

    Searching text in a file


    Hi,

    I'm trying to find some text in all file. I changed the cd to / and I'm using grep -R "string" *

    I was wondering if I did something wrong because the first message I get is that there is dev/log: No such device or address and then nothing but a blinking cursor.

    How do I know if the search is actually running and how long does this process take?

    Thank you.

  2. #2
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    You should never grep everything from /, since you'll be attempting to grep special devices and all sorts of things.

    You can still if it's still running by using "ps" to grep for the instance of grep, e.g.,:
    Code:
    ps aux | grep "grep"
    The process of grepping every file on an entire filesystem for an operating system where everything is a file will take a very very long time - even then it most likely will fail out due to trying to read files that don't have read access, grepping against special files, etc.

    At the very least you should be using "find" to narrow the choices down to files that are actually files (and not files that are block devices, et al), and generally limit yourself to a particular extension just to make things easier - since there's not much use "grep"ing a jpeg or binary.

    e.g.,:
    Code:
    find / -type f -exec sh -c "grep 'phrase' {} && echo {}" \;
    Pending your overall application, you may wish to also look into the "strings" command.

  3. #3
    Just Joined!
    Join Date
    Jun 2012
    Posts
    4
    Helo Dayid,

    Thank you for your suggestion. Upon further research, I understand most of the code. But may you explain why there is a " after the echo {}. And should the first {} be quoted? '{}'?

    Thank you.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    Quote Originally Posted by emirp81 View Post
    Helo Dayid,

    Thank you for your suggestion. Upon further research, I understand most of the code. But may you explain why there is a " after the echo {}. And should the first {} be quoted? '{}'?

    Thank you.
    To break it down:
    Code:
    find / -type f -exec sh -c "grep 'phrase' {} && echo {}" \;
    find / This uses the command "find" to search, starting the path at / (root)
    -type f This tells find to only return files of the type "file" (rather than directories, block devices, etc).
    -exec Tells find to - on the files that match the preceeding qualifiers (in this case, "is in / and is filetype file") execute a command. find only expects this to be one thing. To get it to do two things - I use sh and put the two commands within quotation marks - hence:
    sh -c "grep 'phrase' {} && echo {}"

    This, the " after the echo {} is to close the opening " from after sh -c.

    Neither of the {} should need to be quoted, but it should not matter if they are.

Posting Permissions

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