This brief guide will get you up and running using the incredibly helpful “grep” command for Linux and other *nix operating systems – even the latest versions of macOS.
For anyone familiar with the Linux or UNIX command line interface it’s quite likely that grep has crossed your path sometime. grep is a wonderful command line tool that helps you search for a word or words through files. It ships with almost all flavors of Linux by default. For those who aren’t familiar with grep here’s a quick primer.
# grep www /etc/httpd/conf/httpd.conf
#ServerName www.example.com:80
DocumentRoot “/var/www/html”
# e.g., www.apache.org (on) or 204.62.129.132 (off).
Alias /icons/ “/var/www/icons/”
In the example above we are using grep to search for the term www in the file /etc/httpd/conf/httpd.conf. 4 results were returned. It’s a pretty straightforward command. grep ships with a lot more functionality. Feel free to browse through the manpage for grep, which you can get to by running man grep.
Now we’ll look at a feature of grep that allows you to search through a document for two terms, using only one command. Let’s modify the command shown above to search for www, and also for the term server.
# grep ‘www\|server’ /etc/httpd/conf/httpd.conf
# SetHandler server-status
# Allow remote server configuration reports, with the URL of
# http://servername/server-info (requires that mod_info.c be loaded).
#ServerName www.example.com:80
DocumentRoot “/var/www/html”
# e.g., www.apache.org (on) or 204.62.129.132 (off).
Alias /icons/ “/var/www/icons/”
# SetHandler server-info
# enable the proxy server:
# (“Full” adds the server version; “Block” removes all outgoing Via: headers)
# use only name-based virtual hosts so the server doesn’t need to worry about
# server name.
So as you can see the output was much larger in the second command (12 lines were returned). Note how we modified the first command and changed the search term to ‘www\|server’. Remember to use the forward slash ( \ ) before the pipe, or else you will not get the same result. Alternately you can also use the tool egrep to run a similar search without using the forward slash. Try this:
# egrep ‘www|server’ /etc/httpd/conf/httpd.conf
# SetHandler server-status
# Allow remote server configuration reports, with the URL of
# http://servername/server-info (requires that mod_info.c be loaded).
#ServerName www.example.com:80
DocumentRoot “/var/www/html”
# e.g., www.apache.org (on) or 204.62.129.132 (off).
Alias /icons/ “/var/www/icons/”
# SetHandler server-info
# enable the proxy server:
# (“Full” adds the server version; “Block” removes all outgoing Via: headers)
# use only name-based virtual hosts so the server doesn’t need to worry about
# server name.
You can also add another pipe and search for another term also. You command would look like # egrep ‘www|server|apache’ /etc/httpd/conf/httpd.conf.
When you combined the grep command with the find command there isn’t much you won’t be able to locate on your computer :)
this a a good tip for linux user.
‘/’ = slash
‘\’ = backslash
author = troll