Monday, May 18, 2009

Is PHP supported on my Web Server?

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.

  1. Create a new file with a php suffix (say myphp.php)
  2. 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)
  3. 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.
  4. 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.

Saturday, May 16, 2009

More on Inserting Arguments from Previous Commands

I previously blogged on using the shortcut Alt + dot to insert the last argument from the previous command.

Suppose you don't want the last argument. Instead, you want to insert the first, second, or third argument of a previous command.

No problem!

Alt + 1 + dot inserts the first argument of the last command. To key it in properly, hold the alt key, press the 1 key, and then the dot (".") key.

Similarly, alt + 2 + dot inserts the second command argument.

For example, you just executed this command.
$ ls -l /home/peter/somefile.txt   secondfile.txt
-rw-r--r-- 1 peter peter 8115 2007-12-19 21:03 somefile.txt
$

If you now type in cat and then alt + 2 + dot, it will insert the second argument from the last command (-l is the first).
$ cat /home/peter/somefile.txt 


You can repeat the key sequence, and this will go back 1 command at a time, and insert the specified argument of that command.

If you want just the command itself, not an argument, type in alt + 0 + dot .


P.S. Related articles from this blog:

Friday, May 8, 2009

Open a file from the command line using its default application

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.

$ xdg-open cisco.doc

Tuesday, May 5, 2009

Find all (non-)empty files in a directory

Creating an empty file in Linux is easy. If a-file does not exist, create the file and make it empty by simply:
$ touch a-file
$ ls -l a-file
-rw-r--r-- 1 peter peter 0 2009-05-02 20:15 a-file

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.
 $ find -L /home/peter -maxdepth 1  -type f  -size 0 
/home/peter/file1.txt
/home/peter/file2.txt
$ find -L /home/peter -maxdepth 1 -type f -size 0 -print0 |xargs -0 -r rm -f

For more information about how to use the find and xargs commands, see my earlier blog entry.

Saturday, May 2, 2009

A surefire shortcut to Insert the Last Argument of the Last Command

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.

peter@tiger:~$ ls -l Windows_20081110102654.log
-rw-r--r-- 1 peter peter 808 2008-11-10 10:26 Windows_20081110102654.log
peter@tiger:~$ cat !$
cat Windows_20081110102654.log
.....
.....
peter@tiger:~$

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!

PS Related entries from this blog: