Securely deleting files

I needed to switch cellphones, and given that my cell’s SD card had sensitive data, just formatting the card wasn’t sufficient — it’s trivial to recover files from high-level-formatted FAT file system.

I present to you the world’s most dangerous (unix) command:

 find /path/to/mounted/SDcard -type f -print0 | xargs -0 shred -z -u

(shred is part of the coreutils package, so it should be installed already)

HOWTO: simulate the cron environment

While banging my head on RVM + Rails 3 + crontabs, it became clear that I needed the cron environment in an interactive shell. It’s not hard:

sudo su
env -i /bin/sh

If your SHELL in your crontab is /bin/bash:

sudo su
env -i /bin/bash --noprofile --norc

Remember that the cron-invoked shell won’t have a tty, so some commands will behave differently. Also, if you’ve got a bunch of other environment variables set up in your crontab, you can do this trick from stackoverflow — create a one-off cron that writes env to a file, and load that env later.

HOWTO: Mount your USB hard drives at boot time on Ubuntu

I’ve got a number of external USB hard drives connected to my ubuntu server that need to mount to a predictable directory.

When you log into Gnome, the desktop environment does it’s nifty thing and mounts any drive you’ve got plugged in — but if the box reboots, the drives won’t be mounted until the next person logs into the computer.

I needed something that happens at boot time to do this task.

Continue reading

Set up JAVA_HOME to track Java Preferences.app on Mac OS X

/Applications/Utilities/Java Preferences.appMac OS X’s Java Preferences.app has a pane for switching between versions of the JDK, but I just found out from a coworker (thanks, Mike!) that you can make your shell match that preference easily — just add this to your ~.bashrc:

export JAVA_HOME=$(/usr/libexec/java_home)

If you change your JDK priority preference, you’ll need to re-source your ~/.bashrc or just open a new terminal window.

How to Convert Your Book’s Images to Kindle

Taking your painstakingly typeset book and shoving it through the kindle “conversion” meatgrinder was an exercise in wincing. Most of the images were corrupted, there was whitespace sprinkled randomly throughout the copy, and it was a general mess.

Kindle supports direct upload of an html version of your book, but there’s a lot of finessing you need to do before it all goes smoothly. One of the tasks you’ll need to do is convert your book’s images to greyscale, and reduce their size to something Kindle-friendly. There are free tools to help you do this if you aren’t afraid of the terminal.

Continue reading

Using Mac OS X 10.5′s keychain for ssh

The version of ssh that comes with Mac OS X 10.5.6 has a -K option that stores your passphrases in your system’s keychain.

Run this:

ssh-add -K [path to private keyfile]

Provide your passphrase once when asked, and keychain will provide the passphrase for you automatically. You should probably enable “Require password to wake this computer from sleep or screen saver” in the Security pane of the System Preferences if you decide to do this.

If you see

$ ssh-add -K
ssh-add: illegal option -- K

it’s because you’re using the macports (or fink) version of ssh. (run ‘which ssh’ to find out). With macports, uninstall the “openssh” package:

sudo port uninstall openssh

The ‘-K’ option was discovered courtesy of http://www-uxsup.csx.cam.ac.uk/~aia21/osx/leopard-ssh.html.

http://kimmo.suominen.com/docs/ssh/ has some excellent ssh documentation.

Make “ps -ef” work in a shell on Mac OS X

If you’re used to SunOS or BSD, you’ll be at home with Mac OS X’s “ps -aux” to get a process list from a shell prompt.

If you’ve been using any other recent unix, though, your fingers will want to type ps -ef instead. Rather than hack an alias to wrap ps to make this happen, it turns out there’s an easy way to return to the ps promised lands.

By default on Mac OS X 10.5.2, the shell environment’s COMMAND_MODE is set to legacy. If you set it to unix2003, you’ll get your ps -ef. Just add

export COMMAND_MODE=unix2003
alias zcat='gunzip -c'

to your ~/.bashrc to make it be set automatically.

The alias of zcat to gunzip -c fixes a “feature” in unix2003 mode — it removes gzip support from zcat. If you’re used to using zcat for both compressed .Z files as well as gzipped .gz files, you want the alias line as a workaround.

Preventing an external hard drive from idling on ubuntu

I got a Seagate FreeAgent Pro external hard drive for backups (JWZ has a very straightforward article about this). It happily reformatted to ext3, and I kicked off an rsync of /home.

Because rsync figures out what files need copying before it copies them, and there are hundreds of thousands of files in my /home, there was more than a couple minutes of grinding on the local hard drive building a list of files to copy over. While this happened, the external drive idled into a “sleep” mode that ubuntu can’t seem to awaken it from.

This was slashdotted with an sdparm hack, but I believe this solution is better. Copy this new udev rule into /etc/udev/rules.d/50-local.rules (this is a new file that you will be creating):

# Seagate FreeAgent allow_restart fix (i/o errors)
SUBSYSTEMS=="scsi",DRIVERS=="sd",ATTRS{vendor}=="Seagate*",ATTRS{model}=="FreeAgent*",RUN+="/bin/sh -c 'echo 1 > /sys/class/scsi_disk/%k/allow_restart'"

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.