This blog is about the Linux Command Line Interface (CLI), with an occasional foray into GUI territory.
Instead of just giving you information like some man page, I hope to illustrate each command in real-life scenarios.

Search This Blog

Saturday, November 14, 2009

We all know that the date command tells you the current time. Occasionally, you use the same command to set the time. That however becomes rarer these days with the advent of the ntp service that automatically synchronizes your computer's time with a super accurate public time server of your choice.

Various implementations of the date command are in use today. This article discusses the date command of the GNU coreutils package.

By default, the date command tells you the time now. The date command also lets you do some basic date addition and subtraction. This is achieved by specifying the -d option which displays a time that you entered as a parameter rather than now.

How many times have you asked yourself what the calendar date is N days ago? Just the other day, I needed to find out the precise date of 30 days ago in order to locate the proper log file.

Friday, July 17, 2009

In the olden days, an entire office department shared one printer. At home, an entire household shared one printer. Nowadays, I have 2 printers at home (1 HP LaserJet and 1 Samsung Monochrome Laser printer).

With access to multiple printers come new opportunities and challenges. One of those opportunities is that if one printer goes down for whatever reason, you can route your print jobs to the surviving one. One challenge is that you need to manage multiple printer queues. With more than 1 printer, I often find myself printing to the wrong one. How do you route print jobs from 1 printer to another when the need arises?Job Submitted to Wrong Printer

Let's first consider this situation: you just submit your print job to the default printer(mySamsung), but you realize after the fact that it is the wrong printer.

First, you should find out the print job number. Run the familiar lpq command.

Thursday, July 2, 2009

As an avid emacs user for a decade plus, I am a little embarrassed to confess that I did not know for the longest time how to search for or replace line feed (newline) characters.

If you are only searching for run-of-the-mill ASCII strings such as abc, you just type C-s (control-S), and then the characters, and hit return.

Unfortunately, if your search string includes a line feed character, e.g., abc followed by a newline, I have some good news and some bad news for you.

The good news is that you use the same shortcut (C-s) just like you would normally to search for anything else.

The bad news is that the representation of what emacs understands to be a newline is not that intuitive ... even to geeks. Instead of something like \n which may mean something to developers, you need to enter C-q C-j (Control-Q, Control-J) for each line feed in the search string.

C-q is the shortcut for the quoted-insert command. The character you type immediately after C-q is escaped, i.e., its normal function is suppressed. C-j is the line feed character. Why the letter j, you ask? It turns out that the line feed character is represented by the decimal value 10 in the ASCII table, and j is the 10th letter in the English alphabet. Similarly, if you are searching for the Tab instead of the newline character, use C-q C-i to denote the Tab character.

If you want to replace occurrences of a string that includes one or more line feeds, the method is similar. Use the usual command replace-string or query-replace(M-%). Enter C-q C-j where you would expect to type the new line.

Sunday, June 7, 2009

If you are happily running a desktop search tool such as Tracker or Beagle, read no further. You already have the search tool you need to search OpenOffice files and more. These desktop search tools build indexes to improve search performance, and are in general quite scalable. Yet, a desktop search tool may be an overkill if you are just searching for certain text string in a directory containing OpenOffice files.

loook is a no-frill, yet very functional text search tool for OpenOffice files. Unlike the aforementioned desktop search engines, it does not build an index of your OpenOffice files. Despite the lack of an index, searching a hierarchy of 75 OpenOffice documents only took a couple of seconds. That is good enough for my use.

loook composes of a single zipped-up python script which you can freely download and install. Unzip the downloaded file into a directory of your choice. On my Debian Etch system, I did not have to install any additional packages for loook to run.

Running the tool cannot be any simpler. Open a bash shell, and enter:

$ python somedir/loook.py

The Viewer field has the default value: soffice. This is the name of the OpenOffice executable. You don't need to change this field unless your version of OpenOffice has a different name or the executable is not in your default PATH.

What you do want to change are the following fields:

Search Path

Search Terms

Mode

Search Path

You navigate or type in the path of the directory containing the OpenOffice files you want to search. loook will recursively search the hierarchy starting from the specified directory. Unfortunately, you are limited to specifying one starting directory only.

The Mode determines whether a match requires all the search terms to be present in a document (AND search), any of the terms to be present (OR search), or the terms must appear together (PHRASE search).

You can pre-populate the search path and the search terms. Simply enter them as arguments to loook on the command line. For example,

$ python somedir/loook.py /home/peter/mydocs "Cisco Routers"

The search results are returned in the Matches field. Note that only the full path of the files are returned. It does not display the actual matches. You can double click the file path in the Matches field to directly open the file with OpenOffice.

loook does not have all the bells and whistles that you expect of a desktop search tool, with respect to both features and supported file formats. However, if all you want is a light-weight tool for searching OpenOffice documents in a directory structure, it is worth your time to try it out.

Monday, May 18, 2009

Recently, I had to install a PHP-based web application on my Apache web server.

My first question was: "Is PHP enabled on the web server?"

You can always ask the systems guy. But if you happen to be the systems guy, and you don't quite remember if you have installed PHP, here is how to find out.

Create a new file with a php suffix (say myphp.php)

Copy and paste the following code into the php file.
(I cannot get blogger.com to display PHP code; use the code in the April 29, 2009 comment)

Save the file and upload it to a location on your web server that is accessible to the World Wide Web.For expedience, upload it to the Apache document root directory which is /var/www on my Debian Etch server.

Open your web browser to the URL for that PHP file (http://localhost/myphp.php).

If your web server supports PHP, the browser will return with a page full of information about the PHP version and configuration. In addition, it will display information about your web server and your system in general.

On the contrary, if all you see is a blank page, then you know PHP is not supported.

Note that you must delete the myphp.php file from your web server after you obtain the necessary info. You don't want people to run the PHP script, and obtain sensitive information about your system.

Friday, May 8, 2009

Windows users are familiar with the concept of file association. When you double click a file (say, cisco.doc), Windows examines the file name extension (doc), and opens the file using the default program associated with that extension(Office).

Linux users can open files in a similar way in their X Window graphical user interface. But, if you want to open the file from the command-line, you need to type out at least the program name, oowriter, or do you?

$ oowriter cisco.doc

If GNOME is your window manager, use the gnome-open command as follow:

$ gnome-open cisco.doc

oowriter is automatically started up to open cisco.doc, if oowriter is indeed associated with the .doc file name extension.

For KDE users, use kde-open instead.

Alternatively, you can run the window-manager-neutral program called xdg-open. xdg-open is part of the xdg-utils package.

Finding all empty files in a directory can also be done using a single command. Ditto for non-empty files.

Suppose you want to find all empty files in the directory /home/peter. The command is:

$ find -L /home/peter -maxdepth 1 -type f -size 0

By default, the find command excludes symbolic files. Use the -L option to include them.

The expression -maxdepth 1 specifies that the maximum depth to which the search will drill is one only. By default, the find command will recursively go down the directory. A maximum depth of 1 means that you only want the files directly under /home/peter. Keep in mind that depth 0 is the level of the command line argument (/home/peter). You can use -maxdepth and -mindepth to finely control the depth levels you want included.

-type f means include regular files only. This is not strictly necessary for empty files (as opposed to non-empty ones) because all directories, even those with no files, are of non-empty size.

-size 0 is self-explanatory.

To find all non-empty files in the same directory, simply add ! before -size 0:

$ find -L /home/peter -maxdepth 1 -type f ! -size 0

Note that -type f becomes necessary. Without this expression, subdirectories in /home/peter such as /home/peter/.Trash will appear in the output.

Most of the time, you don't just want to find the files: you want to do something with them. Suppose you want to remove all empty files in the directory. For safety, first run the find command by itself to list the files. Then with the xargs command as below.

Saturday, May 2, 2009

Sometimes, in the Linux command-line world, a seemingly trivial technique can turn out to be tremendously useful. Before I discover the Alt-dot(.) shortcut, I type !$ to insert the last argument of the previous command.

Then, I discovered that typing Alt-dot achieves the same result. That is, press (and hold) the Alt key, then the dot key.

There are some advantages of using Alt-dot over !$. First, you can actually see the argument immediately and interactively. You can verify that is indeed what you want, edit it if necessary, before you continue the command-line input, and eventually hit Enter to execute the command. With !$, you better have a pretty good memory.

Another advantage is that you can repeatedly type Alt-dot. The net effect is that you scroll back in command history, and display the last argument of each successive command.

After assimilating the shortcut into my command-line work habit, I found that I have been using it a lot. Give it a try!