cli-google: google search from terminal

Who wants to leave the comfort of the terminal and open a browser to search Google? 😉 We explored several ways to search google in our earlier articles (GoogleCL, Surfraw). How about some cool utilities that do not need much setup or learning? I revived one myself.

During my search I found the following script on commandlinefu:

function google { Q="$@"; GOOG_URL='https://www.google.de/search?tbs=li:1&q='; AGENT="Mozilla/4.0"; stream=$(curl -A "$AGENT" -skLm 10 "${GOOG_URL}${Q//\ /+}" | grep -oP '\/url\?q=.+?&amp' | sed 's|/url?q=||; s|&amp||'); echo -e "${stream//\%/\x}"; }

It works well in bash but shows only the URLs. Normally the URLs don’t make a lot of sense after the top 10 results.

I came across cli-google, exactly the thing that I was looking for. It was written to show the result title with URL and brief text snippet. If you use a URL-aware terminal like AltYo, you can click and open the links in your default browser. However, it turned out this python utility doesn’t work anymore. I am not a python programmer but I started modifying this promising utility and it’s in a presentable state now.

Features

  • Fetch first n results
  • Fetch results starting at result n
  • Support for different languages
  • Coloured output
  • Open first result in the web browser (remember I’m Feeling Lucky?)

My fixes and modifications

  • Use https
  • Handle google redirections (error 302)
  • Show full text context snippet of search results
  • Unicode in URL works
  • Time limit search
  • Support filetype specific search
  • Colour output by default, -C now disables it (toggled)
  • The first URL now correctly opens in browser with -j switch
  • Honour -j even if -n is not used and open the result in browser
  • Fixed character encoding problem in URL e.g. double quotes (%22) changed to %2522
  • Skip browser to show result in console for empty URL, e.g., first result of ‘define hello’

Future

  • Support country specific Google server
  • Anything else you can think of

google-cli

Initially I raised a pull request but I could see that the last change was made 7 years earlier. In addition, there is no GitHub activity from the author in past year. I have created an independent repo for the project with the name google-cli. Would love to push the changes back to original repo if the author contacts. I retained the original copyright information.

Installation

Run the following command to fetch the repository:

$ git clone https://github.com/jarun/google-cli

Usage

The following options are supported:

  -s N     start at the Nth result
  -n N     shows N results
  -l LANG  display in language LANG, such as fi for Finnish
  -C       disable color output
  -j       open the first result in a web browser
  -f MIME  search for specific file type
  -t dN    time limit search [e.g. d5: 5 days, w5: 5 weeks, m5: 5 months, y5: 5 years]

Contributions are welcome!

On GitHub: google-cli

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s