Tech stuff and info dump

grep: how to use grep to search history (linux)

May 31st, 2010

I often remember only part of something I did before and want to remember how I’ve done something. To easily search through the history, the glorious grep can be invoked like this:

history | grep phrase_to_search_for

If the phrase to search for involves spaces or special characters, then quotes can be used around the phrase:

history | grep ‘phrase to search for’

To search for something with quotes, you can surround your phrase-to-search-for with the ‘other’ kind of quote.

For example,

history | grep ‘phrase to “search” for’

will match

phrase to “search” for

and

history | grep “phrase to ‘search’ for”

will match

phrase to ‘search’ for


Filed under: Command line,linux
May 31st, 2010 11:43:58

linux: find out what you’ve recently typed at the command line

May 31st, 2010

If you open a terminal window (in Ubuntu 10.04, go to Applications->Accessories->Terminal) and type

history

this will print out a list of things that have been recently typed at the command line.

If you’re curious, the history is stored in a file called .bash_history which you can view as a file. In Ubuntu, for example, you can open it with gedit by opening a terminal window and typing

gedit .bash_history

at the command line.

Note: if you have two (or more) terminal windows open and are typing commands in all of them, the history of one terminal window won’t know about what you’ve typed in the other window. If you close, say terminal window A, leave terminal window B open. and then open terminal window C, typing history in terminal window C will display what you’ve typed in A but not in B.

Examples of using grep to search through the history are here.


Filed under: Command line,linux
May 31st, 2010 11:43:53

How to list partitions and basic partition information: linux

May 31st, 2010

To find out basic information about partitions, open a terminal window (Applications -> Accessories -> Terminal in Ubuntu 10.04 and others) and type

fdisk -l

at the command line.

(If nothing happens, try typing

sudo fdisk -l

at the command line and entering your password when prompted.)

Something like this should be displayed:

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x66666666

Device Boot Start End Blocks Id System
/dev/sda1 * 1 2613 20988891 c W95 FAT32 (LBA)
/dev/sda2 2614 17532 119830471+ f W95 Ext’d (LBA)
/dev/sda3 17532 19458 15471448 12 Compaq diagnostics
/dev/sda5 13566 17532 31856640 7 HPFS/NTFS
/dev/sda6 2614 13194 84991819+ 83 Linux
/dev/sda7 13195 13565 2980026 82 Linux swap / Solaris

Here, /dev/sda6 is my linux partition.

In Ubuntu 10.04, you can navigate to System -> Administration -> Disk Utility to get a big graphical display of this information (and more).


Filed under: Command line,linux
May 31st, 2010 11:09:40

How to tell how much disk space is left: linux

May 31st, 2010

Open a terminal (Applications -> Accessories -> Terminal in Ubuntu 10.04 and others) and type

df

at the command line.

Something like this will be displayed:

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 83657080 25098664 54308828 32% /
none 504088 296 503792 1% /dev
none 508500 372 508128 1% /dev/shm
none 508500 96 508404 1% /var/run
none 508500 0 508500 0% /var/lock
none 508500 0 508500 0% /lib/init/rw

The number listed on the top line (/dev/sda6) under ‘Available’ is the amount of space left on my linux partition.

For more human-readable numbers, try:

df -h

which will display something like this:

Filesystem Size Used Avail Use% Mounted on
/dev/sda6 80G 24G 52G 32% /
none 493M 296K 492M 1% /dev
none 497M 372K 497M 1% /dev/shm
none 497M 96K 497M 1% /var/run
none 497M 0 497M 0% /var/lock
none 497M 0 497M 0% /lib/init/rw

The df command won’t show space available on unmounted drives.

If you want to see basic information about the partitions on a machine, see this post.


Filed under: Command line,linux
May 31st, 2010 10:58:03

Ubuntu 10.04 (Lucid Lynx): simple, step-by-step lamp server installation (linux, apache, mysql, php)

May 13th, 2010

Well, today’s the day for installing a lamp server on my 10.04 machine! Wireless is working just well enough that I’ve not given up and gone running back to 9.04, although there are still some mysteries to figure out where that’s concerned.

It’s worth noting that I’m setting up my server for personal use. I want something that will let me test out webpages that will hosted by other people’s servers. (If you’re setting up an internet-facing server, these instructions will get you started, but more work is needed to keep the server safe from the wild world of the internet. If I come across a good tutorial on such things, I’ll post it.)

Downloading Necessary Packages

Synaptic Package Manager can be opened by going here:

System -> Administration -> Synaptic Package Manager

and entering one’s password.

I used Synaptic Package Manager to download the following packages:

  • php5 (this installs apache2-mpm-prefork, etc.)
  • mysql-server-5.1 (this installs the client and core as well)
  • Optional: phpMyAdmin (a nice GUI for managing MySQL tables)
  • Optional: php5-cli (only needed if you want to run PHP from the command line)

The packages can be found by clicking the Search icon and typing in the package names. When a package name is selected, Synaptic Package Manager kindly prompts one to install all other files required. Once you have chosen all the packages needed, click ‘Apply’ (underneath the green check mark) at the top of the Synaptic Package Manager window.

File Installation

For the most part, Synaptic Package Manager will take care of all the installation. There are a couple things that might need to be done manually, however.

Configuring phpMyAdmin

If you have chosen to install phpMyAdmin, you may get a pop-up window called ‘Configuring phpmyadmin’ with the text “Web server to reconfigure automatically:” and a choice of servers. This is just asking which server you want to be able to run phpMyAdmin. I was given the choice of apache2 and lighttpd. I ticked apache2 and clicked the ‘Forward’ button.

Configuring MySQL

The mysql installation also requires some information. A pop-up window called ‘Configuring mysql-server-5.’ should pop up with the text ‘New password for the MySQL “root” user:’ and a text box.

Make up a password for the root user and enter it into this box. (Write it down – you’ll need it later!) Click ‘Forward’. You’ll be asked to re-enter the password. Do so, and click ‘Forward’ again.

Database for phpMyAdmin

If you’ve chosen to install phpMyAdmin, you’ll get another ‘Configuring phpmyadmin’ pop-up with the text ‘Configure database for phpmyadmin with dbconfig-common?’ and a tickbox. I left this ticked; clicking ‘Help’ explains under what circumstances one wouldn’t want this to happen. Once the selection is made, click ‘Forward’ to continue.

If the box was ticked, you are then prompted for “Password of the database’s administrative user:” and given another text box. Use the MySQL root password that you just made up.

After this, the installation should complete itself without any more human help.

Restarting the Apache server

Before anything can be done, the Apache server will need to be restarted.

Open a terminal (Applications -> Accessories -> Terminal) and, at the command line, type:

sudo apache2ctl restart

The first time I did this, I received this warning:

apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

This went away when I did this:

Letting Apache know about localhost

Open a terminal (Applications -> Accessories -> Terminal) and, at the command line, type:

sudo gedit /etc/apache2/apache2.conf

The sudo command gives you root-like powers (needed here to edit this particular file), gedit opens the notepad-like application, and /etc/apache2/apache2.conf is the name of the file being edited.

(If prompted for a password, use the password that is used to log in to Ubuntu.)

This should open a notepad-like window with a lot of stuff.

I added

ServerName localhost

as a separate line at the end of the file and saved the document (File -> Save in gedit).

After saving, restart Apache again by typing

sudo apache2ctl restart

at the command line.

Testing to make sure PHP works

Open a terminal window by going to

Applications -> Accessories -> Terminal

Type this (pressing enter when finished) at the command line:

cd /var/www/

This takes you to the /var/www folder. Then type:

sudo gedit

and press enter. You’ll be prompted for your system password (the one used to log in to Ubuntu). (The ‘gedit’ opens a notepad application; the ‘sudo’ is necessary because if gives you root-like powers which are needed to save a file in the /var/www folder.)

In the notepad application that should have popped up, type the following:

# test.php
<?
phpinfo();
?>

Then go to File -> Save As and enter test.php as the name of the file.

Open a browser (like, say, Firefox at Applications -> Internet -> Firefox Web Browser) and type

http://localhost/test.php

and you should get a page come up with all sorts of useful and interesting information about your Apache server! It works!

(If you get a page that just says “# test.php” then Apache doesn’t know about localhost. Try editing /etc/apache2/apache2.conf and restarting the Apache server as described above.)

Setting up phpMyAdmin

If you’ve opted for phpMyAdmin, there is one quick little thing to do to make it work.

Open a terminal window (Applications -> Accessories -> Terminal) and type:

sudo gedit /etc/apache2/apache2.conf

at the command line prompt. If prompted for a password, use the password for logging in to Ubuntu.

Add this line to the file that has been opened in the notepad-like window:

Include /etc/phpmyadmin/apache.conf

Make sure this is on a line of its own. Save the file (File -> Save), close the gedit window and restart Apache by typing:

sudo apache2ctl restart

at the command line prompt.

Check to make sure phpMyAdmin works

Open your favourite web browser (say, Firefox) and navigate to:

http://localhost/phpmyadmin/

This should bring up a login screen. You can log in to phpMyAdmin by using ‘root’ as the username and the mysql root password set during installation.

The mysql root password can be changed in phpMyAdmin by going to the privilege page and clicking the pencil icon next to each root account. Enter a password in appropriate field of the newly-loaded page.

Once logged in, you can use phpMyAdmin to manage your MySQL databases.

Where Important Things Are

Here’s where to find useful stuff.

The Apache configuration file is located at:

/etc/apache2/apache2.conf

The default web folder is here:

/var/www

Apache error logs are here:

h1

If anything goes horribly wrong, reading these can often be very helpful. (Even if what’s written in the file makes no sense, it’s often quite easy to pop the error message or warning into Google. Very often, there’s someone talking about the problem using real words in the first page of hits.)

Apache access logs live here:

/var/log/apache2/access.log

NB: Both etc and var can be found two levels ‘up’ from the home folder.


Filed under: apache,lamp server,mysql,php,Ubuntu 10.04
May 13th, 2010 12:54:28

How to tell which version of java is running in linux (Ubuntu)

May 10th, 2010

Command line

To find out which version of java is installed, type

java -version

in a terminal window. This will give something like this:

java version “1.5.0”
gij (GNU libgcj) version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)

Browser

You can also visit www.java.com’s Verify Java Version page. On this page, there is a pleasingly big red button that you can click which will tell you what version of java is installed.

Pressing the pleasingly big red button will tell you something like this:

Your Java version is 1.5.0_22

Of course, if one isn’t running the latest and greatest version, this page tells you to install the most up-to-date version, but I usually prefer to wait until there is an official Ubuntu update.


Filed under: Command line,General,java
May 10th, 2010 12:12:07