Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated the website and our policies to make sure your privacy rights and security are respected.
Click here to learn more about the way our website handles your data.

Remove this message.

Bash history navigation and recursive grep searching

Daniel Gheorghe Difficulty: 10 / 50 Tweet

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'