Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

Wednesday, July 29, 2009

Easy command line search with grep and find

I've been using grep and find for searches on command line.

Examples:

# Search recursively for 'User' in file contents under current directory
grep -r 'User' .

# Search recursively for 'User' in file contents under current directory and display 2 lines before and after
grep -r -2 'User' .

# Search recursively for 'User' in file contents under current directory and display 2 lines before and after with color
grep -r -2 --color 'User' .

# Search for filenames that contains user.
find . -name 'user.*'


Combine them together:

grep -r -2 --color 'User' $(find . -name 'user.*')

Wednesday, July 22, 2009

Search for codes in your files quickly with grep

Sometimes when I do unit tests or cucumber tests I'll encounter an error that I've roughly aware of the offending code but not sure which files it resides in. That's where grep comes in useful.

Search for occurrences of ".name" in view files recursively


grep '.name' -r app/views


Get occurrences with its surrounding 2 lines before and after


grep '.name' -r -2 app/views