Thursday, January 13, 2011

cd command with two arguments to do directory substitution

At some place I've worked in the past I had the ability to issue a CD command with two parameters. Extensive googling for this resulted in nothing, so I wrote this bash function to do it.

Using 'cd' in this mode will replace the first occurrence of the first parameter with the second parameter. The simplest use case is changing from classes to src directory when in a deep directory structure:

$ pwd
/home/rcs/project/src/com/xyz/custom/some/really/deep/path
$ cd src classes
$ pwd
/home/rcs/project/classes/com/xyz/custom/some/really/deep/path


function cd {
if [ -z $2 ]
then
builtin cd $1
else
builtin cd `echo $PWD | sed s/$1/$2/`
fi
}


It's not bullet proof, but it's decreased the achyness in my typing fingers!

EDIT - I found a few bugs with the function; namely I could not change dir to a directory that contained spaces. Once I made a change to correct this problem, I could no longer issue 'cd' to return to my home directory.

Here are my change to correct these problems:


function cd {
if [ -z "$2" ]
then
if [ -z "$1" ]
then
builtin cd
else
builtin cd "$*"
fi
else
builtin cd `echo $PWD | sed s/$1/$2/`
fi
}

Tuesday, January 11, 2011

KDE Kontact using 100% CPU

I observed a problem with Kontact maximising one of my four cores periodically. After extensive Googling, it seemed that I alone was having this problem, and so began my analysis.

Since it wasn't happening continuously, I figured it was a periodic event and the most likely periodic event was a POP3 mail check. I disabled "interval mail checking", but that didn't resolve the issue. It would still periodically consume 100% of one of the CPU cores.

On a whim, I decided to use the telnet command to connect to my POP server. After authenticating and issuing the list command, I observed a LONG LONG pause. I waited nearly 5 minutes before I got the response back, 19k messages and hundreds of megabytes!

At every interval mail check (set at 5 minutes), I was asking the POP server to count the number of messages and number of bytes in those messages. To resolve it, I added an automatic archival of messages older than 6 months on my mail server, dramatically lowering my inbox size, resulting in a minimal delay when checking for new mail, and no noticeable CPU usage!

As to why Kontact was using 100% CPU while simply waiting for a response from the POP3 server, that is still unresolved.