Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Search Google from the command line UNIX
Often I'm working at the command line, and want to do a quick Google search on a topic. So I wrote a tiny shell script to make this easy, saving several clicks!

To use the script, simply type google your search terms while you're in Terminal. For example, typing google mac os x turns up pages about our favorite OS (this site is #3!). If you're on your local machine, the search results open in your favorite browser. If you're logged in via SSH, the search results open in the links command-line browser, which you can download here.

You can download the script from me, or copy and paste the following:
#!/bin/sh
query=""
for this_query_term in $@
do
query="${query}${this_query_term}+"
done
url="http://www.google.com/search?q=${query}"

remote_addr=`who am i | awk -F\( '{print $2}' | sed 's/)//'`

if [ -z "$remote_addr" ]; then
open "$url"
else
links "$url"
fi
Just copy-and-paste the script into a text file (call it 'google' to match the examples listed here), and make it executable (chmod 755 google). You'll want to place it somewhere that is in your path, such as /usr/bin, or better, put it in ~/bin and add that to your path.
    •    
  • Currently 2.67 / 5
  You rated: 1 / 5 (6 votes cast)
 
[15,529 views]  

Search Google from the command line | 9 comments | Create New Account
Click here to return to the 'Search Google from the command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Requries OS X 10.2 ?
Authored by: leejoramo on Oct 09, '02 10:50:51AM
I am not getting this to work on my Mac OS X 10.1.5 system. The following error occurs:


2002-10-09 08:42:27.375 open[476] No such file: /Users/ljoramo/bin/http:/www.google.com/search?q=mac+os+x+


I suspect that the version of the open command on my system does not support passing URL's, this feature was probably added in Jaguar. I did a quick search but could not confirm this. The new hard drive, AirPort card and Jaguar are sitting on my desk waiting to upgrade my Titanium. Maybe tonight...

Lee Joramo

[ Reply to This | # ]
or make an alias
Authored by: pink on Oct 09, '02 11:53:19AM

you can also make an alias that does the same:

alias google 'open http://www.google.com/search\?q=\!*'

(there is supposed to be a backslash each before the ? and the !
somehow this is edited out....)
just have to remember to use the right nomenclature, e.g.
google me+you+all

cheers, pink



[ Reply to This | # ]
Or install surfraw....
Authored by: sao on Oct 09, '02 04:09:28PM

I installed surfraw-1.0.7-1 with Fink to satisfy all my google and other searching needs from terminal.

http://surfraw.sourceforge.net/

"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, babelfish, 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."

202 Sao @ ~ $ surfraw
W----------------------------- Activate Surfraw defined web-browser
altavista-------------------- Search the web using Altavista (www.altavista.com)
amazon-------------------- Search the amazon.com bookstore
appwatch----------------- Search entries on Appwatch (www.appwatch.com)
ask------------------------- Question the web using Ask Jeeves (www.ask.com)
austlii---------------------- Search Australian Law docs (www.austlii.edu.au)
britannica----------------- Look up word in Encyclopedia Britannica (www.britannica.com)
cite ------------------------- Search computer science papers (www.researchindex.com)
cnn-------------------------- Search on CNN (cnn.com)
currency ------------------ Abuse the Universal Currency Converter (www.xe.net/ucc)
debbugs------------------ Search the debian BTS (bugs.debian.org)
debcontents------------- Search contents of debian packages(packages.debian.org)
debpackages----------- Search debian packages (packages.debian.org)
deja------------------------ Search usenet using Deja News (www.dejanews.com)
excite---------------------- Search on Excite (www.excite.com)
fast------------------------- Search the web using FAST Search (www.alltheweb.com)
filesearching ----------- Search files on filesearching (www.filesearching.com)
freebsd------------------- Search FreeBSD related information (www.freebsd.org)
freshmeat--------------- Search Freshmeat (www.freshmeat.net)
ftpfind--------------------- Search files on ftpfind (www.ftpfind.com)
ftpsearch---------------- Search files on ftpsearch (ftpsearch.lycos.com)
google------------------- Search the web using Google (www.google.com)
jake----------------------- Search journal/databases in jake (jake.med.yale.edu)
netbsd------------------- Search NetBSD related information (www.netbsd.org)
openbsd----------------- Search OpenBSD related information (www.openbsd.org)
pubmed ----------------- Search medical/molbio databases (www.ncbi.nlm.nih.gov)
raging-------------------- Search the web using Raging (www.raging.com)
rhyme-------------------- Search for rhymes et al using Lycos Rhyme (rhyme.lycos.com)
scaleplus--------------- Search Australian Law using ScalePlus (scaleplus.law.gov.au)
slashdot---------------- Search stories on Slashdot (www.slashdot.org)
slinuxdoc--------------- Search entries in LDP (www.linuxdoc.org)
sourceforge----------- Search SourceForge (www.sourceforge.net)
stockquote------------- Get a single stock quote (multiple providers)
sundocs---------------- Search docs on Sun.com (docs.sun.com)
sunsolve--------------- Search public docs on Sunsolve (sunsolve.sun.com)
swhois------------------ Search Whois (www.geektools.com)
thesaurus-------------- Look up word in Merriam-Webster's Thesaurus (www.m-w.com)
translate---------------- Translate human languages (various providers)
webster----------------- Look up word in Merriam-Webster's Dictionary (www.m-w.com)
wetandwild------------ Real time weather information (many sources)
woffle------------------- Search the web using Woffle (localhost:8080)
xxx----------------------- Superior XXX, hot and ready to press (arXiv.org)
yahoo------------------- Search Yahoo categories (www.yahoo.com)

I use surfraw in combination with 'launch' (a replacement for "open" also installed with Fink) in order to open the urls in mozilla besides links or w3m and created aliases like the following:

alias go 'google -browser='launch''
alias bri 'britannica -browser='launch''
alias wet 'wetandwild singapore -browser='launch''


Cheers...



[ Reply to This | # ]
Or install surfraw....
Authored by: imacusr on Oct 09, '02 08:41:58PM

OMG, that is too cool. Downloading now....



[ Reply to This | # ]
the lizzard alternative
Authored by: metiure on Oct 09, '02 04:11:18PM

I don\'t know about the terminal. Looks like a great hint. But with real browsers, like
Mozilla, no script is needed at all, and you can search Google directly from the URL line.

Just add:

user_pref(\"keyword.URL\",\"http://www.google.com/search?q=\");

to your user.js prefs file.

Type your search, and Mozilla kindly takes you to Google, with plenty of great answers waiting for you!



[ Reply to This | # ]
the lizzard alternative
Authored by: DavidRavenMoon on Oct 09, '02 07:34:40PM

You don't even have to add that. It's already built in. Just set Google as the default search engine (Prefs/Navigator/Internet Search), then type your query in the address field and press the down arrow to highlight "Search Google for "(your text string)"" and hit Enter!



[ Reply to This | # ]
GREAT Hint
Authored by: rocteur on Oct 12, '02 12:39:37PM

This is really GREAT.. Thanks!!!



[ Reply to This | # ]
google in perl
Authored by: svanstrom on Oct 13, '02 08:35:46AM
Well... being a fan of perl I could of course not use that version, so I wrote my own. =)
I didn't compare the two, but I think his version couldn't handle situations where you wanted to use a search "word" containing a space; but mine does.
There shouldn't be any bugs, but if there are, or you've got a requrest for a feature, let me know at tony@svanstrom.com.
Here's the link: google in perl.

[ Reply to This | # ]
surfraw patch
Authored by: flipflop on Sep 04, '04 03:27:16PM

If you want to use surfraw with your default browser, and don't want to set up all those aliases, all you need to do is:

(a) change one line in the surfraw script (if installed w/ fink, it is at /sw/bin/surfraw):

the line which reads:

if ok SURFRAW_graphical && test -n "$DISPLAY"; then

should be changed to read:

if ok SURFRAW_graphical; then

(it was written on the assumption that you would only want to use a graphical browser if X11 was running.)

(b) edit the surfraw.conf file (if installed w/ fink, it is /sw/etc/surfraw.conf):

You want to change:

def SURFRAW_graphical_browser mozilla

to

def SURFRAW_graphical_browser open

or (if you have launch installed):

def SURFRAW_graphical_browser launch

Now you should be able to run surfraw directly, without having to pass the browser option on the command line.

[ Reply to This | # ]