Thursday, May 31, 2007

Disk usage in linux

A quick summary of the used/available space for each partition of the system can be retrieved with the df command:

apapad@Zzzzz:~> df
Filesystem      1K-blocks      Used Available Use% Mounted on
/dev/hda7        15172120   4803284   9598120  34% /
varrun             517208        88    517120   1% /var/run
varlock            517208         0    517208   0% /var/lock
procbususb         517208       120    517088   1% /proc/bus/usb
udev               517208       120    517088   1% /dev
devshm             517208         0    517208   0% /dev/shm
/dev/hda8        12507868   9081380   2791120  77% /home
/dev/hda2        28653984  23529856   5124128  83% /media/windows/D
/dev/hdc          4338192   4338192         0 100% /media/cdrom0
A more comprehensive view can be obtained by using the -h switch (which stands for human readable).
apapad@Zzzzz:~> df -h
Filesystem         Size  Used Avail Use% Mounted on
/dev/hda7           15G  4.6G  9.2G  34% /
varrun             506M   88K  505M   1% /var/run
varlock            506M     0  506M   0% /var/lock
procbususb         506M  120K  505M   1% /proc/bus/usb
udev               506M  120K  505M   1% /dev
devshm             506M     0  506M   0% /dev/shm
/dev/hda8           12G  8.7G  2.7G  77% /home
/dev/hda2           28G   23G  4.9G  83% /media/windows/D
/dev/hdc           4.2G  4.2G     0 100% /media/cdrom0

This still doesn't give enough information. We might wish to know which folders are taking up the most space. This can be done with the du command:

apapad@Zzzzz:~> du -sk /home/apapad/* | sort -rn
2260396 /home/apapad/Desktop
1413532 /home/apapad/torrent
106832  /home/apapad/iexplore
10288   /home/apapad/workspace
6532    /home/apapad/mypics
6300    /home/apapad/mydocs
2236    /home/apapad/bin
880     /home/apapad/archive
428     /home/apapad/wireless
304     /home/apapad/resume
80      /home/apapad/gl2gr
12      /home/apapad/games
8       /home/apapad/dvdrip-data
4       /home/apapad/wine-src
4       /home/apapad/downloads
0       /home/apapad/Examples

The -k option tells du to use 1K-blocks. (Actually this is the default value, so it is not really required). The -s option gives a total for each directory. Piping the output of du to sort gives a nice result: -n tells sort that it should sort numbers rather than strings and -n returns the results in reverse order.

du also has a -h option for human readable results, but this wouldn't work well with sort...

Friday, May 25, 2007

mysql and external files

A mysql database can be dumped to a file with the mysqldump command.

mysqldump -uusername -ppassword database_name > file.sql
This is particularly useful for backup purposes. The dumped database can then be reloaded with this command:
mysql -uusername -ppassword database_name < file.sql
These two commands can be combined in order to replicate a database to a remote mysql server.
mysqldump -ulocal_username -plocal_password local_database_name | mysql -uremote_username -premote_password remote_database_name

If only some of the database tables need to be dumped, the --tables option can be used.

mysqldump -uusername -ppassword database_name --tables table1 table2 > file.sql

This, however, dumps not only the table data, but also the table creation commands and other information. If we just need to write a table into a CSV file or read table data from a CSV file, we can use the SELECT ... INTO OUTFILE and LOAD DATA INFILE commands.

SELECT columns into outfile 'file_name' FIELD TERMINATED BY 'char' FROM table_name; LOAD DATA INFILE 'file_name' INTO TABLE table_name FIELD TERMINATED BY 'char';
Note that the file name should be placed in quotes and it is preferable to use absolute paths, otherwise mysql will look for the file in the mysql directory (eg /var/lib/mysql/database_name). There is also a nasty complication in this point with the permissions that I haven't figured out yet.

Other interesting options for these two commands are
FIELDS TERMINATED BY 'char' OPTIONALLY ENCLOSED BY 'char'
and
LINES TERMINATED BY 'char'.

The full documentation can be found here.


Thursday, May 24, 2007

JSP: Setting and accessing variables

A new variable can be declared in JSP with the <c:set&ht; tag. Example:
<c:set var="variable_name" value="value" scope="page|session|request|application">

As the example above indicates, we can select among the four available scopes:

  • page
  • request
  • session
  • application

The variable can be printed out with the <c:out> tag. It can be accessed in EL (expression language) with the following syntax:

${pageScope.variable_name}
${requestScope.variable_name}
${sessiomScope.variable_name}
${applicationScope.variable_name}
and from within a scriptlet as follows:
<%= pageContext.getAttribute(variable_name) %>
<%= request.getAttribute(variable_name) %>
<%= session.getAttribute(variable_name) %>
<%= application.getAttribute(variable_name) %>

Wednesday, May 23, 2007

A nice-looking font for Ubuntu

I have been looking for a nice font to use as the standard 'application font' in Ubuntu for quite a long time, and finally I found a great one, thanks to this blog.

I only tried the first font-family the author suggests, called Gentium, and since this looks great on my screen (much better than the default Serif/SansSerif fonts I had been using till now), I didn't look any further. One very positive feature of the Gentium fonts for me was the support for Greek characters.

The font can be installed with the command:

sudo aptitude install ttf-gentium

Wednesday, May 16, 2007

HTML/JSP: How to pass the '#' character as an HTML request parameter

I found myself into a situation today, where I was trying to pass a string (actually a URI) as a request parameter through a URL, but my program wouldn't work. I added debug print statements and found out that although I was passing the string
http://localhost/life_event.owl#Document
the page that was processing the request only received
http://localhost/life_event.owl
It immediately became apparent that the problem was caused by the hash/pound symbol ('#'), which is used as a special character in URLs to introduce a "fragment identifier". A URL link containing a fragment identifier links to a specific section of a webpage and a browser that follows this link will set its viewport on this exact section rather than the top of the page. Escaping this character is rather easy: you just need to replace '#' with '%23'. In my case I was using JSP with JSTL, and this string was returned as a bean property (for those who are curious it is the URI for an OWL ontology resource). This substitution can be performed by using the fn:replace function:
<a href="${fn:replace(original_string,'#','%23')}">Text here</a>
I noticed that it is not necessary to "decode" the '%23' escaped character back to '#' on the request-handler side, but I am not sure if this is specific to JSP only.
Of course, in order for all this to work, you need to import the fn tag library.
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Saturday, May 12, 2007

Ubuntu: Setting the Win+D combination to bring up the desktop.

One of the most useful keyboard shortcuts that are set-up by default on Windows (although not widely known), is the Win+D combination, which minimizes all windows and displays the desktop. Ubuntu has this little icon on the bottom-left corner, but that was not enough for me...
This shortcut can be configured to work in Ubuntu in exactly the same way as with Windows, by following these steps:

  1. First we need to make sure the Win key is mapped to 'Super'.
    System &rarr Preferences &rarr Keyboard &rarr Layout Options &rarr Alt/Win Key behaviour &rarr Super is mapped to the Win keys.

  2. Set the Win+D combination to do what we want:
    System &rarr Preferences &rarr Keyboard shortcuts &rarr Hide all windows and focus desktop (which can be found under the category 'Window management').

Friday, May 11, 2007

Linux: The replace command

The linux replace utility is a very useful command for performing string substitutions, which I find much easier to use than sed. It has a much simpler syntax, performs the substitution in place (unlike sed which leaves the input file intact) and allows you to carry out multiple string substitutions in a series of files at once.

The basic syntax for the command is:
replace from to [from to] ... -- file [file] ...
The man page can be found here, although the usage is very simple. You first specify an arbitrary number of from-to string pairs. These strings can be placed in quotes if they contain spaces and other special characters. I am not sure at the moment if replace supports regular expression (of which I am not a really big fan, so I don't really mind :P). The '--' delimiter signals the end of the list of substitution pairs and is followed by the list of files that will be edited.

Example:
replace foo bar one two -- index.html
This command will replace all occurrences of "foo" in file index.html with "bar" and all occurrences of "one" with "two".
The replace command returns a notification on which files were actually converted. It does not report how many changes were made to each file or anything more detailed, but still the piece of information it does return is quite useful.

One disadvantage I find in replace is that it doesn't have an option to recursively access and edit files. One workaround is to feed it with the output of a grep -l command, eg.
replace foo bar -- `grep -l foo *`

Unfortunately, the replace utility is not usually installed by default on linux systems; it was designed to be used by the msql2mysql utility, therefore it is installed only when mysql is installed.