Walker News

Useful Grep Command Option Switches For Most Cases

Linux users who work on command prompt should know what this grep command is. In short, we use grep command to find files or lines in files that contain the search pattern.

While the man grep provides a complete reference of the command syntax, I memorized these 6 useful option switches and use them in most of the cases:
-H to print file name and each lines that contain the search pattern
-I to ignore binary file (e.g. *.tar, *.zip, program files, etc)
-i to perform case-insensitive search
-r to search files under each directory, recursively
-l to print file name only without the line that contains the search pattern and stop further search in the file on first match.
-s to suppress error messages about nonexistent or unreadable files.

For example, to find out all configuration files in /etc directory, including sub-directories, that containing the keyword “walker” (the search pattern) – with and without using -l option switch:
[root ~]# grep -HIirls walker /etc

[root ~]# grep -HIirs walker /etc
/etc/hosts: walker walker.foo.com

The other two options switches that I use in some occasions:
-o to print the match only instead of whole line that contains the search pattern.
--color=auto to highlight the match (colorize text)

For example:
[root ~]# grep -HIirs -o walker /etc

[root ~]# grep -HIirs --color=auto walker /etc
/etc/hosts: walker walker.foo.com

Custom Search

2017  •  Privacy Policy