Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Install and configure Tripwire from source UNIX
Tripwire is a set of open-source Unix command line utilities, spun off by the company of the same name that sells a more-capable commercial prodct; you can use it to verify the integrity of your system files, detect intrusions, and monitor what files get added or changed by your computer's software processes. For the slightly paranoid, it can have a calming effect.

Fortunately, it is relatively easy to run under OS X. Installing it, however, can be another story. Back in 2003, a member named frodo published a hint here on how to install Tripwire with a precompiled package that he had developed. Sadly, his website is no longer operational, so Mac OS X users who wish to use Tripwire have to muddle through the generic installation process for Unix boxes. This can be quite confusing, so I thought that it would be useful to document it in a step-by-step fashion. The following is based on the sources currently available.

The first step is to install the XCode tools so that you can perform compilation of the source; you don't need the latest and greatest, so you can simply install XCode from the CD or DVD that has your operating system install on it. Alternatively, you can join the Apple Developer Connection (free) and download XCode.

Next you need the source for Tripwire. Paul Herman's web site still has the portable Tripwire tarball available, so download tripwire-portable-0.9.tar.gz from there. Move the downloaded file to a convenient place (like Documents) in your user's folder hierarchy, and then double-click the downloaded file; it should expand into a folder of sources.
read more (1,036 words)   Post a comment  •  Comments (9)  
  • Currently 2.00 / 5
  You rated: 1 / 5 (14 votes cast)
[18,756 views]  View Printable Version
Use an 'rmdir' function that's smart about .DS_Store files UNIX
Ever wish that 'rmdir' was smart enough that it would delete a directory if it was empty except for a .DS_Store file?

I wrote a zsh function which will replace rmdir and do exactly that. It is also available as a bash script, which you can rename to rmdir and drop in your $PATH.

The code is thoroughly documented for anyone who wants to see how it works, but the executive summary is this:
  1. Take a bunch of arguments
  2. Check to see if those arguments translate to directories
  3. If they do, check to see if the directory is emptied by /bin/rmdir
  4. If /bin/rmdir fails, check to see if the only file in the folder is .DS_Store
  5. If yes, delete the file and re-run /bin/rmdir on the folder
  6. If no, just report the directory is not empty
[robg adds: I've duplicated both files here on Mac OS X Hints, in case the source ever goes away: zsh function | bash script. I have not tested these myself.]
  Post a comment  •  Comments (10)  
  • Currently 1.76 / 5
  You rated: 2 / 5 (21 votes cast)
[8,793 views]  View Printable Version
A simple way to create PDFs from the command line UNIX
I have used many different methods over the years to print documents to PDFs from the command line. Some have been complicated sequences of pipes to and from groff, others required TeX, and occasionally I set up a "virtual" printer, simply to print to file. I recently read documentation for cups-pdf, however, and found that cupsfilter command is sufficient for most of my own tasks in its bare form!

For example, to print 80-column ASCII plaintext (the majority of my code), I can use this:
$ cupsfilter foo.txt > foo.pdf 
If you find the output of that command a bit verbose (as I do), you can send the errors silently to the null device using this version:
$ cupsfilter foo.txt > foo.pdf 2> /dev/null
There are many ways to wrap this simple command even more conveniently, but I'll omit those here for now. The reason this method is ideal is because it uses built-in routines in OS X; any time you can take advantage of these, do, because many of the core technologies are significantly faster and more secure than third-party alternatives.

Of course, your mileage may vary :-).

[robg adds: cupsfilter PDF conversions work in at least 10.5 and 10.6, and I suspect in earlier OS X releases.]
  Post a comment  •  Comments (10)  
  • Currently 3.16 / 5
  You rated: 2 / 5 (25 votes cast)
[17,907 views]  View Printable Version
Back up OS X to a Windows machine on the network UNIX
I know there are plenty of back up/synchronization packages for the Mac, and I know Time Machine is awesome. But as I have a Linux/FreeBSD background, I wanted to do it my way. And none of the software I tried was able to do it my way :-). So the command line seemed to be a good solution.

My office Windows machine has plenty of disk space on it, and it is rarely used. It was an ideal system (with not so ideal a file system) to do backups on. The solution I came up with requires:
  • A shared folder on the PC, mounted on the Mac.
  • The Unix app rdiff-backup, available via MacPorts.
  • Notifications are sent using growlnotify, part of the Growl notification system.
The final step was to write a custom shell script to back up my Mac to the mounted Windows shared folder; here's what that looks like:
# my backup script in /Users/mkljun/

SourceIP=`ifconfig | sed -n '/en0/,/media/p' | grep -v inet6 | grep inet | awk '{print $2}'`

## delete a log file if bigger than 50M                                                                                                     
# check if the file exists otherwise create it                                                                                              
if [ ! -f $LOGFILE ]; then `touch $LOGFILE`; fi
LOGSIZE=`ls -l $LOGFILE | awk '{print $5}'`
echo "Size of $LOGFILE = $LOGSIZE bytes."  >> $LOGFILE 2>&1
if [ $LOGSIZE -gt 50000000 ]; then rm $LOGFILE; fi

echo "************************************************************" >> $LOGFILE 2>&1
date >> $LOGFILE 2>&1
echo "************************************************************" >> $LOGFILE 2>&1
echo "Pinging destination machine $DestinationIP:" >> $LOGFILE 2>&1
# ping destination (backup) ip address; output redirected to a log file
ping -c 1 $DestinationIP >> $LOGFILE 2>&1
# check if ping successful
if [ $? != 0 ]
    # if ping command not successful, print notification (echo should be in one line!!)
    echo "-t 'Backup failed' -m 'Your IP is $SourceIP. Backup PC $DestinationIP could not be reached'" | xargs /usr/local/bin/growlnotify
    echo "Pinging failed" >> $LOGFILE 2>&1
    # mount samba drive (it should be one line)
    osascript -e "try" -e "mount volume \"$SambaDrive\"" -e "end try" > /dev/null 2<&1
    # check if mount was successful
    if [ $? == 0 ]; then
      # this echo should be in one line with a pipe
      echo "-t 'Backup' -m 'Your IP is $SourceIP. Starting backing up now'" | xargs /usr/local/bin/growlnotify
      echo "Backing up" >> $LOGFILE 2>&1
      rdiff-backup $SourceDir $DestinationDir >> $LOGFILE 2>&1
      # if rdiff-backup 
      if [ $? == 0 ]; then
        echo "-t 'Backup' -m 'Backup completed successfully'" | xargs /usr/local/bin/growlnotify
        echo "Backup completed successfully" >> $LOGFILE 2>&1
        echo "-t 'Backup failed' -m 'Something was wrong'" | xargs /usr/local/bin/growlnotify
        echo "Backup failed" >> $LOGFILE 2>&1
      # delete all backups older than 2 weeks
      rdiff-backup --remove-older-than 2W $DestinationDir >> $LOGFILE 2>&1
You can read the entire tutorial on my blog, and download the latest version of the script.

[robg adds: I've copied the script as it currently exists here, just in case the blog entry ever goes away. However, check the linked blog entry first for a newer version, and to read the details on how it works.]
  Post a comment  •  Comments (2)  
  • Currently 2.48 / 5
  You rated: 3 / 5 (21 votes cast)
[6,967 views]  View Printable Version
Create a range of solid colors for Desktop backgrounds UNIX
Mac OS X Leopard and Snow Leopard come with a paltry set of solid colors for desktop backgrounds. This is easily remedied with the ImageMagick package (available for install through both MacPorts and Fink), and a quick Terminal command.

The script below generates all the background images for web-safe colors that should then be immediately available for use in the Solid Colors section of the Desktop tab of the Desktop and Screen Saver System Preferences panel.
cd /Library/Desktop\ Pictures/Solid\ Colors; for r in 00 33 66 99 cc ff; do for g in 00 33 66 99 cc ff; do for b in 00 33 66 99 cc ff; do echo Creating image for color $r$g$b; convert -background "#$r$g$b" -page 256x256 text:- "Background $r$g$b.jpg" < /dev/null; done; done; done
You can copy and paste the script into Terminal, but you’ll need to be a system administrator for it to succeed, because it writes the images into /Library/Desktop Pictures/Solid Colors.

[robg adds: I tested this on a 10.5 machine, and it works as described. If you just have a few solid colors you want to use, you can create them yourself, as we covered in this hint. In addition, if you'd like to make it possible to use any color as your desktop background, you can create a 128x128 transparent PNG, and then use a color picker to set the color of the desktop to anything you like -- see this hint for more details.]
  Post a comment  •  Comments (9)  
  • Currently 2.47 / 5
  You rated: 3 / 5 (19 votes cast)
[8,513 views]  View Printable Version
10.6: Install fish shell on an upgraded version of 10.6 UNIX
Snow Leopard only hintAll the cool kids are using fish (bash is so 90's!), so why can't I? Because it won't install on my copy of Snow Leopard, dang it. I spent the greater part of this weekend trying to figure out why fish didn't like me. Read this so you won't have to endure my pain.

Using MacPorts, typing port install fish wouldn't result in a working installation. The install kept asking for iconv, gettext, or whatnot. 64-bit/32-bit Unix apps don't play well together in Snow Leopard, and because I upgraded from Leopard, I needed to reinstall MacPorts to deal with it. Long and painful, indeed.
read more (183 words)   Post a comment  •  Comments (11)  
  • Currently 2.13 / 5
  You rated: 4 / 5 (15 votes cast)
[7,117 views]  View Printable Version
Swap Command and Alt/Option keys in UNIX
The free program cmd-key-happy allows you to swap the Command and Alt/Option keys in any application, but in particular, in

This can be extremely handy when ssh'ing into other UN*X boxes and running emacs -nw. It also allows you to have the traditional readline navigation work properly when using Bash (i.e., alt-backspace, alt-f, alt-b, etc). See this blog post for more details.

[robg adds: Installation help is available in the INSTALL file in the github repository. I haven't tested this one.]
  Post a comment  •  Comments (2)  
  • Currently 2.47 / 5
  You rated: 2 / 5 (17 votes cast)
[5,930 views]  View Printable Version
Record CPU usage for a given process over time UNIX
For an upcoming project involving virtualization applications, I wanted an easy way to record the CPU usage for a given program over time.

I'm aware of the Instruments program included in the Xcode Developer Tools, but I was looking for something simple, that I could run without a lot of overhead, and that would just record data for further analysis in Excel.

After a bit of digging with Google, I didn't find anything that quite worked (a friend tells me that mrtg and Cacti should handle this; I haven't tried them yet), so I created a relatively simple bash script to get the job done - special thanks to Keith Bradnam for assistance in making it more interactive.

The program reports on a chosen program (process ID, actually) at a specified interval, then saves a few items (timestamp, process ID, CPU usage, and command name) to a text file, in this format:
20:20:39 29916  15.7 firefox-bin
20:20:41 29916   4.0 firefox-bin
20:20:43 29916  48.6 firefox-bin
The program does not create pretty graphs, or do any sort of analysis -- that part is up to you! However, with the data, you can do some interesting things. For instance, the graph above (click it for a much larger version) shows Firefox's CPU usage while browsing and playing a few Flash games on my MacBook Pro for ten minutes or so.

Read on for the details...

read more (195 words)   Post a comment  •  Comments (12)  
  • Currently 2.84 / 5
  You rated: 5 / 5 (19 votes cast)
[14,339 views]  View Printable Version
Download all Apple open source OS X files at once UNIX
While it is well known that Mac OS X contains open source code, how to access and download that source code is perhaps less well known. Apple publishes all its open source code on their Apple Open Source site. However, this site makes you download each program individually, without an obvious option to download an entire OS X release at once (i.e., all public sources for 10.6.1).

So I wrote the following bash script to automate the download procedure. It downloads the individual tarballs for each program, expands them locally, and then rolls everything up into one big tarball for local storage. Some caveats are that this script does not build the code, and that you are bound by a variety of licenses which are not included here (because no Apple code is included here).
#!/usr/bin/env bash
# --- applesource.bash --- downloads source code for an entire Apple release

# Take input from command-line (use "10.5.8", "10.6.1", etc.)
  version="mac-os-x-`echo $* | tr -d "."`"

# URL:

# Announce beginning, and prepare a directory for the untarred sources
  echo "Preparing to download..."
  mkdir -p ${sources}
# Process the webpage for the locations of the tarballs themselves
  curl --silent ${URL} | sed -n 's/<a href="\(.*.tar.gz\)">/\1/p' | \
    while read line; do
      # Find and announce the name of the next tarball to be downloaded
        echo -e "\nDownloading `basename ${tarball}`..."

      # Download the tarball and keep Apple's original directory structure intact
        curl --create-dirs --output ${tarball} "${homepage}${line}"

      # Untar into the "sources" directory (see line 13)
        tar xfz "${tarball}" -C ${sources}


# Compress all sourcefiles into a single tarball for posterity
  echo "Creating ${version}.tar.gz from all sources..."
  tar cfvz "${version}.tar.gz" ${sources}

# Explicitly show that everything finished.
  echo "Done."

# I like for my Mac to talk to me, although sometimes this can be creepy.
  say "The source of ${version} is now ready."
Save the above somewhere on your path, make it executable (chmod a+x scriptname), and then run it with the version number you'd like to download: getsource 10.6.1, for instance. I am a bit shy about releasing my scripts, so please be gentle!

[robg adds: I tested this and it worked as described.]
  Post a comment  •  Comments (2)  
  • Currently 2.80 / 5
  You rated: 3 / 5 (15 votes cast)
[11,919 views]  View Printable Version
10.6: See more process info in top UNIX
Snow Leopard only hintI am an old timer and use top (instead of Activity Monitor) to keep an eye on my system.

I just notice that in Snow Leopard when you horizontally resize the Terminal (or xterm) window, top displays more information about the running processes -- new columns are added as the window grows wider. Use man top to get an explanation of what those extra columns display.

Another nice change is that the Process ID (PID) is now suffixed with - to indicate a 32-bit process, and a * to indicate a PowerPC process.
  Post a comment  •  Comments (6)  
  • Currently 2.71 / 5
  You rated: 1 / 5 (17 votes cast)
[7,645 views]  View Printable Version