Ant bash completion on Mac OS X

bash will do tab-completion for ant targets on Debian/Ubuntu boxes out-of-the-box. If you haven’t upgraded lately, you may need to:

sudo apt-get install bash-completion

On Mac OS X, it needs a bit of massaging. First install the macports version of bash-completion and ant:

sudo port install bash-completion apache-ant

Then add this to the end of your ~/.bashrc:

if [ -f /opt/local/etc/bash_completion ]; then
. /opt/local/etc/bash_completion
fi
complete -C /opt/local/share/java/apache-ant/bin/complete-ant-cmd.pl ant

See http://marius.scurtescu.com/2005/03/23/ant_bash_completion for Windows instructions.

Secure VNC with ssh port forwarding

Need to help out a damsel in distress (username “damsel”) sitting on a remote debian/ubuntu box (“remotehost”)? Have you set up ssh on a non-standard port (port 12345) already? Great. Keep reading.

Step 1: Install x11vnc on the remote machine:

ssh remotehost
sudo apt-get install x11vnc

Step 2: Spin up x11vnc on the remote host:

ssh remotehost
sudo -u damsel x11vnc -noxdamage -speeds dsl -solid -display :0 -passwd SECRET

Keep this ssh running. -speeds dsl -solid makes vnc more responsive.

Step 3: Forward the remotehost’s vnc port, 5900, to your local host using ssh:

ssh -p 12345 -L 5900:127.0.0.1:5900 remotehost

Step 4: Start up your VNC viewer application, pointing to localhost.

Tightvnc is a great vncviewer and is installable through macports:

sudo port install tightvnc

Then run:

vncviewer localhost

Dealing with a directory with ~∞ files

Got a directory with > 10K of files? Need to move them up one directory? mv will fail you:

$ mv * ..
-bash: /bin/mv: Argument list too long

The solution is to list the files one line at a time (with find or ls -1) and feed that to xargs:

ls -1 | head -100 | xargs -I f mv f ..

This moves the first 100 files up one directory.

Note that this won’t work if you’ve got whitespace in your filenames. Use find -0 and xargs -0 to null-separate your filenames in that case.

Verifying file integrity with debsums

After upgrading my Ubuntu server, some security applications grumped about changed contents of some common binaries.

Just to be safe, I wanted to verify them explicitly with debsums, but debsums looks for package names, not paths to binaries. Here’s a script that validates chattr, find, perl, and lsattr–the “-s” option to debsums is “silent”, so no news is good news:

for i in /usr/bin/chattr /usr/bin/find /usr/bin/perl /usr/bin/lsattr ; do
  echo $i
  debsums -as $(dpkg -S $i | cut -d':' -f1 | sort -u)
done

Running a command for all files whose name matches…

I found a stray image named “img_1234.jpg” on a laptop and wanted to see if I already had it on my server.

On my Mac I could use spotlight’s nifty “kind:image” filter along with quicklook. Macworld has a great article about advanced spotlight usage.

On Ubuntu, it’s almost as easy:

locate -i img_1234.jpg | xargs -d'\n' feh -F -d
  • The locate -i says “find img_1234.jpg without case sensitivity. Locate likes to separate filenames (that might have spaces) with a newline.
  • The xargs -d'\n' says “expect filenames that are are separated by newline
  • The feh -F -d tells feh, a great little image viewer, to reduce the image to fit to the screen and draw the filename.

Installing VMware Player on Ubuntu Gutsy

Please note that these instructions are for Ubuntu 7.10. Newer versions of Ubuntu can just follow the normal installation instructions at http://www.vmware.com/download/player/.

So it turns out that the current release of Ubuntu (at least for another four days), “Gutsy Gibbon”, gave VMware Player the big cold shoulder and removed it as an installable package.

To top it off, the email-capture marketing application that VMware uses is broken, so you can’t download vmware-player through their website now. (How is this acceptable? Shame on both companies–eloqua for downtime with something as simple as a form capture, and vmware for not canceling their service).

Some sleuthing came up with the direct URL to the download for VMware Player 2.0.3.

While that downloads, go install the required packages:

sudo apt-get install \
  build-essential linux-headers-generic \
  linux-headers-$(uname -r)

Uncompress the archive:

sudo mkdir -p /opt/vmware-player
sudo chown $(whoami) /opt/vmware-player
cd /opt/vmware-player
tar xvzf ~/Desktop/VMware-player-2.0.3-80004.i386.tar.gz

Then start the install. I’d recommend not putting the binaries into /usr/bin (as this will be a non-.deb installation, and it will make the uninstalling easier):

sudo ./vmware-install.pl
...
In which directory do you want to install the binary files?
[/usr/bin] /opt/vmware-player/bin
...

From here on out, take the default values.

+1 for Apple

Night before last, I found bad blocks on my MacBook Pro. I dropped off the laptop at the Burlingame Apple Store (after making an “Apple Concierge” appointment, so no lines), and the Apple employee that took my laptop said that it would be ready in “3 to 5 days, but we’ll try our best”. That afternoon the laptop hard drive had been replaced and was ready for pickup.

When I turned on the mac, the clean install of Leopard asked me if I had a Time Machine backup to restore from, I plugged in the external drive, and 4 hours later (!!) the computer was back.

Bad Blocks Make Macs Unhappy

My fairly young MacBook Pro started randomly hanging, not coming out of sleep, and being generally disagreeable a couple days ago, and it turned out to be a bad hard disk.

What’s disconcerting is that Disk Utility.app didn’t see any problem with the disk. I had to install smartmontools to find the error.

After installing MacPorts, install smartmontools:

sudo port install smartmontools

Then tell the drive to do a long self-check in the background:

sudo smartctl -t long /dev/disk0

Note that this check may take an hour to run, but it’s done in the background, so you can continue to use your computer while it does its little dance on the catwalk.

Check the status of the test with:

sudo smartctl -c /dev/disk0

I was unlucky:

...
Self-test execution status: ( 121) The previous self-test completed having
  the read element of the test failed.
...

See The macosxhints forums for more discussion about this issue.

Maildir auto-archive

GPLv3
If you’ve got your mail sitting on some server and in Maildir format, and you’ve used Outlook’s “Auto Archive” feature, you might wish that your inbox (and subdirectory contents) could be automatically swept clean of items older than, say, 3 weeks, and shoved into a Year/Quarter sub folder (like “Inbox/2008/Q1/”).

A couple years ago I wanted this too. So I wrote a cronjob and perl script to make this happen.

Installation is straightforward:

  1. Copy the shell script that cron calls, autoarchive, and the perl script, autoarchive.pl, onto the server holding your Maildir.
  2. Edit the autoarchive shellscript to make sure the path and –max-days is ok with you.
  3. Make both scripts executable with chmod u+x
  4. Make a backup of your mail. This is GPL code. No warranty is implied. Read the code and try it out with –dry-run first.
  5. Calling autoarchive.pl with the “–dry-run” option will let you make sure it’s doing what you want it to do. If it does, delete the dry-run.
  6. Wire it up to cron with something like:
    PATH=$HOME/bin:/usr/bin:/bin
    42 10 * * * autoarchive > /dev/null