Mac OS X: Replicating md5sum Output Format

The md5sum program is used to calculate and verify 128-bit MD5 hashes. This program is installed by default in most Unix, Linux, and Unix-like operating systems including BSD. Mac OS X is a BSD variant and it also includes the md5sum program. However, the program is called md5 instead of md5sum and outputs an MD5 checksum in a different format than the standard md5sum program.

Here's what the standard md5sum output looks like:

$ md5sum test.txt
d0ea20794ab78114230ba1ab167a22c2 test.txt

Now here's what the output of md5 on Mac OS X looks like:

$ md5 test.txt
MD5 (test.txt) = d0ea20794ab78114230ba1ab167a22c2

While this normally wouldn't be a big deal, it can cause major issues if you're trying to run scripts that were written for a Unix-like environment which expect the default md5sum format.

Thankfully, md5 has a switch that reverses the output:

$ md5 -r test.txt
d0ea20794ab78114230ba1ab167a22c2 test.txt

If you'd like to permanently change md5's behavior to mimic that of md5sum, you have two options:

The first is to simply add the following alias to ~/.profile:

alias md5sum='md5 -r'

Now when you type 'md5sum test.txt', the command will be replaced with 'md5 -r test.txt'. However, this may not work with your scripts.

The second solution, and my preferred method, is to create a small script called md5sum that contains the following:

#!/bin/bash
/sbin/md5 -r "[email protected]"

I then make this script executable (chmod +x md5sum) and put it in /sbin/. Now, whenever a script calls md5sum, the small bash script above is used and it produces output identical to that of md5sum on other Unix systems.

Mac OS X: Fixing 'Always Open With'

After installing the TextMate editor I wanted all shell scripts (files ending with the .sh extension) to open in TextMate. Unfortunately, selecting Open With -> Other... from the context menu, choosing an application, and then clicking the Always Open With checkbox doesn't change the default application for all files with that extension, but rather only changes that specific file.

If you want to change the default application used to open all files with a specific extension, the steps are slightly different:

Select the file and choose File -> Get Info (or Cmd+i) and expand the Open With section:

Get Info

From here, select the application you want to use for opening those file types and then click Change All. This will update the OS X Launch Services Database, which is consulted when opening files. Now all files with that extension will be opened with the application you selected.

Mac OS X: Resizing Oversized Windows to Fit the Screen

I recently switched from a 15" MacBook Pro (2007 model) to the latest 13" MacBook Pro (2009, unibody model). After transferring my iTunes library to the new laptop, I found that the size of the iTunes window exceeded the size of the screen (the 15” MBP had a 1440x900, while the 13” MBP has a slightly smaller 1280x800). This meant I was unable to grab the bottom right corner of the window to resize it:

iTunes too large to resize!

Unfortunately, the maximize button in iTunes only cycles through Previous Size and the Mini Player mode; at no point does it actually maximize the window to fit the screen!

Note: As Ondrej commented below, you might be able to fix this by simply holding down the Alt/Option key and pressing the zoom/maximize button (the green button).

I tried installing an application called RightZoom which changes the behavior of the maximize button in OS X by maximizing the window to fill the screen (as opposed to simply maximizing for “best fit”). However, that did nothing to change the behavior of iTunes.

After searching the web for solutions and finding nothing, I remembered a trick I had used in the past on Windows computers: Change the display resolution to something smaller, thereby forcing the operating system to resize windows to fit the new resolution. Then, after the OS resizes the windows, change the resolution back to the original. Of course this only works if the original resolution is not already set for the lowest one (it most likely isn't).

To my relief, this trick worked perfectly! When I switched to a smaller resolution, the iTunes window was resized to fit the smaller screen.

For your reference, here are the steps:

  1. Launch System Preferences -> Displays
  2. Select a smaller resolution
  3. iTunes (or any other application) will be resized to fit the smaller resolution
  4. Change the resolution back to the default
  5. Oversized window problems solved!

Lazy Linux: 10 Essential tricks for admins

Lazy Linux: 10 Essential tricks for admins is an awesome list of cool things you can do with Linux. I learned about trick #3 (collaboration with screen) from an admin at the datacenter where one of my servers is hosted. I remember being thrilled sitting there watching him do stuff on my server while sharing the keyboard to type messages back and forth in vi (think Remote Desktop or VNC, but on the console). Trick #5 (SSH back door) is something I've been using for years at work for remote diagnostics. It is an invaluable trick for getting around firewalls. Very cool stuff!

Instantly Preview Files in OS X using Quick Look

I discovered this feature as I discover many new things in OS X: entirely by accident. While I had a file on my desktop selected, my wrist accidentally hit the space bar on the keyboard. Instantly a quick preview window popped up giving me a preview of the image:

Mac OS X Quick Look

The feature, known as Quick Look, is one of the more than 300 new features in OS X Leopard (10.5). Quick Look also works on text files, documents, PDF files, and even HTML and PowerPoint. This method of previewing files is so much easier than opening them in their native application! (It's more than a preview though, since you can also scroll through the pages for multi-page documents.) Closing the preview window is as easy as pressing Esc, or you can simply select another file with the preview open and it will preview the newly selected file.

HOWTO: Show the Full Path in Finder's Title

An annoying feature of the OS X Finder is that it doesn't show the full path of the folder you're currently browsing -- it only shows the name of the folder in the title. I like seeing the entire path of the folder because I do a lot of stuff from the command line, so as you can imagine I was thrilled to discover an easy fix to this problem.

Simply open the terminal (Applications -> Utilities -> Terminal.app) and run the following command:

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES

You'll then need to restart Finder by either rebooting or running the killall Finder command. Now Finder will show the full path in the title!

Path in Finder's Title

If for some reason you want to revert back to seeing only the folder name (I can't imagine why you would), simply repeat the process and change YES to NO at the end of the command.

Google Reported Attack Site

Google Reported Attack Site

I'm sure some of you must have seen this warning when you tried to visit my site. Fear not, I have fixed the problem. There was an old file on my domain that had a link to a site that was defined as "malicious" by Google, so they basically added my entire domain to the watch list. I removed the file and, after asking Google to check my site again using Google's Webmaster Tools, they removed my domain from the list.

So, how did I find the few pages (among thousands of files on my site) that contained a link to the malicious site Google was blocking me for? I logged into my site via SSH and ran a command like the following:

for i in `find . -name "*.ht*"` ; do echo $i; cat $i | grep 195.2.252; done

This basically searched every single .htm or .html file inside my public_html directory and returned anything that contained the IP address I was looking for. Whenever there was a match, the filename that preceded the output was the offending file. I'm sure there's a more elegant way of doing this, but hell, I just wanted to fix the problem!

Although this was annoying to deal with, it made me feel good that Google is actually keeping track of these things and, with the help of Firefox, is warning people of such sites. Site owners must be vigilant in fixing such problems or they risk losing loads of traffic from Google (and from visitors with Firefox).

Escaping Filename or Directory Spaces for rsync

To rsync a file or directory that contains spaces, you must escape both the remote shell and the local shell. I tried doing one or the other and it never worked. Now I know that I need to do both!

So let's say I'm trying to rsync a remote directory with my local machine and the remote directory contains a space (oh so unfortunately common with Windows files). Here's what the command should look like:

rsync '[email protected]:/path/with spaces/' /local/path/

The single quotes are used to escape the space for my local shell and the forward-slash is used to escape the remote shell.

iCal Missing AM/PM Option? Change to 24-hour Mode!

Am I crazy or is there no way to make iCal show AM/PM when editing a calendar entry?

iCal Missing AM/PM Option

The AM/PM option IS there, because if I use the arrow key to move over to next field, there's a hidden AM/PM field which I can change. However, unless I know what it's already set to, I have no way of knowing what I'm changing it to. Very annoying.

My fix for this is to simply use 24-hour mode, which I already use on my main system clock anyway. (By the way, if you have trouble converting 24-hour time to 12-hour time, you might want to check out my post on Learning to Interpret Military Time.) Unfortunately, enabling the "Use a 24-hour clock" option in the Date & Time settings does not enable 24-hour clock mode in iCal. To do this, you must modify the International settings (!?).

System Preferences -> International -> Formats -> Times -> Customize

Changing time format

From here, you can select and delete the AM/PM option from the white field and then click the little arrow on the hour field to change it's format to 24-hour (this screen is very non-intuitive for something made by Apple). I made the changes to Short, Medium, Long, and Full time formats and then clicked OK and WHAM! iCal showed 24-hour time and made it possible to easily change the time on an entry.

iCal 24-hour format

Uppercase table tags create extra whitespace in WordPress

When creating tables inside a WordPress post, using uppercase TABLE and/or TR tags causes additional whitespace to appear above the table. I say and/or TR tags because uppercase TABLE tags alone don't seem to create additional whitespace, however uppercase TR tags do. If you combine the use of uppercase TABLE and TR tags together, they create even more whitespace!

First, lets see what normal output looks like:

[html]
This is a simple sentence used to demonstrate an odd spacing effect when uppercase TABLE and TR tags are used in a table.

[/html]

WordPress output:

And now, if we copy the exact same content and make the TABLE and TR tags uppercase, look what happens:

[html]
This is a simple sentence used to demonstrate an odd spacing effect when uppercase TABLE and TR tags are used in a table.

[/html]

WordPress output:

I'm running the latest version of WordPress (v2.3.1 as of this post) and I have confirmed this oddity appears on multiple platforms (Mac OS X 10.4.11, Windows XP SP2) with multiple browsers (Firefox 2.0.0.11 on Windows and OS X, Internet Explorer 6.0.29 and Opera 9.22 on Windows, and Safari 3.0.4 on OS X).

Upon closer inspection of the actual output produced by WordPress in my browser (using view source), I discover the problem:

Browser output of the lowercase example:

[html]

This is a simple sentence used to demonstrate an odd spacing effect when uppercase TABLE and TR tags are used in a table.

[/html]

Browser output of the uppercase example:

[html]

This is a simple sentence used to demonstrate an odd spacing effect when uppercase TABLE and TR tags are used in a table.

[/html]

It appears that WordPress is looking for the <table> and <tr> tags and when it finds them it doesn't automatically add <br /> and <p> tags around them like it does with the rest of your post. (I don't use the visual editor, so I'm able to easily add hand-written HTML directly to my post.) However, the search for <table> and <tr> must be case sensitive which causes problems if your tags are anything but lowercase.

I would normally report this as a bug to WordPress, but I'm sure the reply will be that all XHTML must be lowercase and so the problem is not relevant. I say that's bad backwards compatibility. 😐

Recovering from CTRL+S in Putty

Every once in awhile, I'll press CTRL+S by accident while I'm inside a terminal window. For the longest time, this simple accidental keystroke meant I had reconnect to my Linux server, kill whatever program I was running, and then start it again. Eventually I got sick of this happening and decided to do what I should have done in the first place: Google It.

Apparently CTRL+S actually does XOFF, which means the terminal will accept key strokes but won't show the output of anything. It will appear as if your terminal is dead when it's really just waiting to be turned back on. The fix? Simply press CTRL+Q to turn flow-control on (XON). If you pressed a whole bunch of keys before pressing CTRL+Q, you'll see the output from those keystrokes.

In the Windows world, CTRL+S is used as the Save command. Over the years, I've developed the habit of pressing CTRL+S every few minutes while working on a document, simply because I've had too much work lost from stupid errors. Thankfully, this habit will no longer get in my way of working in the Linux world.

Update: A commenter suggested a way to disable CTRL+S from sending XOFF altogether:

add this to your .bashrc (man stty for more options):
stty ixany
stty ixoff -ixon

Update: A commenter provided a tip for making CTRL+S actually save the file in Putty'd VIM.

Recursively Renaming Multiple Files

I needed to rename a bunch of files for a customer at work the other day -- more than 60 files. The files were on a Linux system, so I knew there was an easy way of doing it. A few days ago I used perl to search and replace a piece of text in a several files , so I decided to find a way to do it with perl.

I found the following script on this site:

[perl]
#!/usr/local/bin/perl
#
# Usage: rename perlexpr [files]

($regexp = shift @ARGV) || die "Usage: rename perlexpr [filenames]n";

if ([email protected]) {
@ARGV = ;
chomp(@ARGV);
}

foreach $_ (@ARGV) {
$old_name = $_;
eval $regexp;
die [email protected] if [email protected];
rename($old_name, $_) unless $old_name eq $_;
}

exit(0);
[/perl]

After saving the script to a file called rename (and chmod 755'ing it) I was able to run the following command to change the file extension on all .JPG files from uppercase to lowercase .jpg. To search for all files underneath a particular directory, I used the find command and piped it's output to the rename script:

find /home/customername/content/images/ | rename 's/JPG$/jpg/'

A few seconds later and all the files were renamed! This script is incredibly versatile, as you can pass it any regular expression! A few quirks I found were that you cannot reference the script; you must use it from the directory you stored it in (find / | ~/rename 's/JPG$/jpg/' won't work). This is because the script uses itself (on line 17). This also means if you save the script as something other than rename, you must also modify line 17 of the script.

Easily Replace all Occurrences of a String in Multiple Files Using Linux

This came in handy when I needed to change the IP address used in several links on a customer's site. I simply specified the path to all the web files, and ran a command similar to the one below.

find /path/to/directory -name "*.txt" | xargs perl -pi -e 's/stringtoreplace/replacementstring/g'

Note that you can change *.txt to just * to search inside all files. Also keep in mind the replacement command will be completed recursively (files nested inside directories will also be searched).