juretta.com

Mac OS X command line goodies

February 04, 2007
Tags: Bash Mac Unix

Mac OS X has several command line programs you maybe unaware of. Here are a few that are worth knowing of.

Command+Shif+4 or Command+Shift+3 are the built in commands to capture the current screen (3) or parts of the screen (4). With screencapture you can use this facility using the command line.

macbook-sts:~ stefan$ screencapture -C ~/Desktop/screen.png

The command open opens files and directories.

macbook-sts:~ stefan$ open ~/Desktop/screen.png
# opens the file screen.png using the default program
# which would be used if you double clicked the file
macbook-sts:~ stefan$ open -a textmate /etc/hosts
# opens the file /etc/hosts using the application "Textmate"

The command pbcopy and it's sibling pbpaste open up the Mac OSX clipboard for your command line programs. Select some text in your browser, press Command+C and you can print the content of the clipboard with pbpaste.

macbook-sts:~ stefan$ pbpaste | sed s/e/E/g
command linE programs. SElEct somE tExt in your browsEr, prEss...

To put some text into your clipboard you can use pbcopy. The following command reads the /etc/motd file (motd stands for message of the day, this is what you can see when opening a terminal - on Mac OS X its usually "Welcome to Darwin") and puts it's content into the clipboard.

macbook-sts:~ stefan$ cat /etc/motd | pbcopy

Are you using Ruby? Do you have RubyGems installed? You might want to consider installing the attachr gem (sudo gem install attachr). You can save text snippets to attachr.com from your commandline.

macbook-sts:~ stefan$ sudo gem install attachr
Successfully installed attachr-0.1.5
Installing ri documentation for attachr-0.1.5...
Installing RDoc documentation for attachr-0.1.5...

macbook-sts:~ stefan$ cat /etc/motd | attachr
Your text was posted to http://attachr.com/6948

With lsbom you can examine the contents of a Mac OS X installer (.pkg).

macbook:~ stefan$ lsbom -f /Library/Receipts/Java.pkg/Contents/Archive.bom

lipo is a tool to operate on universal binary files. You can get detailed info on a universal file by using lipo -detailed_info /path/to/binary

macbook-sts:~ stefan$ lipo -detailed_info /Applications/\
	DVD\ Player.app/Contents/MacOS/DVD\ Player 
Fat header in: /Applications/DVD Player.app/Contents/MacOS/DVD Player
fat_magic 0xcafebabe
nfat_arch 2
architecture i386
    cputype CPU_TYPE_I386
    cpusubtype CPU_SUBTYPE_I386_ALL
    offset 4096
    size 2291764
    align 2^12 (4096)
architecture ppc
    cputype CPU_TYPE_POWERPC
    cpusubtype CPU_SUBTYPE_POWERPC_ALL
    offset 2297856
    size 2525088
    align 2^12 (4096)

The system_profiler command reports system hardware and software configuration. The following example prints the hardware configuration.

macbook-sts:~ stefan$ /usr/sbin/system_profiler SPHardwareDataType
Hardware:

    Hardware Overview:

      Machine Name: MacBook Pro 15"
      Machine Model: MacBookPro1,1
      Processor Name: Intel Core Duo
      Processor Speed: 2 GHz
      Number Of Processors: 1
      Total Number Of Cores: 2
      L2 Cache (per processor): 2 MB
      Memory: 2 GB
      Bus Speed: 667 MHz
      Boot ROM Version: MBP11.0055.B08
      SMC Version: 1.2f10
      Serial Number: XXXXXXXXXX
      Sudden Motion Sensor:
          State: Enabled

macbook:~ $ /usr/sbin/system_profiler SPHardwareDataType | grep Memory
      Memory: 2 GB

softwareupdate installs system software updates from apple.

macbook-sts:~ stefan$ sudo softwareupdate -i -r
Software Update Tool
Copyright 2002-2005 Apple
[...]

You might know how to use the find command to search for files. It's mac specific sibling mdfind is using the Spotlight metadata repository to find files. To list the metadata for a specific file you can use the mdls command.

macbook-sts:~ stefan$ mdfind -onlyin /usr/local stomp 
/usr/local/lib/ruby/gems/1.8/specifications/stomp-1.0.5.gemspec
/usr/local/lib/ruby/gems/1.8/gems/stomp-1.0.5
/usr/local/lib/ruby/gems/1.8/gems/stomp-1.0.5/lib/stomp.rb
/usr/local/lib/ruby/gems/1.8/doc/stomp-1.0.5
/usr/local/lib/ruby/gems/1.8/cache/stomp-1.0.5.gem
macbook-sts:~ stefan$ mdls ~/Desktop/spring-reference.pdf 
/Users/stefan/Desktop/spring-reference.pdf -------------
kMDItemAttributeChangeDate = 1970-01-01 01:00:00 +0100
kMDItemFSContentChangeDate = 2006-10-03 15:17:50 +0200
kMDItemFSCreationDate      = 2006-10-03 15:17:50 +0200
kMDItemFSCreatorCode       = 0
kMDItemFSFinderFlags       = 0
kMDItemFSInvisible         = 0
kMDItemFSIsExtensionHidden = 0
kMDItemFSLabel             = 0
kMDItemFSName              = "spring-reference.pdf"
kMDItemFSNodeCount         = 0
kMDItemFSOwnerGroupID      = 501
kMDItemFSOwnerUserID       = 501
kMDItemFSSize              = 3684682
kMDItemFSTypeCode          = 0
kMDItemID                  = 1949239

The last command convert[s] text to audible speech. See man say for more information to use say.

macbook-sts:~ stefan$ say -v Albert "Hello world"
# converts your clipboard content to speech
macbook-sts:~ stefan$ pbpaste | say
# You can get a list of the available voices:
macbook:~ $ ls /System/Library/Speech/Voices/ | sed s/.SpeechVoice//g
Agnes
Albert
BadNews
Bahh
Bells
Boing
Bruce
Bubbles
Cellos
Deranged
Fred
GoodNews
Hysterical
Junior
Kathy
Organ
Princess
Ralph
Trinoids
Vicki
Victoria
Whisper
Zarvox

About

This is the defunct blog of Stefan Saasen.