The power of grep
Intro to grep
The grep command allows you to search the output of a command or files for a given string or regular expression. By default, it will display only the lines that contain the search criteria.
Filtering command output
You can pipe the output of a command into grep. This essentially filters the output as it will only display the lines of output that contain your search criteria.
iptables -L INPUT -vn | grep 192.168.1.1
iptables -L INPUT -vn will display your INPUT chain in your firewall. If you had many rules, it could be a little tedious to find the rules that apply to the IP address 192.168.46.1. With this example usage of grep, you will only see the firewall rules that contain that IP address.
Anytime you run a command that has lots of output and you only need to see specific lines, grep is a huge time saver, especially if the output can't be sorted easily.
Searching files
Instead of opening a file and searching using the text editor, you can simply display the lines you are looking for.
grep 1G02Ff-0000m9-KX mainlog
The previous example will display each line in mainlog that has the message ID 1G02Ff-0000m9-KX. This is a useful way to look at each line in the log related to one email message.
You could also use this to quickly look at a configuration parameter (see next example), or to look at each time a certain IP address connected to you access_log.
grep PermitRootLogin /etc/ssh/sshd_config
This will display your setting for PermitRootLogin.




commands

Comments
There are no comments yet.