A common question asked by linux beginners, so here’s how to do it.

Let’s say you need to search your /etc/passwd file for the users apache AND ftp. You could do it this way:-

The manual way of greppin'

The manual way of greppin’

Or being a sysadmin, you’d want to do it smarter.

Use the pipe symbol and the ‘-E’ switch with grep will solve this problem.

$ grep -E 'apache|ftp' /etc/passwd
Grepping 2 words at a time

Grepping 2 words at a time

While your there, lets add some colour to your output. This will help you see where exactly the pattern matches.

$ grep -E --colour 'apache|ftp' /etc/passwd
Grepping with some colour

Grepping with some colour

A final tip that will come in handy, especially when you’re grepping long config files is the line number. Adding a ‘-n’ to your grep will display the line number for the matching term. Notice how adding the ‘–colour’ option makes the line numbers coloured in green, while the actual matched word is coloured in red.

$ grep -En --colour 'apache|ftp' /etc/passwd
Grepping, now with colours and line numbers

Grepping, now with colours and line numbers

Till another post, happy searching grepping.