Rcjp's Weblog

February 1, 2007


Filed under: python, unix — rcjp @ 11:23 am

I hate globbing. Whoever thought that it was a good idea to let the shell snaffle the arguments you’ve given to a program needs to be pushed off somewhere high onto some burning cactii.

You can turn it off in the shell, but this makes most unix commands useless since they don’t do the file expansion themselves (like they should).

In ruby…

    for f in Dir.glob("/home/r/tmp/**/*")
        File.open(f) { |file|
            puts "filename #{f}"
        } if File.file?(f)


    import glob, os

    for root, dirs, files in os.walk('/home/r/tmp'):
        for f in files: print 'file -> ', os.path.join(root,f)

The Common Lisp pathname stuff can be a bit confusing, which is a pity because it does have some nice tricks up its sleeve.

You build the directory section of the pathname as a list taking keyword arguments :wild where you want to match at one directory level and :wild-inferiors when you want to recurse.

CL-USER> (make-pathname :name :wild
                         :type :wild
                         :directory (list :absolute "home" "r" "tmp" :wild-inferiors))

so running (DIRECTORY #P"/home/r/tmp/**/*.*") should give you all the files in the tmp directory and below. Some compilers though take directories themselves to be files and others don’t, so you might need to use FILE-NAMESTRING to see if the filename part of the path is nil i.e. its a directory.

January 7, 2007

Renaming Groups of Files

Filed under: python, unix — rcjp @ 4:13 pm

I have done it in the times in the past, but I always forget the-right-way because different unix shells do loops differently etc, so I now always use ipython to e.g. lowercase all filenames in a directory…

    In [14]: import os

    In [15]: !! ls

    In [16]: for f in _15: os.rename(f, f.lower())

and for doing that recursively of course just do

    !! find . -type f

More nvi tricks

Filed under: unix — rcjp @ 2:31 pm

Came across Garrett Hildebrand’s nice collection of vi tricks including a cracking one to insert a previous occurring word – handy for long function names etc.

September 5, 2006

Backup script

Filed under: python, unix, utils — rcjp @ 12:45 pm

Just a handy bit of code to backup a file.

#!/usr/bin/env python

import os, sys
from shutil import copyfile

def backup(filename):
        i = 1
        while os.path.exists('%s.old%d' % (filename,i)):
        backup = '%s.old%d' % (filename,i)
        print "Backing up %s to %s\n" % (filename, backup)
        copyfile(filename, backup)

if len(sys.argv) != 2:
        print "Usage: backup filename"
        except IOError, val:
                print "*** Backup Failed : %s ***\n" % val

September 1, 2006

FreeBSD palm-sync

Filed under: unix — rcjp @ 2:08 pm

Finally got round to thinking about syncing my palm with FreeBSD rather than having to reboot into Windows. So far, I like it much better than the graphical system I was using and it was suprisingly easy to set up. Just made sure I had the pilot-link port installed and added a file /usr/local/etc/devd/palm.conf containing

# Palm Pilot
attach 10 {
    device-name "ucom[0-9]+";
    // matches Palm Pilot but not Handspring etc. see /usr/src/sys/dev/usb/usbdevs
    match "vendor" "0x0830";
    action "/bin/ln -s $device-name /dev/pilot";
    action "/bin/chmod 0666 /dev/pilot";
detach 10 {
    device-name "ucom[0-9]+";
    action "rm -f /dev/pilot";

(I could have done something similar in /etc/usbd.conf but I think that is depricated) then just restart devd /etc/rc.d/devd restart and pressing the hotsync button gives me a /dev/pilot device for something like pilot-xfer -b ~/backup to backup my palm.

June 2, 2006

Using a shared lib from cmucl

Filed under: c, lisp, unix — rcjp @ 1:24 pm

library code mylib2.c

#include <stdio.h>

int mytest()
    printf("inside c lib !!!\n");
    return 2;


(defun fact (n)
  (if (= n 1)
      (* n (fact (- n 1)))))

(ext:load-foreign "/usr/local/lib/libmylib2.so.1")
(alien:alien-funcall (alien:extern-alien "mytest" (function c-call:void) ))


#include <stdio.h>

int main()
    printf("inside main program\n");
    printf("back inside main program\n");

build using

cc -fPIC -c mylib2.c
gcc -shared -Wl,-soname,libmylib2.so.1 -o libmylib2.so.1   mylib2.o
# now use the lib
# need to copy libmylib2.so.1 to /usr/local/lib or somewhere
# then as root do 'ldconfig' 
cc test2.c -L. -lmylib2.so.1 -o test2

Using an archive library

Filed under: c, unix — rcjp @ 1:19 pm

main program that calls into the archive test1.c

#include <stdio.h>

int main()
    printf("inside main program\n");
    printf("back inside main program\n");

the code in the archive mylib1.c

#include <stdio.h>

int mytest()
    printf("inside c lib !!!\n");
    return 2;

build with the script

cc -c mylib1.c
ar -cvq mylib1.a mylib1.o
# create a symbol table to link with
ranlib mylib1.a
# now use the lib
cc test1.c mylib1.a -o test1

February 20, 2006

Printing with lpr

Filed under: unix — rcjp @ 2:20 pm

Setup simple printing for my faithful (but crinkly paper heating noisy) oki printer. Just uncommented the lp section in /etc/printcap file and started the lpd daemon.

That wasn’t quite it though, printing a test with lptest 20 5 | lpr the text staircased across the page. Fortunately the solution was simple, just copied /usr/share/examples/hpif to /usr/local/libexec/if-oki chmod’ed it 555 and added an ‘input filter’ section to the printcap so
it now looks like

lp|local line printer:\\

It would have been a two minute job if I hadn’t missed that I’d put a space after the 'if=' in the printcap entry, and spent twenty minutes scratching my head. Hmmm, I wish unix wasn’t so sensitive to such things.

Finally I added some ESC codes to the input filter to set the CPI to 15…

printf "\33&k2G33(s0p15h0s3b4102T" && cat && printf "\\f" && exit 0
exit 2

…now lpr filename all works nicely.

January 18, 2006

FreeBSD – my main system

Filed under: unix — rcjp @ 1:24 pm

I seem to have finally gone over to FreeBSD for everything after many years of dual booting with Window – but there are still a few things to test e.g. completely finish the palm back… burning CD/DVD’s etc. But hopefully it won’t cause any problems.

Create a free website or blog at WordPress.com.