Google Search from Linux Terminal

59,447

Solution 1

google-cli is supposed to do just that (it's the revived version of cli-google).

Solution 2

Here's a simple bash function that lets you type

google foo bar

and which will then open your default browser to display the Google results page for those search terms:

google() {
    search=""
    echo "Googling: $@"
    for term in $@; do
        search="$search%20$term"
    done
    xdg-open "http://www.google.com/search?q=$search"
}

Simply paste that in your terminal to give it a try.

For Windows or Mac OS X, substitute the last line with one of the following (assuming you are using Cygwin or similar on Windows):

Windows

start "http://www.google.com/search?q=$search"

Mac OS X

open "http://www.google.com/search?q=$search"

Solution 3

#!/bin/bash

if [[ $(echo $*) ]]; then

    searchterm="$*"

else

    read -p "Enter your search term: " searchterm

fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

lynx -dump http://www.google.com/search?q=$searchterm | less

Copy and paste this script into ~/bin, name it "goose" or something (GOOgle SEarch). Chmod it +x

Usage is:

goose searchterm

Clearly, you have to have Lynx installed.

Solution 4

If you want to search from the command line and jump to a user-definable browser for results, another solution is use surfraw.

   Surfraw  provides  a fast unix command line interface to a variety of
   popular WWW search engines and other artifacts of power.  It reclaims
   google,  altavista, dejanews, freshmeat, research index, slashdot and
   many others from the false‐prophet,  pox‐infested  heathen  lands  of
   html‐forms,  placing  these  wonders  where they belong, deep in unix
   heartland, as god loving extensions to the shell.

It's available pre-packaged in some Linux distributions (Debian, Ubuntu, unknown others), from source at debian.org, and the latest development code and releases are available from the official git repository (now living on GitLab).

Installation instructions from tarball or deb file are found on the Wiki.

Trivia note : Surfraw was originally written by Julian Assange. (Acronym/backronym for 'SURFRAW' is Shell Users' Revolutionary Front Rage Against the World Wide Web.)

To do a Google search from the command line:
sr google archibald tuttle

In addition to plain old Google, there are a lot of other built in search types.

To search for an RFC dealing with S/MIME:
sr rfc s/mime

Translate a word:
sr translate logiciel

Find torrents:
sr piratebay free music

(These keyword search types continue to be updated.)

More advanced usage :

     $ surfraw google -results=100 RMS, GNU, which is sinner, which is sin?
     $ sr wikipedia surfraw
     $ sr austlii -method=phrase dog like
     $ /usr/lib/surfraw/rhyme -method=perfect Julian

Surfraw is configurable. You can set it up with some defaults, either per-user in $HOME/.surfraw.conf or system-wide in /etc/surfraw.conf :

SURFRAW_graphical_browser="/usr/bin/links2 -g"
SURFRAW_text_browser="/usr/bin/elinks"
SURFRAW_graphical=yes

Here I've set it to use links2 and elinks, but you can use Firefox, Chrome, or any others you prefer.

(Nb. links2 -g is a strange terminal-embedded graphical-mode browser. It's fast but hates modernity.)

Solution 5

With wget, example:

wget -U 'Firefox/3.0.15' http://www.google.com/search?q=wget+google+query+to+file -O file.html

Source

Share:
59,447

Related videos on Youtube

Dean Rather
Author by

Dean Rather

"Professionally" I do PHP, but in my spare time I tinker in C++/Java etc. Playing with Node a little lately.

Updated on September 17, 2022

Comments

  • Dean Rather
    Dean Rather over 1 year

    I saw a Google console app someone wrote a while back, but it was actually a website emulating a console.

    What I'm after is a shortcut or Linux terminal app which I can use to quickly search Google.

    Ideally, it will show the top 10 search results with numbers next to them, and pressing the number will open the site in a browser.

    Having the Google results open in a browser is fine too.

    Does anyone have a solution?

    • Admin
      Admin over 14 years
      The website emulating a console - are you talking of goosh? goosh.org
    • akira
      akira over 14 years
      you will end up open the browser anyway, whats the point?
    • Landon Kuhn
      Landon Kuhn over 11 years
      the point is to issue google searches from the terminal where we spend most of our time and are most comfortable. it might also be neat to have your recent google searches all visible in a command line history.
    • neverMind9
      neverMind9 over 5 years
      Closed as off-topic? Can it not just be migrated?
  • Suhaib
    Suhaib over 11 years
    Beagle link is not working !!
  • Daniel Jacobson
    Daniel Jacobson almost 9 years
    i turned this into a shell script for mac by changing xdg-open to open, removing the function part and then adding script to bin. Thanks!
  • albal
    albal over 8 years
    Please bring the relevant content of the link into the answer.
  • Thomas8
    Thomas8 about 8 years
    simply gold!!!!
  • Jeff7566
    Jeff7566 about 5 years
    If you are using oh-my-zsh, try the web-search plugin --> github.com/robbyrussell/oh-my-zsh/tree/master/plugins/…