Linux tools

Command line browsing, google and DuckDuckGo request

Command line browsing

Google request command line:

Adding the code bellow in your .bashrc file:

google() {
    search=""; img=""; wiki=0; serie=0;
    echo "Googling: $@";
    for term in $@; 
    do  
     if [ "$term" = "-i" ]; 
     	then img="&tbm=isch" 
     elif [ "$term" = "-l" ]; 
     	then img="$img&btnI=I%27m+Feeling+Lucky" 
     elif [ "$term" = "-w" ];
        then wiki=1
     elif [ "$term" = "-S" ];
        then serie=1
     else
	search="$search%20$term"; 
     fi
    done
    if [ $wiki -eq 1 ];
        then    w3m "http://en.wikipedia.org/wiki/Special:Search/$search"
    elif [ $serie -eq 1 ];
        then    w3m "http://en.wikipedia.org/wiki/Special:Search/list%20of%20$search%20episodes"
    else	    
        w3m  "http://www.google.com/search?hl=en&q=$search$img" 
    fi
}
give you the possibility to ask for
ubuntu @ ubuntu  ~
└─ $ ▶ google hello world
that will produce a google request for "hello world" and display the google results using w3m.
You can use the options: This script was inspired by the shortest one found here.

Google now command line tool

by adding the code bellow in your .bashrc file:

googlenow(){
    search="";
    for term in $@;
    do
     search="$search%20$term";
    done     
    w3m  "http://www.google.com/search?hl=en&q=$search" |sed -n "/About.*results/{n;N;p;}" |sed -r 's .{23}  ';
}

> googlenow examples (hide)
ubuntu@ubuntu:~$ googlenow birth Richard Stallman
March 16, 1953 (age 60 years), Manhattan, New York City, New York, United
States
ubuntu@ubuntu:~$ googlenow age Richard feynman
69 (1918 1988)
Richard Feynman, Age at death
ubuntu@ubuntu:~$ googlenow age universe
13.8 billion years
Universe, Age
ubuntu@ubuntu:~$ googlenow age earth
4.54 billion years
Earth, Age
ubuntu@ubuntu:~$ googlenow earth diameter
7,918 miles (12,742 km)
Earth, Diameter
ubuntu@ubuntu:~$ googlenow moon mass
  • Web            
     mass of the Moon = 7.34767309 × 10^22 kilograms
ubuntu@ubuntu:~$ googlenow 5 celsius to kelvin
  • Web            
     5 degrees Celsius = 278.15 kelvin
ubuntu@ubuntu:~$ googlenow 5 celsius to Fahrenheit
  • Web            
     5 degrees Celsius = 41 degrees Fahrenheit
ubuntu@ubuntu:~$ googlenow 5miles to km
  • Web            
     5 miles = 8.04672 kilometers
ubuntu@ubuntu:~$ googlenow height michael jordan
6' 6" (1.98 m)
Michael Jordan, Height
ubuntu@ubuntu:~$ googlenow height barack obama
6' 1" (1.85 m)
Barack Obama, Height
ubuntu@ubuntu:~$ googlenow area France
260,558 sq miles (674,843 km²)
France, Area
ubuntu@ubuntu:~$ googlenow president of india
Pranab Mukherjee
India, President
ubuntu@ubuntu:~$ googlenow capital kazakhstan
Astana
Kazakhstan, Capital
ubuntu@ubuntu:~$ googlenow population paris
2.211 million (2008)
Paris, Population

DuckDuckGo request command line:

Adding the code bellow in your .bashrc file:

ddg(){
    search=""; bang=""; zeroc=0;
    for term in $@;
    do
     if [ "$term" = "-z" ];
        then zeroc=1
     elif [[ "$term" =~ -([A-Za-z0-9._%+-]*) ]];
        then bang="!${BASH_REMATCH[1]}"
     else
        search="$search%20$term";
     fi
    done
    if [ $zeroc -eq 1 ];
      then w3m  "https://www.duckduckgo.com/?q=$bang$search" | sed -n "/Zero-click/,/More at/p" ;
    else
      w3m  "https://www.duckduckgo.com/?q=$bang$search" ;
    fi
}
short version:
ddg(){ search=""; bang=""; for term in $@;  do  if [[ "$term" =~ -([A-Za-z0-9._%+-]*) ]]; then bang="!${BASH_REMATCH[1]}" ; else search="$search%20$term" ; fi ;  done ; w3m  "https://www.duckduckgo.com/?q=$bang$search" ;}
give you the possibility to ask for
ubuntu @ ubuntu  ~
└─ $ ▶ ddg hello world
that will produce a DuckDuckGo request for "hello world" and display the results using w3m.
You can use the !bang just by replacing the "!" by a "-". So you have:

Surfraw

It seems that I was not the first one to wish such a tool... A similar one (not giving direct answer as ddg -z and ggn) is already existing, and has been created by Julian Assange (the known one).
It's Surfraw, and it has quite a long list of direct research possibilities! More infos can be found on the website.

Videotop: command line youtube browser

This is a "command line browser" for youtube you can find here https://github.com/intnull/videotop (I'm not the author of this!).
Using this

depends on the following packages: be sure to have an updated youtube-dl version and you may need to add in the youtube_client.py '--prefer-insecure' as youtube-dl argument at line 126, mplayer not being able to handle https urls (here) and browsing is similar to vim. Below, an example of YouTube browsing after a search using: ":s Hello World" (or ":search Hello World"): videotop

Pressing Enter starts the download of the vid, pressing "p" plays the downloaded video, and "s" will start the streaming in mplayer without downloading for offline use.

Various .bashrc functions/aliases

You can find below a list of functions you can add to your .bashrc for various purposes, some of them come from commandlinefu.com, a website repository of great shell commands.

Redaction and text tools

Command line data analysis

Live Scores