Wednesday, 28 November 2012

cd to directory containing a file

Many times I have run a program on a file and then immediately, wanted to change to the directory containing it, to do more operations. For example:
vim path/to/some/dir/file
then I next want to do:
cd path/to/some/dir
I used to do
cd !$
and then use the up arrow key to get the previous failed command to edit out the trailing filename and re-execute. Then I decided I should write a shell alias or function to do this. It has to be an alias or a function and not a script as cd needs to work in the current shell.

This is what I came up with:
cdf() { cd ${1%/[^/]*}; }
This uses the remove matching suffix operation of parameter substitution, see the bash manual page for details. So now I can do:
cdf !$
and I will end up in the directory containing the file I just worked on.

I picked cdf as an abbreviation for change to directory of file, but you may prefer some other name.

Friday, 23 November 2012

VirtualBox host/guest conflct over audio device

I started a VirtualBox instance on my workstation that contains a CentOS instance to develop software. A bit later on I found this error from a periodic cron job I run on the workstation to record a radio program using the sound card:

rec FAIL formats: can't open input `hw:0': snd_pcm_open error: Device or resource busy
That error message comes from the rec program, part of the sox package. The first time it happened I thought the audio hardware had locked up so I rebooted it and the error went away. The next morning it happened again. I realised that the reason it went away the first time was because the CentOS guest had been shutdown by the reboot.

I disabled the audio device in the guest OS and had no more failures recording from the sound card. In general if you do not need audio in the guest, disable the device so that the guest does not interfere with the host OS's use of it.

You can also disable by editing the XML config fie, but only when the guest OS is not running. This is the relevant line:
<AudioAdapter controller="AC97" driver="ALSA" enabled="false"/>

Thursday, 1 November 2012

Filezilla, Domain OS (Apollo), and Cygwin

What do these three things have in common? Well, there's a story behind it.

At a site where I work there are a couple of ancient Apollo workstations running Domain OS, a Unix-like OS. This OS had an early networked filesystem where the super-root is called // and hosts have their filesystems underneath this. E.g. if you have two workstations named ws1 and ws2, their roots will be at //ws1 and //ws2.

Users needed to connect to the workstation filesystem using FTP. I proposed Filezilla as a replacement for an older, less friendly client. A side note,  you need to use active mode FTP when connecting to this old FTP server. We could login fine and get a directory listing, but when we tried to enter a directory by clicking on the folder icon, we would get an error like: /ws1 not found.  Looking at the command stream it was obvious what was going wrong. Filezilla was issuing CWD /ws1 when it should be CWD //ws1.

How to get Filezilla to either 1. use relative paths for CWD, or 2. understand that the root is //, not /? There was no option to use relative paths, it seems that Filezilla always converts paths to absolute ones for CWDBy trial and error I discovered that Cygwin has by coincidence the same pathname convention, // is the super-root. So by setting the Server Type to Cygwin in the Site Manager entry for this Apollo workstation, Filezilla connections worked.

So that's the connection. Hope this tip helps you if you happen to have to connect to a Domain OS FTP server with Filezilla.