Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated our privacy policy to let you know that we use cookies to personalise content and ads. We also use cookies to analyse our traffic and we share information about your use of our site and application with our advertising and analytics partners. By using this website or our application you agree to our use of cookies. Learn more about the way this website uses cookies or remove this message.

Bash history navigation and recursive grep searching

February 16, 2015 Difficulty: 10 / 50 Tweet
Car-Bashing-In-a-Wall

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' .
    

Good to know

  • You can also exclude files and folders from your search in order to only look inside the files you know could include your search string. To do this use --exclude='file' or --exclude='folder'.
  • Keep in mind that by default grep searches are case sensitive... so if you don't know the case of the string you are searching for, use the -i flag to do a case insensitive match

Pipe the output of various commands to grep

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'
    
comments powered by Disqus