Wilson Mar bio photo

Wilson Mar

Hello. Join me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

Find, grep, sed stuff on your hard drives using regular expressions


Here are my notes on finding (and replacing) stuff on a Mac.

  1. cd to the folder you want searched. For example:

    cd ~/gits/wilsonmar/wilsonmar.github.io/_posts

    PROTIP: It’s important that you drill down into the lowest folder you can. If you are too high in the folder hierarchy, you’ll encounter messages like these about protected folders and files not processed:

    find: ./.DocumentRevisions-V100: Permission denied
    find: ./.fseventsd: Permission denied
    find: ./.MobileBackups: Permission denied
    find: ./.Spotlight-V100: Permission denied
    find: ./.Trashes: Permission denied
    find: ./dev/fd/3: Not a directory
    find: ./dev/fd/4: Not a directory
    find: fts_read: Permission denied

    Grep Utilities

  2. See the version of the Grep utility installed:

    grep --version


    grep (BSD grep) 2.5.1-FreeBSD

    PROTIP: The Mac is a combination of BSD and Linux goodness.

  3. Display just the filenames containing the word “foo”:

    grep -r -l "foo" .

    From https://www.cyberciti.biz/faq/howto-recursively-search-all-files-for-words/

    Use find command to show text within files

  4. Find text “foo” within files by diving recursively into folders :

    grep -ri "foo" .

    TIP: Remove the i to not ignore case distinctions.

  5. To find text “Exception” within files of “.md” type at the current folder path:

    find . –name "*.md" –print | xargs grep "Exception"

    Find “foo” within files by diving recursively into folders :

    grep -ri "foo" .
    • Remove the i to not ignore case distinctions.

  6. Use -print0 option to find filenames that contain spaces or other metacharacters:

    find /path/to/dir -type f -print0 | xargs -0 grep -l "foo"

    Find file names

    See Mommy, I found it! — 15 Practical Linux Find Command Examples

  7. Find files using file-name ( case in-sensitve find)

    find -iname "MyCProgram.c"
  8. Execute commands on files found by the find command:

    find -iname "MyCProgram.c" -exec md5sum {} \;
  9. Find all empty files in home directory:

    find ~ -empty

    WARNING: There is a lot of these.

  10. Find the word “server” with case -insensitive in a file:

    grep -i Server /etc/ntp.conf

    The “-i” for insensitive capitalization.

    The response contains the word “server” searched:

    server time.apple.com.
  11. Find lines that don’t (-v to reverse search) begin with # (specified by a ^) or blank lines (specified by a ^) to the end of line $:

    grep -ve'^#' -ve'^$'/etc/ntp.conf

    The response:

    server time.apple.com.

Regular Expressions

We’ll use the spelling dictionary of English words that comes with Linux.

  1. Search for words ending with “fine”:

    grep 'fine$' /usr/share/dict/words

    $ (Shift+4) specifies search from the end of string.

    Responses include “refine”.

  2. Search for words beginng with “fine”:

    grep '^fine' /usr/share/dict/words

    ^ (Shift+6) specifies search from the beginning of string.

    Responses include “refine”.

  3. Search for “fine” anywhere within the line:

    grep 'fine' /usr/share/dict/words
  4. Search for lines containing “fine” anywhere within words:

    grep 'fine' /usr/share/dict/words


  5. Search for a space before “system” in lines, such as either “system” or “file system”:

    grep '\ssystem' /etc/ntp.conf
  6. Search for any word boundary after “server” in lines, such as “servers” or “serverless”:

    grep 'server\b' /etc/ntp.conf

    PROTIP: The response “server time.apple.com” is the server used for Network Time Protocol used to update your machine’s clock.

  7. Return lines that do not start with # for comment:

    grep -v '^\s*#' /etc/hosts
  8. Search for specific characters C or c:

    grep '[Cc]'


  9. Search for “color” or “colour” in any line, using a combination, including a ? to specify the previous character u as an optional character:

    grep '\b[Cc]olou?r\b' /usr/share/dict/words

    u+ matches one or more occurrences.

    u* matches zero or more times.

    u{4} matches exactly four occurrences.

    enhanced grep

  10. Return words with five consequtive vowel characters:

    grep -E '[aeiou]{5}' /usr/share/dict/words

    BTW, “euouae” (pronounced your-you-ee”) consists only of vowels.

    Regex ranges

  11. Search for characters, upper and lower case ranges from A to Z, plus underscores:

    grep '[A-Za-z_]' ???
  12. Search for just numbers range 1 through 9:

    grep '[0-9]' ???


The Linux sed utility replaces contents within a file.

To delete empty lines and comment lines:

   sed -i.bak '/^s*#/d;/^$/d' somefile

The -i generates a backup.

The semicolon separates multiple specifications.



Hidden files

If you are searching for hidden files:

How to Search the Content of Your Files on Windows

Using Utility programs



Using IDE

Within Eclipse, press ctrl+F for the Find dialog.

Press ctrl+H for Find & replace.

Search are multi-line by default in Eclipse when you are using regex:

(\@Length\(max = L_255)\)([\r\n\s]+private)