unix: odd behavior w/ 'find' command

Discussion in 'Mac Help/Tips' started by zimv20, Dec 19, 2002.

  1. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #1
    the other day i tried running a certain construct of the find command, via command line, but it didn't work:

    Code:
    % find . -type f -exec grep foo {}\; -print
    
    it just sat there, never returned (even on my very small test case). i've used this construct for years, so i know that it works (on some or most unix implementations, anyway).

    i was able to find a workaround (for those interested):

    Code:
    % find . -type f -print | xargs grep foo
    
    (sadly, the workaround doesn't work all the time, as xargs was choking on something coming through the pipe and had to restrict its running to a subdir)

    any unix heads know what's up w/ the first find? has 'find' changed since i was doing all my unix coding?

    i'm running 10.2.2, if that matters.
     
  2. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #2
    May I ask what the purpose of the command is? Maybe you don't need the -print option if you use other grep switches, e.g.,

    % find . -type f -exec grep -l foo {} \;
     
  3. zimv20 thread starter macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #3
    i believe the print lets me know in which file the grep hit is in.

    does this work on your machine?

    oh -- the purpose is to search a subdir for files containing the string "foo". well, okay, i was searching for "smtp." but you get the idea.
     
  4. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #4
    Yes, the find command that I suggested works for me - to display the names of files in the current directory or any subdirectory, recursively, that contains the string "foo". The -l switch of the grep command means "display filename instead of displaying the matching line".

    Please note that a space before the \; is required, as in the example I gave.
     
  5. zimv20 thread starter macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #5
    DOH!

    that's it. thank you. gee, i actually knew unix once upon a time...
     

Share This Page