Cygwin BASH function to open the latest version of a document

One handy function I’ve added to .bashrc (so it is always available) under Cygwin (the LINUX command environment for Windows) works out the current working version of a document. It assumes that you keep copies that have a version number or date in the file name that will sort correctly.

# Opens the latest version of a file using the Windows default application
# Assumes that you have a range of files that can be identified using some for of prefix
#   and that the last part of the file contains a version number or date that sorts in the correct order
#   e.g. myfile-lots-of-rubbish-20090720-01.doc & myfile-lots-of-rubbish-20090723.01.doc
# Two arguments are required. The first is the PATH to search in. The 2nd is the shared file prefix (e.g. 'myfile-lots-of-rubbish-')
#   Put single quotes around the arguments to prevent them from being GLOBed by the shell.
# Only searches in the GIVEN path, not subfolders.
# Use with an ALIAS to have an easy way of opening a specific file from the shell
function cyOpenLatest {
    # We have to use find rather than ls because of shell expansion issues in the arguments (and problems with spaces in file/folder names)
    res=`find "$1" -iname "${2}*" -type f -prune -printf '%f\n' | sort -r | head -1`
    # Work out the file type from the .ext
    ext=`echo $res | sed "s/.*\.//"`
    # Add whatever you want to this list above the *)
    case $ext in
        doc*) TYPE="in Word" ;;
        xls*) TYPE="in Excel" ;;
        ppt*) TYPE="in PowerPoint" ;;
        vsd*) TYPE="in Visio" ;;
        *) TYPE="with Windows default application"
    esac
    if [ "$res" != "" ]; then
        echo "Opening [$res] $TYPE"
        cygstart "$res"
    fi
}

You can use it with an alias like this:

alias gic="cyOpenLatest '$HOME/Documents/Here is a folder with a space or two/' 'a-document-'"

If you name your documents sensibly such as “a-document-2009-07-20.doc” or “a-document-v01.01.doc”, then the latest version of the file will be opened in the default application

Technorati :
Diigo Tag Search :

Speeding up Cygwin

Yesterday I mentioned my success with Cygwin.

One issue I did have though was with the speed of startup. It was taking 15-20 seconds to start a BASH shell.

It turns out that this was a PATH issue. I went through my Windows PATH and cleared out the clutter. Now it takes just around 3-4 seconds for a full BASH login and less still for just running a script.

I now find myself using the BASH shell for all sorts of things and I’ve set up a number of alias’s to switch to folders I’m using a lot and to open common documents.

One handy function I’ve added to .bashrc (so it is always available) works out the current working version of a document. It assumes that you keep copies that have a version number or date in the file name that will sort correctly.

You can find the code on my development blog.

Here are a few more alias’s I use:

alias np='cygstart "/cygdrive/c/Program Files/Notepad++/notepad++.exe"'
alias c='cd /cygdrive/c/'
alias d='cd /cygdrive/d/'
alias work='cd "$HOME/Documents/Workdocs/"'
alias pers='cd "$HOME/Documents/Persdocs/"'
alias facebook='http://www.facebook.com'

Windows command prompt vs PowerShell vs Cygwin for remote backup scripts

I’ve been struggling with trying to get a new backup routine working for my Laptop.

I should point out that I have several complex requirements for backup so my needs are probably not average.

However, it really shouldn’t be this hard!

I need to use a combination of BZR (Bazaar) for document version control and RSYNC (for files that don’t need version control and for those folders that might contain files too big for version control systems – around 1/3 to 1/2 available memory).

All of the backups need to happen over a secure link since I am often outside of my home network – indeed quite often behind locked-down enterprise firewalls but that’s a story for another time. So I use SSH (Secure SHell) to manage the secure connection and transmission. Thankfully both BZR and RSYNC can both use SSH as a transport.

I don’t want to have to enter my remote system password loads of times though and this is where things started to get annoying. Using the Windows native versions of BZR, RSYNC and SSH I could not get a single shared password to work no matter what I tried.

I also had some problems trying to control the output from the various tools and use it to further control what happens next – for example getting an IP address and working out whether I am on a network and where that network is.

I tried to do this with a Windows command script first but even with the Windows 7 extensions it really is far to hard to get anything useful done and when I found myself turning to more and more utilities to help I thought “enough is enough”.

At that point I happened to be reading an article on Windows PowerShell, the .NET scripting host so I thought I’d give it another go (having tried it before). I soon found that, although powerful for controlling the WMI interface, it is desperately convoluted and annoying for general use.

So, realising that most of the tools I wanted to use have their roots in the UNIX world, it would make sense to try out the latest version of Cygwin. This has really come on a long way since it’s early days and is far more mature. It is also very much lighter in weight than the Microsoft provided UNIX services for Windows or whatever they are currently calling it. The Microsoft provided tools load perhaps a dozen services into memory permanently though they are rarely required. Cygwin only uses memory when it needs to.

After converting my backup script from Windows batch to a BASH script under Cygwin, I soon had everything working as I wanted it – including the seemingly intractable problem of the shared passwords, now using KEYCHAIN to manage the SSH-AGENT and keys. So now I only need to supply a password once, it is held reasonably securely in memory and used by SSH as and when required. I only need to enter it once per reboot.

The full script not only backs things up, it also auto-commits changes to BZR and changes network settings to match my current location and proxy server requirements. The later is not yet converted from batch as I don’t need it just at the moment.

Let me know if you are interested in a copy of the script and I’ll upload it somewhere.