Snipe.Net Geeky, sweary things.

Handy Mac OSX Command Line Tricks

H

Mac users love their Macs for lots of different reasons – the interface is pretty and (generally) stable, things just “work”, and for the command-line junkies, there’s nothing you can’t do by command line that you can do through the GUI. In fact, there’s some stuff you can’t do with the GUI, but a quick terminal command can bang it out in a jiffy.

I’ll preface this post by saying that I am not a Mac expert by any means. I’ve only been using one on a regular basis since May 2008 (I have had one for much longer, but it’s used solely for video editing), and have only owned one that I use every day for a few months. Fortunately, Mac OS X is based on the Mach kernel and is derived from the Berkeley Software Distribution (BSD), and as a Linux user, I’m pretty comfy on the command line.

If you’re brand-spankin’ new to the command line, this isn’t the place to start. Instead, check out The Mac Terminal: An Introduction by Alex Galonsky of MacApper, and then come back here.

I had the idea to write this post, to help other people – and to keep track of the bits I had picked up over time – so I did some research, and reached out to a few friends who are far more Mac-savvy than I.

Web Development & Browsing

Clear your Mac’s DNS Cache
If you’re a web developer on a Mac and have made a DNS switch, you may need to clear the DNS cache on your machine in order for the web address in question to correctly resolve to the new host after propagation. This command will flush your DNS cache. (Bear in mind that your ISP may have DNS cached as well, so this trick won’t always work, but its always worth a shot.)

dscacheutil -flushcache

(Note: For Tiger users, type: lookupd -flushcache)

Turn on Debugging/Developer Mode in Safari (via Vidyut Luther)
Make sure you restart Safari after entering this in Terminal. On the top right of the menu bar there will appear a new menu item titled “Debug” that will let you do a host of new things.

defaults write com.apple.Safari IncludeDebugMenu 1

Force All Links in Safari to Open in New Tabs Instead of New Windows
If you can’t stand it when links create a new browser window and prefer them to always open in a new tab, type:

defaults write com.apple.Safari TargetedClicksCreateTabs -bool TRUE

File Utilities

Eject a Stubborn CD
I’ve run into this a few times, where a CD is in the CD drive, but Finder isn’t, well, finding it. Since I can’t see it, I can’t eject it or drag it to the trash. I can press the eject button on my keyboard until my fingers bleed, but no joy. This command uses the Disk Arbitration Command Tool. Type disktool to see all the options. The -e flag tells disktool to eject:

disktool -e disk

Turn Off the Stupid Time Machine Query (via Brian Little)
Time Machine tries to be “helpful” by asking you if you want to use the external disk drive you’ve just attached as a backup. Every. Single. Time. This little command tells Time Machine to STFU about it.

defaults write com.apple.TimeMachine DoNotOfferNewDisksForBackup -bool YES

Re-Index Spotlight to Fix WonkyResults (via Brian Little)
If your Spotlights results don’t seem to add up, try these two commands to re-index the drive.

sudo mdutil -E /

and then

sudo mdutil -i on /

Show Hidden Files
I switch back and forth for this one. In general, I prefer to leave hidden files hidden, but if I’m developing a site locally and uploading to the FTP location, the default Mac settings will hide my .htaccess file, so I can’t upload them. On the other hand, if I’m uploading whole directories, leaving hidden files visible will cause all of the .DS_STORE files to be uploaded as well. They won’t hurt anything, but my OCD won’t allow that sort of thing, so I toggle this on and off depending on what I’m doing. Be sure to restart Finder after you type this:

defaults write com.apple.finder AppleShowAllFiles TRUE

Show Full File Path in Finder Windows
On a PC or a Mac, the first thing I always do is enable the display of the full file path in the Explorer/Finder window, so I can always see exactly where the file or directory I’m looking at lives. To always display the full path to any file or folder in the Finder window, type (remember to restart Finder for this to take effect):

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES

Screen Captures

Macs make it easy to capture and save screenshots using your command key

Change The Default Screen Capture Image Format (via Brian Little)
By default, Mac will save screen captures in .png format. If you prefer to default to a different file format, type:

com.apple.screencapture type image_format

where image_format is the new file format you want to use. The image_format can be BMP, GIF, JPEG-2000, JPEG, PDF, PICT, PNG, PSD, SGI, TGA or TIFF.

Then type:

killall SystemUIServer

Save Your Screenshots Somewhere Other Than Your Desktop
If you are as neurotic as I am about desktop clutter, you can change the default location where screenshots are saved, by typing:

defaults write com.apple.screencapture location "/Users/yourusername/screenshots" 

Everything else

Enable half-star ratings in iTunes (via Lifehacker)
If you’re a music buff who uses iTunes, and the five-star rating system isn’t quite granular enough for you (“do I really like it, or do I really, really REALLY like it?”), this handy command will enable half-star ratings in iTunes.

itunes-half-stars

To set this up, just type:

defaults write com.apple.iTunes allow-half-stars -bool TRUE

Drag Widgets Out Of Dashboard Onto Desktop
I love the widgets in the Dashboard, but being a former Windows user, I always forget they’re there. It never even occurs to me to click on my dashboard. To keep important widgets handy, drag widgets from your dashboard onto your desktop. You need to re-launch the dock for this to take effect by typing “killall Dock” and hitting enter. If you click and hold onto a widget in the dashboard and then press F12 to return to the desktop, the widget won’t disappear with the rest.

defaults write com.apple.dashboard devmode YES

type this to reverse it:

defaults write com.apple.dashboard devmode NO

Remember to type killall dock to restart your dock before the changes will take effect.

Add Spaces to Your Dock

If you’re like me and constantly mis-click and launch the wrong application (xchat instead of terminal, since they have similar icons and because I am dumb), adding spacers into your dock to visually separate the icons can be a big help. By adding spaces, you can group related icons together for ease of use, but have just enough of a visual difference to prevent you from spastically clicking on the wrong ones (like I do). For example,my dock looks something like this:

dock

In your Terminal prompt, type:

defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}'

When your dock restarts, you’ll see a spacer in the right-side. You can click and drag this spacer to wherever you need, and you can re-run that command in your Terminal window to create as many spacers as you want. And just like any other dock icon, you can right+click and remove, or just drag it off the dock to get rid of it.

Remember to type killall Dock to restart your dock before the changes will take effect.

Useful Links

For more great command line tips and tricks, check out the following:

Are you a Mac cli wizard? What are your favorite terminal commands? Let us know in the comments.

About the author

snipe

I'm a tech nerd from NY/CA now living in Lisbon, Portugal. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead, skeet me at @snipe.lol, or read more...

By snipe
Snipe.Net Geeky, sweary things.

About Me

I'm a tech nerd from NY/CA now living in Lisbon, Portugal. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead, skeet me at @snipe.lol, or read more...

Get in Touch