Grep

From LQWiki
Jump to: navigation, search

Grep (global regular expression printer) is a powerful utility that searches files and prints lines which contain a given pattern. Supports regexp.

It is one of the older *nix commands, and is also one of the most frequently used.

Examples

$ grep Something *
Search all files in the current directory for the word 'Something'.
$ grep -i something example.txt
Search example.txt for all occurances of the word 'something', case-insensitive.
$ grep -r something .
search all files both in the current directory and all subdirectories for the word 'something'.
$ grep -E "video|audio" /etc/group
search in /etc/group for every line containing video OR audio (this command uses a regular expression)
$ find -name "exam*" | xargs grep something
search all files that start with 'exam' (ie: example.txt, exam4.txt) both in the current directory and all subdirectories for the word 'something'.
$ find -name "exam*" -exec grep buttermilk {} \; | xargs grep something
search all files that start with 'exam' (ie: example.txt, exam4.txt) both in the current directory and all subdirectories for the word 'buttermilk' in files that also contain the word 'something'.

See also