Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: grep "XXXX" `find . -name "*.[ch]"`

  1. #1
    Silent Key Member 5-25-2015 W1GUH's Avatar
    Join Date
    Aug 2008
    Location
    NYC
    Posts
    10,471

    grep "XXXX" `find . -name "*.[ch]"`

    That's what I use on Unix to recursively search for a pattern (XXXX) in .c and .h files. The single quotes are "tics" -- the character below the tilde in the upper left corner of the keyboard.

    I appears that that doesn't work in Red Hat Linux, maybe because the "tic" means something else in linux? The part of the command within the tics works on linux, recursively finding c files, but it does something different when I enclose in it tics.

    Anyone know how to search recursively for patterns in files on linux? Tnx.
    If it's a war on drugs, then free the POW's.

  2. #2
    Orca Whisperer
    Join Date
    Oct 2009
    Location
    Buffalo, NY
    Posts
    22,593

    Re: grep "XXXX" `find . -name "*.[ch]"`

    the ` character in Bash means "explode the following command, so it should work. BUT, I would just suggest this:

    find ./ -name "*.[ch]" -exec grep {} "foo" ;\
    Big Giant Meteor 2020 - We need to make Earth Great Again

    http://www.coreyreichle.com

  3. #3
    Silent Key Member 5-25-2015 W1GUH's Avatar
    Join Date
    Aug 2008
    Location
    NYC
    Posts
    10,471

    Re: grep "XXXX" `find . -name "*.[ch]"`

    That one didn't work with Red Hat, but

    fine ./ -name | grep "foo"

    does.

    Well, DUH, of course it will.

    Awwwww....where's the Homer Simpson DOH! when you really neet it?
    If it's a war on drugs, then free the POW's.

  4. #4
    Orca Whisperer
    Join Date
    Oct 2009
    Location
    Buffalo, NY
    Posts
    22,593

    Re: grep "XXXX" `find . -name "*.[ch]"`

    That'll do it too. More than one way to pipe a command, I always say
    Big Giant Meteor 2020 - We need to make Earth Great Again

    http://www.coreyreichle.com

  5. #5
    Silent Key Member 5-25-2015 W1GUH's Avatar
    Join Date
    Aug 2008
    Location
    NYC
    Posts
    10,471

    Re: grep "XXXX" `find . -name "*.[ch]"`

    Actually, no...that's all worng. The pipe's in the wrong direction. Back to square one.

    Thanks, anyway.

    I'll take a close look at that "exec" command....seems a step in the right direction.
    If it's a war on drugs, then free the POW's.

  6. #6
    Orca Whisperer
    Join Date
    Oct 2009
    Location
    Buffalo, NY
    Posts
    22,593

    Re: grep "XXXX" `find . -name "*.[ch]"`

    This should do it:
    find ./ -name "*.[ch]" -exec grep "foo" {} \;

    Had the args for grep backwards.
    Big Giant Meteor 2020 - We need to make Earth Great Again

    http://www.coreyreichle.com

  7. #7
    Snarky Dick ka8ncr's Avatar
    Join Date
    May 2008
    Posts
    2,486

    Re: grep "XXXX" `find . -name "*.[ch]"`

    Quote Originally Posted by W1GUH
    That one didn't work with Red Hat, but

    fine ./ -name | grep "foo"

    does.

    Well, DUH, of course it will.

    Awwwww....where's the Homer Simpson DOH! when you really neet it?
    Doesn't find accept wildcards?

  8. #8
    Silent Key Member 5-25-2015 W1GUH's Avatar
    Join Date
    Aug 2008
    Location
    NYC
    Posts
    10,471

    Re: grep "XXXX" `find . -name "*.[ch]"`

    Quote Originally Posted by KC2UGV
    This should do it:
    find ./ -name "*.[ch]" -exec grep "foo" {} \;

    Had the args for grep backwards.
    That does it, thanks a bunch. Sorry it took so long to check it out...I've been away from a linux box til tonight. Well, that and I was too busy (lazy?) to check it over the weekend. Plus, I wanted to test my memory of the Unix command on a Unix box.

    Leads me to more general question....

    Can anyone recommend a good linux primer that'll decipher this stuff? I tried web searches to find out what the "tic" does in both OS's (I get different results on Unix and linux), and can't find anything succinct and/or written for a novice. (Someday my "speed" will hit 13, and then 20! )

    I learned the original Unix command from a guru in the usual way...memorized it.
    If it's a war on drugs, then free the POW's.

  9. #9
    Orca Whisperer
    Join Date
    Oct 2009
    Location
    Buffalo, NY
    Posts
    22,593

    Re: grep "XXXX" `find . -name "*.[ch]"`

    http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

    :)

    And this is almost all the documentation you'll ever need:
    http://tldp.org/
    Big Giant Meteor 2020 - We need to make Earth Great Again

    http://www.coreyreichle.com

  10. #10
    Silent Key Member 5-25-2015 W1GUH's Avatar
    Join Date
    Aug 2008
    Location
    NYC
    Posts
    10,471

    Re: grep "XXXX" `find . -name "*.[ch]"`

    Thanks for the links...they look like good quick look-up pages.

    But they didn't help my current quest....finding out about that "back tick" (finally learned it's proper name) operator. That's been a very tough search, but I finally learned about it in Barnes & Nobel tonight....sitting on the floor, looking through all the linux books on the shelf.

    Went through about six of them until I FINALLY found out what it means....

    "Use the output of the enclosed command as input to the previous command." Called command substitution.
    Gripe #1 -- never did find that on the web. Sure, if I would have known about "command substitution", I could have searched for that.

    I also read that it recommended to use $() instead. Book didn't really say why, just gave the suggestion. Well, OK...

    So I ran home to try out my new found knowledge and, well, my Ubuntu linux actually likes:

    grep XXXX `find . -name "*.[ch]"`

    Works just fine. So does grep XXXX $(find . -name *.c)

    But I couldn't make it work on a Red Hat linux box. ???

    Maybe the most important thing I learned while using Barnes & Nobel as a library is what I really want to learn is Command Line Scripting. It looked as if that's where all the stuff about these special characters & stuff is described. Uh-oh....keep this quiet...word of this gets around I might get tasked with dealing with make files. Horrors!

    One thing I couldn't find anything about (looking in the indices of a bunch of books) is that "exec". I saw it mentioned briefly once....but the text didn't say anything about what it is or how to use it.

    Just reportin' one guy's struggle to get into linu/unix.

    I'm getting a very strong impression that most (Almost all? All?) unix/linux types primarily learn through osmosis -- being in a SW lab with gurus and wizards who share their knowledge (and who probably learned the same way!)

    This is contrast to, say the c family of languages where primers are available to lead you by the hand. The best I found are C Primer Plus and C++ Primer Plus. Not only are that very well written and easy to learn from, the author has a GREAT sense of humor!
    If it's a war on drugs, then free the POW's.

Posting Permissions

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