Search code examples
macosfileterminalgreposx-mountain-lion

Grep file search returns: Is a directory in OSX Mountain Lion


I am trying to search for a specific file using the example How do I use grep command to search a file?

Trying grep searchingforfile /Users/myuser/path/to/dir returns:

grep: /Users/myuser/path/to/dir: Is a directory

Please let me know what is the best way to search for a file then in this case.


Solution

  • If you want to find files which begin with f in the current directory:

    ls f*
    

    If you want to find files ending with "jpg" in the current directory and all directories below:

    find . -type f -name "*.jpg" -print
    

    If you want filenames containing pattern in current directory and below:

    find . -type f -name "pattern" -print
    

    but put an asterisk either side of the pattern (I can't do it on here)

    Or, if your pattern is more complicated

    find . -type f -print | grep "someFunkyPattern"
    

    If you want to search for pattern in a file called fred:

    grep pattern fred
    

    If you want to search for bill or steve in files called friends1 and friends2 and you don't care about case (capital letters) use the -i option:

    egrep -i "steve|fred" friends1 friends2
    

    If you want to search for Mike in all files whose names start with "friend*":

    grep Mike friend*