Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Search for Files in Command-line Using grep

The article 14 Practical examples of the grep command has some good examples on how to use the grep command.

  1. The Perl style (option -P) regular expression is more powerful than the basic (default) and extended (option -E) regular expression. It is suggested that you use the perl style as much as possible. However, be careful about unimplemented features.

  2. The option -n/--line-number prefixes each line of output with the 1-based line number within its input file. And the option -o/--only-matching prints only the matched (non-empty) parts of a matching line, with each such part on a separate output line. Both of those options are very useful.

  3. Search in current directory recursively for files containing the text "video". Symbolic links are followed.

    grep -iR 'video' .
    
  4. Search in current directory recursively for files containing the text "video". Symbolic links are NOT followed.

    grep -ir 'video' .
    

Comments