Using Wikipedia from the Linux command line
A tool called wikit provides an easy way to get information from Wikipedia without leaving the Linux command line.
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:
I can also easily save the returned text in a file like this:
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:
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:
Then, to get wikit installed, use an npm command like this:
I also ran the following command to upgrade to the latest release of npm after being prompted to do so.
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:
To split all those characters into multiple lines with a maximum length of 80, but only on word boundaries, I used this command:
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:
A multi-word subject does not have to be enclosed in quotes for wikit as this example illustrates:
Of course, even Wikipedia isn’t going to provide you with everything you’d like to know.
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.
Change “en” to “fr” and the description of my old home town will look like this:
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.