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

Preserve photo timestamp when exporting from iPhoto UNIX
When photos are exported from iPhoto using the Export function, all the photos will get the current date/time stamp. But if you use any of these photos in an app that organizes them by date/time, then the organization will be lost. For example, I export a set of images from iPhoto to view in my Wii's photo tool, but those images are organized by date.

To fix this problem, I took advantage of the EXIF data in the JPEG images from my digital camera. To batch change a large set of images, I use the open source command-line tool jhead, which is available as a pre-compiled Intel binary (source code is also available from the website, or via Fink).

I copied the jhead binary into an Applications folder in my home directory, and made it executable with the following command:
chmod 750 jhead
Then I wrote a simple shell script to batch update the images:

for i in *.JPG; do base=`basename "$i" .JPG`; mv "$i" "$base".jpg;done
find . -type f -name "*.jpg" -exec ~/Applications/jhead -ft {} \;
Don't forget to make the shell script executable as well.To run the script, just change into the directory with all images and run the shell script.

[robg adds: This worked for me, though the script does more than just change the date -- it also changes the extension from .JPG to .jpg. In my case, my exports already had a lower-case name, and I didn't want the filename changed, so I simplified the script by just using the second (find . -type...) line.

Once I had it down to one line, I realized a script wasn't necessary. Instead, I created a new alias in my .profile file:
alias fixdate="find . -type f -name \"*.jpg\" -exec ~/bin/jhead -ft {} \;"
Now after an export, I just cd to the directory and type fixdate to correct the date on the exported images.]
  Post a comment  •  Comments (7)  
  • Currently 2.27 / 5
  You rated: 4 / 5 (11 votes cast)
[10,004 views]  View Printable Version
Copy timestamps between a file or multiple files UNIX
Sometimes I find the need to copy a file's timestamp to another file. For example, when I do batch conversion of video files (e.g. via VisualHub), the converted files' timestamps are all based on the current date. Subsequently when I import them in iMovie, I have to spend some effort to get them to show the correct date. It would be nicer if the converted files' timestamps were exactly the same as the original ones.

So I created this shell script, which allows you to copy the timestamp from one file to another.

if [ $# -ne 2 ]; then
  echo "Usage: `basename $0` timestamp_src dest_file";
  exit 1;

function printtimestamp()
  stat -n -f "%Sm" -t "%Y%m%d%H%M" "$1"


if [ ! -f "${SRC}" ]; then
  echo "Error: ${SRC} does not exist.";
  exit 1;

if [ ! -f "${SRC}" ]; then
  echo "Error: ${SRC} does not exist.";
  exit 1;

TIME_SRC=`printtimestamp "${SRC}"`;

touch -t ${TIME_SRC} ${DST}
Just paste into a text editor and save it as a new shell script (, for instance) and run like so:
$ ./ timestamp_src dest_file
This will copy the timestamp of the file timestamp_src to the file dest_file.
read more (205 words)   Post a comment  •  Comments (7)  
  • Currently 2.00 / 5
  You rated: 5 / 5 (9 votes cast)
[9,630 views]  View Printable Version
xargs and working with spaces in filenames UNIX
When I'm working on a project I don't know much about, I do this before I do the compiles, so that the file FILES only has the source code:
find . -type f -print > FILES
Then, on most Unix platforms, I can do something like this with FILES:
xargs egrep whatever < FILES
On the Mac, however, this often breaks because of spaces in file names. I know about the -print0 option in find, so I could have two files, FILES and perhaps FILES0, which I could produce with:
find . -type f -print0 > FILES0
I could then follow that with this:
xargs -0 egrep whatever < FILES0
But I just figured out another solution...
read more (50 words)   Post a comment  •  Comments (10)  
  • Currently 1.67 / 5
  You rated: 4 / 5 (9 votes cast)
[14,694 views]  View Printable Version
Get CPAN to work with passive FTP UNIX
I don't know if this is already common knowledge, but it's been an irritation to me for some time and I finally figured it out. Whenever I tried to use CPAN to install Perl modules in Mac OS X, I would always get a never ending series of long waits and error messages akin to:
Fetching with LWP:
LWP failed with code[404] message[File '01mailrc.txt.gz' not found]
Fetching with Net::FTP:
Couldn't fetch 01mailrc.txt.gz from
When I searched for the error messages, all the top hits lead to the same question others had with no responses. Finally, I kept digging and tweaking my search parameters until I found a solution that worked. You need to set an environment variable to tell CPAN to use passive FTP mode. I have aliased this so that I never need to think about it again:
env FTP_PASSIVE=1 perl -MCPAN -e shell
If you type that into the command line, CPAN will finally work correctly. Don't forget that you may need to sudo the command.
  Post a comment  •  Comments (8)  
  • Currently 2.36 / 5
  You rated: 4 / 5 (11 votes cast)
[9,419 views]  View Printable Version
View international characters in SSH sessions UNIX
Files with Japanese names that show up fine when I'm in the local terminal, all turn into ???? when I ssh in from my laptop. It's because the locale of the remote sessions is ignoring the default locale and encoding of my local terminal. My local terminal defaults to "en_US.UTF-8," while the ssh sessions are something called "C," which apparently doesn't support Unicode.

One solution I've discovered is to to edit the ssh_config and sshd_config files so that the client sends the language environment variables, and the server accepts them. I've created two commands you can run in Terminal to achieve this: On the server, you should run:
sudo sh -c 'printf "\n\n# Accept language environment variables\nAcceptEnv LANG LC_*" >> /etc/sshd_config'
and on the client, run:
sudo sh -c 'printf "\n\n# Send language environment variables\nSendEnv LANG LC_*" >> /etc/ssh_config'
These commands append lines to your config files. They tell your client and server to respectively send and receive only the environmental variables that control language. You do not have to restart the server after running this command because the new config will be applied to the next session.

[Editor's Note: This hint has not been tested.]
  Post a comment  •  Comments (6)  
  • Currently 1.67 / 5
  You rated: 3 / 5 (9 votes cast)
[7,764 views]  View Printable Version
Clear the msftres 'flag' set by GNU parted UNIX
If you have the problem that OS X doesn't recognize your FAT/NTFS partition because GNU parted changed the partition type to "Microsoft Reserved," this might help.

In GNU parted, it looks like 'msftres' was a flag, but this is not the case. It refers to the partition type 'Microsoft Reserved,' which can be used for NTFS and FAT. The partition type 'Basic data' can also be used for these file systems, which is then recognized by OS X.

Fortunately Linux also uses 'Basic data' for its main partitions, so the solution is to change the file system type to ext2 using GNU parted.
  Post a comment  •  Comments (3)  
  • Currently 1.33 / 5
  You rated: 2 / 5 (9 votes cast)
[6,596 views]  View Printable Version
Yet another way to read beautifully formatted man pages UNIX
Many contributors have come up with ways to read man pages in a more usable format. However, using troff to produce PDFs takes time and I don't really like the output. There are also some excellent man page reading apps out there, but they require launching and browsing a list.

So I came up with a simple shell function to provide exceptionally fast lookups from the Apple Developer site's HTML man pages. That way, you get your nicely-formatted Darwin-specific man pages in a tab in your browser.

The function uses man -w to get the section number and then forms a URL which then gets launched. The web pages are organized by section, and so the URL is not straighforward. I'm new to sed, and my guess is that there are readers that can improve my regex. I've tested this with with a variety of commands, but certainly not exhaustively.

Drop this into your user's .bashrc file. You should also put it into .bash_profile if you maintain them separately -- my .bash_profile has a single line (source ~/.bashrc), so I only have to maintain one.
function wman() {
   url="man -w ${1} | sed 's#.*\(${1}.\)\([[:digit:]]\).*\$#\2/\1\2.html#'"
   open `eval $url`
Thereafter, you can just type wman and the name of the command you want (e.g., wman rsync). If anyone knows how to automatically scroll the browser down to the horizontal rule, I'd love to know how. There are no named anchor points in the pages, so a # in the URL doesn't work.

[robg adds: Two previous hints on different ways to read man pages: as a PDF in Previewas ASCII, HTML, or PostScript. I tested the wman command in this hint, and it works as described.]
  Post a comment  •  Comments (21)  
  • Currently 2.00 / 5
  You rated: 5 / 5 (10 votes cast)
[9,249 views]  View Printable Version
Browse current Terminal directory in a web browser UNIX
While browsing (a very useful site, by the way), I found this gem that uses Python to quickly set up a webserver for the current directory in Terminal. First launch Terminal and cd to the directory you'd like to access via your web browser. Then just use this command to start a webserver serving just that folder (and folders within it):
python -m SimpleHTTPServer
The webserver runs on port 8000, so to access the pages in your browser, you'd use http://localhost:8000. If you'd rather run the webserver on another port, just include the port number on the python line: python -m SimpleHTTPServer 8080.

Depending on which browser you're using and the files you're browsing, you may be able to open the files in the browser, or via a helper application. Safari, unfortunately, wants to download file types it doesn't know about, which may not be all that useful.

To quit the server, press Control-C in Terminal (does anyone know a more graceful way to do this; Control-C throws a bunch of errors as it quits?).
  Post a comment  •  Comments (5)  
  • Currently 1.78 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (9 votes cast)
[7,496 views]  View Printable Version
Change screen resolution in Terminal UNIX
Terminal is very useful in managing of rooms of Macs with Apple Remote Desktop. One thing I do often is change the screen resolution. To do that, I use a custom Terminal program that's based largely on Jeffrey Osterman's code posted in this Mac OS X Hints Forums thread.

It is easy to compile (you'll need Xcode to do so, however), and it supersedes the old and missing cscreen. First, here's the code:
* setgetscreenres.m
* juanfc 2009-04-13
* Based on newscreen
*    Created by Jeffrey Osterman on 10/30/07.
*    Copyright 2007 Jeffrey Osterman. All rights reserved. 
*    c++ setgetscreenres.m -framework ApplicationServices -o setgetscreenres
* USE:
*    setgetscreenres 1440 900

#include <ApplicationServices/ApplicationServices.h>

bool MyDisplaySwitchToMode (CGDirectDisplayID display, CFDictionaryRef mode);

int main (int argc, const char * argv[])
	int	h; 							// horizontal resolution
	int v; 							// vertical resolution
	CFDictionaryRef switchMode; 	// mode to switch to
	CGDirectDisplayID mainDisplay;  // ID of main display

	CFDictionaryRef CGDisplayCurrentMode(CGDirectDisplayID display);

	if (argc == 1) {
	    CGRect screenFrame = CGDisplayBounds(kCGDirectMainDisplay);
		CGSize screenSize  = screenFrame.size;
		printf("%d %d\n", screenSize.width, screenSize.height);
		return 0;
	if (argc != 3 || !(h = atoi(argv[1])) || !(v = atoi(argv[2])) ) {
		fprintf(stderr, "ERROR: Use %s horres vertres\n", argv[0]);
		return -1;

	mainDisplay = CGMainDisplayID();

	switchMode = CGDisplayBestModeForParameters(mainDisplay, 32, h, v, NULL);

	if (! MyDisplaySwitchToMode(mainDisplay, switchMode)) {
	    fprintf(stderr, "Error changing resolution to %d %d\n", h, v);
		return 1;

	return 0;

bool MyDisplaySwitchToMode (CGDirectDisplayID display, CFDictionaryRef mode)
	CGDisplayConfigRef config;
	if (CGBeginDisplayConfiguration(&config) == kCGErrorSuccess) {
		CGConfigureDisplayMode(config, display, mode);
		CGCompleteDisplayConfiguration(config, kCGConfigureForSession );
		return true;
	return false;
Save that as a pure text file named setgetscreenres.m, then compile it in Terminal using the command shown in the code (assuming you're in the directory where the file is saved):
c++ setgetscreenres.m -framework ApplicationServices -o setgetscreenres
Move this file somewhere on your user's $PATH, and then use it by typing setgetscreenres hor_res vert_res, where hor_res and vert_res are the desire horizontal and vertical resolution.

[robg adds: I compiled this, then tested it, and it works as described.]
  Post a comment  •  Comments (20)  
  • Currently 2.76 / 5
  You rated: 4 / 5 (17 votes cast)
[59,811 views]  View Printable Version
Create symbolic links for all items in a folder UNIX
If, for some reason (space in my case), you want your applications to reside somewhere other than /Applications, you will still want to have links to those relocated programs in /Applications. For Apple applications, updates are likely to fail if the applications aren't located there, and tools like AppFresh only search that location.

Creating a bunch of aliases is tedious, so here's a quick Terminal trick (that I didn't think would work, as it doesn't under Linux):
$ cd /Applications
$ ln -s /path/to/apps/* .
Change /path/to/apps to the path to your replacement Applications folder, and then run those two Terminal commands (don't overlook the very-important . at the end of the second one), and you're done.
  Post a comment  •  Comments (20)  
  • Currently 2.18 / 5
  You rated: 4 / 5 (11 votes cast)
[15,688 views]  View Printable Version