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

One response to “A Few Command-Line Party Tricks”
  1. Gwyneth Llewelyn Avatar
    Gwyneth Llewelyn

    More! I want to know more!

    Thanks for those, anyway — I was only aware of plutil (and vaguely remember how to use pbcopy/pbpaste). The others are quite interesting: I use ImageMagick, but I could have been using sips; I use pandoc to accomplish the same as textutil; and since immemorial times I’ve always used locate (and have it always running in the background, too) when apparently I could use mdfind for the same purpose — and spare all those background activity for locate!

    You gave me a taste of the wonderful world of unknown/little-known command-line utils, and I surely hope you’ll be satisfying our common curiosity digging for more 🙂

    Thank you! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *