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 "$@"

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.

Unix/Linux: Finding and Killing Processes by User

If you've ever run ps aux | grep user to list processes and hunt for process IDs, you'll be happy to know there is a simpler alternative. Both finding and killing processes owned by a particular user is made simple using the handy pgrep and pkill utilities.

Listing Processes with pgrep

Listing all the processes owned by the user raam can be done like this (the -l switch causes the output to include the process name):

[bash]
$ pgrep -l -u raam
9614 screen
9628 bash
9644 irssi
16165 bash
16297 rtorrent
19462 ssh
19515 bash
19526 ssh
20964 sshd
[/bash]

You can also filter the list of results by appending a full (or partial) process name to the command:

[bash]
$ pgrep -l -u raam bash
9628 bash
16165 bash
19515 bash
[/bash]

Killing Processes with pkill

The pkill command does basically the same thing as pgrep, except it kills the processes instead of listing them. This is useful if you have a user with several dead processes, or if you were deleting a user and you wanted to kill any running processes first.

Killing all the processes owned by the user raam looks like this:

[bash]
$ pkill -u raam
[/bash]

And once again, if you only wanted to kill all the bash processes owned by raam, you would append the process name to the command:

[bash]
$ pkill -u raam bash
[/bash]

As always, check the man pages for pgrep and pkill for more information and switch options.