In today's tutorial I will show you how to search through your bash history in order to avoid re-typing long commands every time. You will also learn about a few use cases for the
grep CLI utility.
Open your terminal and press
Ctrl + R this will open up the "(reverse-i-search)". Once you've done that start typing a string that you remember from one of your previously executed commands. Bash will display the most recent command that matches your search. If you need to search further back in your history, just press
Ctrl + R again until you find your command.
Grep searching is IMHO the coolest tool in Linux. In order to search recursively inside every file in a folder and all of its sub-folders use the command below you need to add the
-R flag to your grep command. Another thing that I find useful is to use the
--color=always flag - which will highlight the matched string in your search results.
grep -R --color=always --exclude='some file' 'some string' .
grepsearches are case sensitive... so if you don't know the case of the string you are searching for, use the
-iflag to do a case insensitive match
In Linux, you can use
grep to search inside output from another command. A real life usage of this would be using
tail to track an access log and piping the output to grep which in turn filters the output by an arbitrary filter. Here's a command to monitor requests done by a certain bot (we all know it :)) on your website.
tail -f access.log | grep 'Googlebot'