10 fun and/or useful things to do at Ubuntu’s command-line

Ah, the command-line! Refuge for the scoundrel, proving ground for the true expert, tool of choice for the power-user. 

The following 10 tips explain how to do cool and interesting things at the command-line. They’re not specific to Ubuntu but they were written with Ubuntu in mind, and have not been tested on other distros. But all should work on most versions of Linux or even Unix. 

Use the mouse in virtual consoles

This is a neat hack that brings a block cursor to virtual console windows so that text can be easily copied and pasted. Just use Synaptic to install gpm. Once installed, open a terminal window, and type sudo /etc/init.d/gpm start to get the program running. In the future, gpm will start automatically on bootup.

Then switch to a virtual console to see the fruits of your labor. You should now have a block mouse cursor that moves around the screen. You can highlight text in the usual way. To paste it, click the middle mouse button (on most mice, this is the scroll wheel button; if your mouse has only two buttons, the right-click button will paste the text).

Some software that offers text-mode menus also responds to mouse clicks in this way (for example, the Lynx text-mode web browser—see below). Check the command’s help output to see whether a special command option is needed to support gpm.

See a calendar

You’ve probably already realized that clicking the time display at the top-right of the Ubuntu desktop shows a calendar. To see the same kind of thing at the command line, type cal. Without any command arguments, it will show the dates for the current month. If you want the axis of the calendar reversed (days down the side, rather than across the top), type ncal instead. To see the dates for last month, this month, and next month, type cal -3 (for some reason, however, this particular command option doesn’t work with ncal).

To see a calendar for a whole year, type the year straight afterward: cal 2010. To see a calendar for December of any particular year, type cal dec followed by the year (or you could type jan, feb, mar, and so on).

Both cal and ncal can be used to find out historical dates. To find out the day when the Declaration of Independence was signed, type cal july 1776. If you seriously need to know precise dates going back millennia, there might be issues with the Julian/Gregorian calendar switch—see cal’s man page for details.

Create text banners

Start Synaptic. Then search for and install figlet. Then type the following into a terminal window:

figlet "Ubuntu Kung Fu"

The output is built from symbols, letters, and other characters. There’s even different fonts available—take a look in /usr/share/figlet. Any file with an .flf file extension is a font. To use a different font, just specify its name after the -f command option, without a file extension:

figlet -f lean "Ubuntu Kung Fu"

Believe it or not, figlet did have a serious use (well, actually, its older brother called banner did). In the days of shared dot-matrix printers and sheet-fed paper, the command was used to clearly indicate who had sent which print job. The banner text would appear at the start of any printed documents, so it was clear where the sheet output could be torn off.

I like to add a figlet command to the end of my .bashrc file so that figlet runs every time I log in at a virtual console or open a terminal window. Just type gedit ~/.bashrc to open the file in Gedit and add the entire command as a new line at the end. If you want a sentence to appear, as opposed to just a single word, ensure you enclose the sentence in quotation marks (that is, figlet -f small “Greetings Professor Falken”).

You might want to take a look at the unfortunately titled toilet, which does exactly the same thing but with added color. Once it’s installed, try the following:

toilet -f mono12 -F gay "Ubuntu Kung Fu"

See a visual representation of files/folders

If you’re new to the Ubuntu file system (or even an old hand), it can be easy to get lost while browsing the file system. The pwd can be used to get a quick reminder of the current folder, but you might also use the tree command. First you’ll need to install it using Synaptic—search for and install tree. Then just type tree at the prompt. Here’s what I saw on my test system when I typed the command within my /home folder:

.
|-- Desktop
| |-- gnome-terminal.desktop
| `-- synaptic.desktop
|-- Documents
| |-- accounts08.ods
| `-- brochure.pdf
|-- Examples -> /usr/share/example-content
|-- Music
| `-- tom gold-magic.mp3
|-- Pictures
| |-- barbecue.jpg
| `-- disneyland.jpg
|-- Public
|-- Templates
`-- Videos

It should be obvious what’s what here. The folders (Desktop, Documents, Music, and so on) are represented as branches on the virtual tree and the files (or subfolders) as subbranches. What you don’t see here, and which is very useful, is that everything is color -coded according to the standard color-coding used at the prompt. Thus, folders are light blue, image files are purple, the MP3 file is green, and so on. To see only folders, and not files within them (possibly more useful), use the -d command option: tree -d. To filter the results for a particular type of file or files with a particular name, use the -P command option. For example, to filter for .doc files, you could type the following:

tree -P *.doc

Or to filter for files that include disneyland in their name, you could type the following:

tree -P *disneyland*

As if all this wasn’t enough, tree has a trick up its sleeve. It can output everything as a hyperlinked HTML file. This can be useful if you need to quickly create a directory listing of online files. Let’s assume you have a website called http://www.example.com and the local folder that contains your local copies of the site is /home/keir/website. The following command will output a file called index.html that contains a visual tree representation of the files contained within website, including hyperlinks to the files themselves:

$ tree -H http://www.example.com -T "Click a file to download" /home/keir/website/ > index.html

First we provide the URL that the hyperlinks should be prefaced with. This could be a path on the server (for example, www.example.com/files; note that you must not include the trailing slash in the path). Then we provide the -T command option, which gives the web page a header—this can be anything you want, but steer clear of symbols like !, which have specific functions at the command prompt. After this, we provide the location of the files. Finally, we redirect output into the index.html file.

View PDFs at the Command Line

If you want to view a PDF, simply use the Evince program: evince filename.pdf. This will open a program window showing the PDF file. If you actually want to look at the PDF within the terminal window (or maybe in a virtual console), you’ll first need to convert it to text. To do this, use the pdftotext program: pdftotext filename.pdf. This will create a .txt file containing the contents of the PDF. To view it, use the less command: less filename.txt. To extract the images from the PDF, use the pdfimages command. You’ll need to specify the filenames for the pictures and also the -j command option to ensure the photographic images are outputted as JPEG. For example, the following:

pdfimages -j filename-pdf pictures

…will extract the images as JPEGs and give them filenames beginning with pictures. So, the first might be pictures-001.jpg, the second pictures-002.jpg, and so on.

Mirror Commands and Output Across Different Terminal Windows

To have one terminal window mirror the contents of another, first start a screen session in one of them. screen effectively allows you to create a command-line login that’s independent of any actual terminal windows or virtual consoles (so if the terminal window quits, the command-line login will still be running in the background).

To start it, simply type screen. Then open another terminal window, and attach to the currently running screen session by typing screen -x. Now try typing something to see what the effect is. To detach from the screen session, in either or both terminal windows, type Ctrl + a, and then hit d. Note that if you detach in both terminal windows, the screen session will still be running in the background. To quit it, you must reattach to it (type screen -r) and hit Ctrl + d (or just type exit at the prompt).

This trick works in a virtual console too—you could start a screen session in a terminal window and have it mirrored at a virtual console prompt by attaching to it using screen -x.

By combining this tip with an SSH remote connection, not only can you create a command login using screen that will persist on the remote computer even if the SSH connection is lost (useful if running commands that take some time to complete or if you’re using a flaky connection), but you can create a setup whereby what you type is mirrored on the remote computer in a terminal window—just ask the user sitting in front of the remote computer to open a terminal window and type screen -x, once you’ve started screen in the SSH session. This provides an excellent way of remote teaching.

Insert Command-Line Output and Files into the Clipboard

Wouldn’t it be useful to quickly pump an entire configuration file, or the output of a terminal command, into the clipboard for pasting into a website forum’s posting page or similar? Well, that’s just what xclip is designed to do. It can be installed via Synaptic. Once installed, you can redirect text files into xclip so that they become the clipboard contents:

xclip < /etc/fstab

…which will add the contents of the /etc/fstab configuration file to the clipboard, or you can pipe the output of a command into it:

dmesg|xclip

…which will place the output of the dmesg command in the clipboard (dmesg shows system log output and can be useful when diagnosing problems). There is one proviso. The piped output/files are placed in the selection buffer clipboard, which is distinct from the standard cut/copy and paste clipboard accessible from the Edit menu of most applications. xclip’s output can be pasted by positioning the cursor in the relevant spot and clicking the middle mouse button (this means pressing the scroll wheel, if your mouse has one; if not, click both the left and right mouse buttons simultaneously). In theory, the use of the -selection command option with xclip should allow the user to add to the primary clipboard, but this doesn’t appear to work, perhaps because of the way the Ubuntu desktop is configured. To be honest, I see this as less of a bug and more as a feature—xclip will leave any existing clipboard contents untouched.

Have Ubuntu Speak to you

Ubuntu includes a built-in speech synthesizer called espeak. It’s there to work in partnership with the Orca screen reader, which provides support for those who are partially sighted, but it can also be called from the command line, as follows:

espeak "Ubuntu Kung Fu"

As you’ll be able to tell, it’s not the most sophisticated speech synthesizer in the world (it has a feel of Speak & Spell about it), but it can be fun to play around with. By simply typing espeak and then hitting Enter, whatever you type after this will be spoken. To quit, type Ctrl + d. To switch voices, use the -v command option, but first you’ll need to find out the available voices, which can be done by typing espeak –voices=en (note that’s two dashes before voices). For example, to have the phrase “How about a nice game of chess?” spoken in a Jamaican voice, you could type this:

espeak -s 140 -v en-westindies "How about a nice game of chess?"

In this example I also added the -s command option, by which you can specify the speech speed in words per minute. The default value of 170 tends to be a little fast, especially when it comes to longer sentences.

Get Rid of the Virtual Console Legal Boilerplate

Whenever you log in to a virtual console, you’ll see a few paragraphs of legal boilerplate, reminding you that Ubuntu is free software and is supplied without warranty. Once you’ve read this, you’re unlikely to forget it, so to stop it appearing each time you log in, type the following into a terminal window, which will delete the contents of the “message of the day” (motd) file, which is responsible for the message:

sudo rm /etc/motd
sudo touch /etc/motd

Of course, rather than deleting the contents of the file, you might just choose to replace the text within it with something else. It’s a simple text file. To load it into Gedit, type gksu gedit /etc/motd, and change its contents to whatever you want.

Browse the Web from the Command Line

Call it a form of insurance, but I like to have a command-line web browser installed in case anything goes wrong with either Firefox or the entire GUI system. I can then look up help and solutions from a virtual console or just check the news while I’m waiting for things to get better. Command-line browsers are pretty primitive. There are no images, for example, or even color. The page design always gets mangled. In other words, they’re not for use all the time, unless you’re a masochist. Or a command-line fanatic.

There are two competing text-mode browsers—links and lynx. links is perhaps the better of the two because it understands frames and thus gets the layout of pages slightly more correct, but both are only a download away via Synaptic.

Once either program has started, hit g to enter a URL (with lynx, you’ll also need to type http:// if the address doesn’t start with www). Once the page has loaded, use Page Up and Page Down to scroll the page. Use the up/down cursor keys to cycle through each link onscreen until you find the one you want, and then hit Enter to follow it. To go back a page, hit the left cursor key. To download a file that’s linked to, highlight the link, and hit d. You can search for words using a forward slash (/), in the same way as with man pages.

Hitting Esc in links will cause a rudimentary menu to appear—use the cursor keys to navigate, and hit Enter to select a menu option. Once you’re done, hit q to quit the program. If links is used in a terminal window, you’ll be able to click each link using your mouse.

If gpm is installed (see above), you’ll have rudimentary mouse control over the browser and can click links in a virtual console window.



__________

 

These tips are taken from Ubuntu Kung Fua book of over 300 excellent tips for Ubuntu. The author, Keir Thomas, also wrote Beginning Ubuntu Linux, a “from zero to hero” guide to Ubuntu, which has recently reached its third edition. Take a moment to check out both of them, and also take a look at the rest of the site.

1 comment so far ↓

#1 La Figueres Groga · on 08.19.08 at 6:31 am

[...] News » News 10 fun and/or useful things to do at Ubuntu’s command-line2008-08-19 08:30:57You reversed (days down the dates for the axis of thing at the command arguments, [...]

You must log in to post a comment.