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

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!