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

Show a random message at bash shell startup UNIX
I really don't know what I'm doing here (my experience in shell scripting amounts to about two hours), but I put together this little script that will print out a random message each time you open a new bash window. Just copy this into your user's .bash_profile file, located in your user's home directory:
cnt=$(wc -l $fn)
if [ "$lns" = 0 ]; then
  let "lns += 1"
  rnd=$(expr $RANDOM % $lns)
  let "rnd += 1"
sed -n ${rnd}p $fn
Set the fn variable in the first line to the path and name of your quotes file. Each quote should be on its own line, with no extra lines between the quotes.

[robg adds: This worked for me -- note that this will not replace Terminal's welcome message (Welcome to Darwin!) with the quote -- it shows up after the welcome message. If you want to change the welcome message, you need to edit /etc/motd. Replacing the welcome message with a random quotation is left as an exercise for the reader (i.e. I don't have a clue...), but this hint might get you started.]
  Post a comment  •  Comments (8)  
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
[15,105 views]  View Printable Version
A Folder Action Script to auto-set the Execute bit UNIX
This is a simple Folder Action Script that will set the executable bit on any file saved within the folder to which the script is attached. Copy and paste the following into a Script Editor document, and save it as a script into your Folder Action Scripts directory.
on adding folder items to thisFolder after receiving theItem
  set fileName to POSIX path of theItem
  do shell script "chmod +x " & fileName
end adding folder items to
Remember to call your shell (#!/bin/bash) or application (#!/usr/local/bin/ruby -w) in the first line of your script. Then save to, or drag your file to, the folder with the action script attached.
  Post a comment  •  Comments (2)  
  • Currently 3.33 / 5
  You rated: 5 / 5 (3 votes cast)
[8,670 views]  View Printable Version
Run apps at different color depths simultaneously in X11 UNIX preferences allow you to set it to 256 colors for the occasional x-windows programs requiring this setting, but you may then find other x-windows applications don't function at all, or at least look terrible. There is a simple way to have it both ways, simultaneously. The trick is to create a wrapper shell script that looks something like this:
#!/bin/zsh -f
Xquartz $DISPLAY  -depth 8    2>/dev/null  & 
quartz-wm &
This essentially forces a second X11 session to be started on a different (randomly determined) DISPLAY, so it can peacefully coexist with running in default mode (usually on DISPLAY :0.0, unless multiple users have X11 running with Fast User Switching). By randomly assigning the DISPLAY, multiple instances of the program invoked from the wrapper script can be run simultaneously. This works on Intel powered Macs now, too.

[robg adds: I haven't tested this one...]
  Post a comment  •  Comments (4)  
  • Currently 1.40 / 5
  You rated: 1 / 5 (5 votes cast)
[7,769 views]  View Printable Version
Compare directories via diff UNIX
I like to keep the home directories on my work (PC) and home (Mac) machines more-or-less in sync using a hard drive that I tote back and forth every few weeks. In addition to rsync, one useful tool is the unix diff command.

As mentioned in other hints, diff can not only compare two files, it can, by using the -r option, walk entire directory trees, recursively checking differences between subdirectories and files that occur at comparable points in each tree. The trick is to use the -q option to suppress line-by-line comparisons in files that differ:
diff -rq dirA dirB
This command will provide a nice list of files that occur in dirA but not in dirB, files that occur in dirB, but not in dirA, and files that differ between dirA and dirB. Pipe the output through grep to remove mention of uninteresting files, and sort to tidy it up, e.g.:
diff -qr dirA dirB | grep -v -e 'DS_Store' -e 'Thumbs' | 
sort > diffs.txt
This list gives me a good feel for the big picture before I start overwriting things: which files or subdirectories can be deleted, which can be synced (and in which direction) using rsync, and which should be carefully checked before replacing, in case changes need to be merged.

To forestall some obvious comments, Unison would seem to be the ideal tool, but it lists hundreds of files that only differ in their permissions metadata (not important to me). Although Unison appears to have an option to turn off permission checking (-perms 0, or -perms=0), I couldn't get it to work. There are, of course, a number of GUI apps that would do the job, too (e.g., FileMerge), many of them shareware.
  Post a comment  •  Comments (14)  
  • Currently 4.53 / 5
  You rated: 5 / 5 (17 votes cast)
[192,048 views]  View Printable Version
A script to back up files to a single ISO image UNIX
I bring my iBook everywhere, and I store on its drive 80% of my important data, since I use it for studying, working, scripting, doing projects for university, listening to music, downloading stuff, browsing, printing nice articles found on the Web to PDFs, etc. Scared by the fact that hard drives fail when you least expect it -- and trust me, they still fail if you (like me) love your hardware more than your girlfriend. So I spent some days thinking about and coding a little script-based backup system with the following features:
  • low memory usage
  • fast!
  • capable of backing up several directories in a single .iso image, ready to be burned.
My files-to-backup and my backup files must be stored on the same hard disk (the 40GB one inside my iBook), at least until I burn them on CDs every weekend. So, to prevent useless duplicates floating in my laptop, and my confusion during my several daily Spotlight searches, I chose to back up to a disc image (i.e. a single explorable file), instead of a "normal" method of back up (with duplicate files/folders, usually on another drive).

I chose the ISO format instead of a disk image, because the first one is a more standard kind of disc image compared to the second one. It can be mounted and explored or burned on both Mac and on Windows machines (using the nice freeware tool called Daemon Tools, for example). Why did I code a script instead of using one of the tons of backup apps out there? Because the ones I tested don't do exactly what I need, and because doing the things I need myself and watching them work wonderfully makes me happy like a child who has finally received its most-wanted toy from Santa Claus! And by the way, scripting on Mac OS X is so easy that you can't really stop doing it once you get started.
read more (776 words)   Post a comment  •  Comments (9)  
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
[30,807 views]  View Printable Version
A revised fix for the apropros long-entry problem UNIX
I'm frustrated and surprised that the problems with apropos have still not been fixed. I noticed this was discussed here before, but apparently those fixes don't work on my current install of Mac OS X 10.4.9. Using ideas from the prior hint, I added the following as a fix in my .bash_profile:
function aproposFunc() {
  width=`stty size | cut -d  -f2`; apropos $* | cut -c1-$width;
alias apropos=aproposFunc

For those not aware, the problem causes many lines (or pages) of extra information to be printed when you execute apropos key. The prior fix involved limiting the line-length as a pipe after the $PAGER variable (i.e. less), or using the -S command-line option. The current implementation of apropos no longer uses the $PAGER variable, but instead becomes a grep on a large text file at /usr/share/man/whatis. That text file is built by a system process, but perhaps ends up with the over-extended lines because of problems between makewhatis and the man pages' formatting.

[robg adds: This one didn't work for me in testing -- apropos serial returned the same long text as it did before ... if anyone can fix the code, please post in the comments. Note that I had to add single quotes around the alias entry in my .profile file to get the alias recognized.]
  Post a comment  •  Comments (5)  
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
[5,054 views]  View Printable Version
Create incremental backups using an rsync shell script UNIX
Remarkably, there are many pitfalls in the tools currently available to back up OS X. I ran into difficulties with several recommended backup solutions, such as the popular Carbon Copy Cloner and others. After poking around the web, I found a shell script solution based on rsync that works fine for me.

Here's my version of it. It keeps two older backups, and uses hard links for all unchanged files to avoid unnecessary duplication. Edit it in the obvious way to put the backups where you want them. Also, you must run this script as root.

[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (18)  
  • Currently 3.20 / 5
  You rated: 3 / 5 (5 votes cast)
[29,053 views]  View Printable Version
Send mail via cron job with an overly-protective ISP UNIX
I recently got a new ISP after a house move, and have always had a cron job running to back up my files each day to an online server. Previously, I would have this job (rsync) run and pipe the output automatically to the mail command, which would throw it out to my inbox, allowing me to automatically see if there was anything wrong with the sync.

With my new ISP, they have this annoying habit of blocking any emails that look like they are coming from an SMTP server that is not their own. Obviously, so they do not get spammers blacklisting them, and hence not even their SMTP servers would be allowed to send mail, or their mail would not be accepted. This poses an issue for tech savvy people who may know what they are doing, but are being punished for knowing how things work.

Hence, I stopped getting my emails to say how the sync had gone. After getting annoyed by this, I started to look into it further. It seemed that if I was on a different network, for example my university one, and the job ran, the email would not get through -- at least not until I got home, at which point I would receive the email (the other network was blocking all smtp email; it is smarter than my ISP).

After examining the headers of the email I did receive, I noticed that the "from" address was not the normal root@robs-powerbook.local, but had rather taken on the domain of the network I was on. You can see where this is going. If you use the -r command in the mail command when piping information to it in cron, you will get a senders address that you specify. So here is my modified command (line breaks added for readability; enter as one long line):
sh /Users/rob/bin/ 2>&1 | 
mail -s "Daily Backup to joyent report" 
If you set the two email addresses to be the same thing, i.e. the email address you want to send to, you should get an email from yourself every time it is run. This may not work on all hosts, but it may help with some!
  Post a comment  •  Comments (9)  
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
[15,011 views]  View Printable Version
Learn how to build, compile, and customize mutt UNIX
There have been a few hints on the site regarding using the CLI-based email client mutt, but they've typically been little snippets about how to accomplish certain things. Being an avid mutt user on Linux, and then on OS X, I've written an article called Using mutt on OS X, which is a full guide on using mutt, covering everything from mutt configuration (with sample configuration files), to setting up fetchmail and procmail, to even tweaking the postfix MTA (or installing and using exim instead).

It details a number of OS X-specific items, such as integration with Address Book, being able to automatically open attachments from mutt, etc. A few of these are from previous hints that ran, but this article takes you from having never used mutt all the way to being able to power-use it.

Using mutt gives you the most rich and flexible email client experience currently available on OS X. Yes, it's a commandline client, and as such doesn't have all the mouse clicks and sounds and whatnot that something like would have, but it's stable, handles huge mailboxes, and is customizable like you would not believe.
  Post a comment  •  Comments (11)  
  • Currently 2.25 / 5
  You rated: 2 / 5 (4 votes cast)
[9,211 views]  View Printable Version
10.4: Integrate CUPS 1.2.8 with Mac OS X 10.4.8 UNIX
The nice folks at have made a good installer package for the latest version of CUPS, 1.2.8. This version is newer than the version of CUPS included in Mac OS X 10.4.8, and offers features like: The ability to move jobs from queue to queue via the web interface (https://localhost:631); secure access via the web interface; and the ability to set print dialog default selections on a per-queue basis.

You may find it useful, especially if you use a Mac OS X or Mac OS X Server system as a print server. However, the installation isn't perfectly smooth, and it breaks some conventions that the Apple-supplied CUPS uses. Read on to see what breaks, and how I worked around those issues...
read more (474 words)   Post a comment  •  Comments (11)  
  • Currently 1.50 / 5
  You rated: 1 / 5 (4 votes cast)
[35,442 views]  View Printable Version