Americas

  • United States
sandra_henrystocker
Unix Dweeb

Using Wikipedia from the Linux command line

How-To
Oct 14, 20225 mins
Linux

A tool called wikit provides an easy way to get information from Wikipedia without leaving the Linux command line.

woman looking at computer abstract

If you are sitting in front of a Linux system, you can always pop open a browser and query topics of interest on Wikipedia. On the other hand, if you’re logged on through a terminal emulator like PuTTY or you just prefer using the command line, there is another option: wikit.

Wikit is a tool that queries Wikipedia from the command line and provides summaries of its content on a huge collection of topics. It’s easy to use and allows you to quickly query and, if you want, save the rendered information in a file.

How to use wikit

One of the things Wikipedia will not, at least currently, tell you about is wikit itself. So, this post will provide information on the command and show you how you can use it.

As an example, if I want to look up one of the many cities where I lived while growing up, I might run a command like this:

$ wikit Rahway
Rahway is a city in southern Union County, New Jersey, United States. A bedroom
 community of New York City, it is centrally located in the Rahway Valley region,
 in the New York metropolitan area. The city is 21.6 miles southwest of Manhattan
 and 5 miles west of Staten Island. Built on the navigable Rahway River, it was
 an industrial and artisanal craft city for much of its history. The city has
 increasingly reinvented itself in recent years as a diverse regional hub for
 the arts. As of the 2010 United States Census, the city's population was 27,346,
 reflecting an increase of 846 (+3.2%) from the 26,500 counted in the 2000 Census,
 which had in turn increased by 1,175 (+4.6%) from the 25,325 counted in the
 1990 Census.

I can also easily save the returned text in a file like this:

$ wikit Rahway > Rahway

Wikit, which rhymes with words like “click it” or “ticket”, retrieves the summaries from Wikipedia and, if you provide an argument which matches multiple Wikipedia pages, will provide a list of related topics that you can scroll through with your up and down arrow keys. If I ask about 911, for example, the list will start with entries like these, but run on for quite a number of screens of additional topics:

$ wikit 911
? Ambiguous results, "911" may refer to: (Use arrow keys)
▯ AD 911
  911 BC
  September 11
  September 11 attacks
  11 de Septiembre
  November 9
  911 (number)
  9-1-1
  9-1-1 (Philippines)
  9/11: The Twin Towers
  9/11 (2002 film)
  9/11 (2017 film)
  Reno 911!: Miami
  Reno 911!
  9-11 (Noam Chomsky)

How to get wikit

While wikit isn’t installed on Linux systems by default, it only takes a few steps to get it. You first need both nodejs and npm installed. Otherwise, you can install them with a command like one of these:

$ sudo dnf install nodejs npm
$ sudo apt install nodejs npm

Then, to get wikit installed, use an npm command like this:

$ sudo npm install wikit -g

I also ran the following command to upgrade to the latest release of npm after being prompted to do so.

 $ sudo npm install -g npm@8.19.2

Getting and using text from Wikipedia

If you query a complex subject, be prepared for wikit to provide one very long line of text. For example, if I ask Wikipedia for information on astronomy, I would see this even though the file would contain about 2,000 characters:

$ wikit astronomy > Astro
$ wc -l Astro
1 Astro

To split all those characters into multiple lines with a maximum length of 80, but only on word boundaries, I used this command:

$ fold -s -w80 Astro > Astro2
$ wc -l Astro2
24 Astro2

The -w80 specifies the maximum length of 80 while the -s tells it to break on spaces.

Where the original file had a single line, the folded one has 24:

$ wc -l Astro
1 Astro
$ wc -l Astro2
24 Astro2

A multi-word subject does not have to be enclosed in quotes for wikit as this example illustrates:

$ wikit pecan pie
 Pecan pie is a pie of pecan nuts mixed with a filling of eggs, butter, and sugar
 (typically corn syrup). Variations may include white or brown sugar, cane syrup,
 sugar syrup, molasses, maple syrup, or honey. It is popularly served at holiday
 meals in the United States and is considered a specialty of Southern U.S. origin.
 Most pecan pie recipes include salt and vanilla as flavorings. Chocolate and
 bourbon whiskey are other popular additions to the recipe. Pecan pie is often
 served with whipped cream, vanilla ice cream, or hard sauce.

Of course, even Wikipedia isn’t going to provide you with everything you’d like to know.

$ wikit pecan pie recipe
pecan pie recipe not found :^(

The language that wikit will use is specified in the wikit.jspn file and can be modified if you prefer to see the subject summaries available in other languages.

$ cat .config/configstore/wikit.json
{
        "lang": "en"
}$

Change “en” to “fr” and the description of my old home town will look like this:

$ wikit Rahway
 La ville de Rahway (en anglais ) est située dans le comté d’Union, dans l’État
 du New Jersey, aux États-Unis. Sa population s’élevait à 27346 habitants lors
 du recensement de 2010, estimée à 30131 habitants en 2017. Rahway fait partie
 du Grand New York.

Wrap-Up

Wikit provides an easy way to grab text from Wikipedia on the command line and to view it or store it on your Linux system.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.