A Few Command-Line Party Tricks

It never ceases to amaze me how much I still have to learn about the innards of Mac OS X. It seems like I am always coming across new tools that I never knew existed, many of which have been in the system for many years. So I thought I would share a few of the command-line tools and tricks that have surprised me at some point, in the hope that some of you may also have a moment of enlightenment…

1. Process images with sips

You may think the command line is an inappropriate place for processing images, but if you have to do a batch of operations, it can make a lot of sense. sips allows you to query image properties, and perform conversions. For example, here is how you can change a jpeg image to png, change the resolution, and add a finder thumbnail:

sips --resampleHeightWidth 100 200 --addIcon --setProperty format png --out Photo.png Photo.jpg

2. Process text with textutil

textutil is a bit like the textual version of sips. You can use it to convert between various types of textual documents, including Word, HTML, and RTF. For example, say you have a bunch of Word documents that you want to put on the web. You could batch convert them to HTML, like this

for f in *.doc; do
    textutil -convert html $f
done

3. Search file content and metadata with mdfind

You may not realize that you can utilize Spotlight from the command line, as well as in Finder. The Spotlight commands all begin with ‘md’, for ‘metadata’. Actually, using Spotlight from the command line is more powerful than from Finder, because you can construct more advanced searches, using predicates. Here is how you might search in your Documents folder for any document that has the text ‘macresearch’ in its metadata:

mdfind -onlyin ~/Documents macresearch

and here’s how you might search for all Keynote documents:

mdfind -onlyin ~/Documents "(kMDItemKind == 'Keynote Document')"

If you are wondering how you can find out what metadata properties a file has, you can use the mdls command to list them:

mdls "/Users/cormack/Documents/Posters/WWDC2007 Poster/WWDCPoster2007.key"
/Users/cormack/Documents/Posters/WWDC2007 Poster/WWDCPoster2007.key -------------
kMDItemAttributeChangeDate     = 2007-06-04 12:20:35 +0200
kMDItemAuthors                 = ("")
kMDItemComment                 = ""
kMDItemContentCreationDate     = 2007-05-29 12:35:45 +0200
kMDItemContentModificationDate = 2007-06-04 12:14:47 +0200
kMDItemContentType             = "com.apple.iwork.keynote.key"
kMDItemContentTypeTree         = (
    "com.apple.iwork.keynote.key", 
    "com.apple.package", 
    "public.directory", 
    "public.item", 
    "public.presentation", 
    "public.composite-content", 
    "public.content"
)
kMDItemDisplayName             = "WWDCPoster2007"
kMDItemFSContentChangeDate     = 2007-06-04 12:14:47 +0200
kMDItemFSCreationDate          = 2007-05-29 12:35:45 +0200
kMDItemFSCreatorCode           = 0
kMDItemFSFinderFlags           = 8208
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 1
kMDItemFSLabel                 = 0
kMDItemFSName                  = "WWDCPoster2007.key"
kMDItemFSNodeCount             = 35
kMDItemFSOwnerGroupID          = 501
kMDItemFSOwnerUserID           = 501
kMDItemFSTypeCode              = 0
kMDItemID                      = 142650887
kMDItemKind                    = "Keynote Document"
kMDItemLastUsedDate            = 2007-06-04 12:20:35 +0200
kMDItemTitle                   = ""
kMDItemUsedDates               = (
    2007-05-29 12:35:47 +0200, 
    2007-05-29 02:00:00 +0200, 
    2007-05-30 02:00:00 +0200, 
    2007-06-04 02:00:00 +0200
)

4. Convert property lists with plutil

Mac OS X makes extensive use of property lists for configuration, not only in the OS itself, but also in every app written for it. One problem with property lists is that there isn’t a single format: Apple have started to use a binary format, rather than the text-based XML format, to save space and increase performance. Luckily, there is a tool to help you convert those binary plist files to a human readable form: plutil. Here’s how you can convert the contents of a binary property list to XML format:

plutil -convert xml1 some_file.plist

5. Copy and paste with pbcopy and pbpaste

You don’t typically associate the clipboard with the command line, but using pbcopy/pbpaste, you have the same access to it as you do in graphical applications. Here’s how you can get the contents of a directory onto the clipboard without touching your mouse:

ls | pbcopy

Pasting is just as easy:

pbpaste > lsoutput.txt

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

mdfind

mdfind is much more powerful than Spotlight and here are a few things I've picked up since I started using it.

If you want to search multiple words you need to put the words in quotes

mdfind macresearch article

Will only search for article, instead use

mdfind "macresearch article"

This will mean both terms must appear.

if you want to search for macresearch OR article use the pipe | character

mdfind "macresearch|article"

To exclude terms use

mdfind "macresearch(-article)"

Using quotes also helps stop the shell try to intercept special characters.