Home » Featured » Handy Mac OSX Command Line Tricks
Handy Mac OSX Command Line Tricks

Handy Mac OSX Command Line Tricks

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.

Advertisement

Net Tuts

Advertisement

Themeforest

flattr this!

About snipe

I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist, and the former CTO and CSO at a business innovation agency in New York City. Tweet at me @snipeyhead or read more...
  • http://metacrawl.tumblr.com/ Metacrawl

    Great post, i just recently got into trying to learn more in terminal and command line myself. Just for geeks sakes, i just ejected my CD using command line : } A few months ago actually, i had to find a MYSQL Database on a remote server, took me a few trys, but Terminal rocked it!

    Metacrawl’s last blog post..We have overcome… by Niggy Tardust

  • http://metacrawl.tumblr.com Metacrawl

    Great post, i just recently got into trying to learn more in terminal and command line myself. Just for geeks sakes, i just ejected my CD using command line : } A few months ago actually, i had to find a MYSQL Database on a remote server, took me a few trys, but Terminal rocked it!

    Metacrawl’s last blog post..We have overcome… by Niggy Tardust

  • http://www.phpcult.com/ Vid Luther

    Thanks for the post, now I know how to flush DNS on a mac. Will save me tons of restarts etc. You rock

    Vid Luther’s last blog post..Scaling a startup, technically and fiscally

  • www.phpcult.com Vid Luther

    Thanks for the post, now I know how to flush DNS on a mac. Will save me tons of restarts etc. You rock

    Vid Luther’s last blog post..Scaling a startup, technically and fiscally

  • http://www.phpcult.com/ Vid Luther

    In addition to showing the full path in the title of the Finder window, what I find very helpful is clicking on View in the Finder, and then clicking “Show Path Bar”.

    this shows a path on the bottom of the finder, but it’s clickable now, which makes dragging and dropping files easier, as well as navigating to a specific part of the path faster than hitting the back button several times.

    Vid Luther’s last blog post..Scaling a startup, technically and fiscally

  • www.phpcult.com Vid Luther

    In addition to showing the full path in the title of the Finder window, what I find very helpful is clicking on View in the Finder, and then clicking “Show Path Bar”.

    this shows a path on the bottom of the finder, but it’s clickable now, which makes dragging and dropping files easier, as well as navigating to a specific part of the path faster than hitting the back button several times.

    Vid Luther’s last blog post..Scaling a startup, technically and fiscally

  • http://www.espractico.com/ Rodrigo Polo

    This is an excellent post!!!
    If you want to extend the possibilities try macports!!!

    Rodrigo Polo’s last blog post..Full Screen En Flash

  • http://www.espractico.com Rodrigo Polo

    This is an excellent post!!!
    If you want to extend the possibilities try macports!!!

    Rodrigo Polo’s last blog post..Full Screen En Flash

  • http://ablnkslate.com/ Drew Tufano

    I too have only recently starting using OS X as my primary OS. Great tip on dock spacing. Would it be possible to post another screen shot of your dock, and label each app (Skitch is amazing, no?!)

    I see you’re using Coda for development, have you checked out MacRabbit Espresso? I think it’s quite literally changed my day to day efficiency. http://macrabbit.com/espresso/

    Drew Tufano’s last blog post..Our new logo

  • http://hopeablaze.com Drew Tufano

    I too have only recently starting using OS X as my primary OS. Great tip on dock spacing. Would it be possible to post another screen shot of your dock, and label each app (Skitch is amazing, no?!)

    I see you’re using Coda for development, have you checked out MacRabbit Espresso? I think it’s quite literally changed my day to day efficiency. http://macrabbit.com/espresso/

    Drew Tufano’s last blog post..Our new logo

  • http://www.snipe.net snipe

    Hi Drew – sure, tho my dock has changed since I posted this :) This is what I currently have:
    http://www.snipe.net/wp-content/uploads/2009/01/dock1.jpg

    I also have FontLab Studio there, but managed to drag it off my dock by accident, and didn’t notice until I had already written all the other app names out – so you can use your imagination. :)

    I have also been trying out different things with regard to grouping apps. Previously, I was grouping them solely by function. Browsers with browsers, editors with editors, etc. what I’ve come to find is that by doing that, I end up opening the wrong app multiple times during the day, especially if their icons look similar (coda and zde, xchat and terminal, etc). So I now group them based off the amount of time I generally need to click on them during the day.

    I believe I got a copy of Espresso in the MacHeist software bundle – haven’t had a chance to check it out yet. Really, the old text editors are the only things I miss from my PC. Keep meaning to actually try Espresso, since I have it already.

  • http://www.snipe.net snipe

    Hi Drew – sure, tho my dock has changed since I posted this :) This is what I currently have:
    http://www.snipe.net/wp-content/uploads/2009/01/dock1.jpg

    I also have FontLab Studio there, but managed to drag it off my dock by accident, and didn’t notice until I had already written all the other app names out – so you can use your imagination. :)

    I have also been trying out different things with regard to grouping apps. Previously, I was grouping them solely by function. Browsers with browsers, editors with editors, etc. what I’ve come to find is that by doing that, I end up opening the wrong app multiple times during the day, especially if their icons look similar (coda and zde, xchat and terminal, etc). So I now group them based off the amount of time I generally need to click on them during the day.

    I believe I got a copy of Espresso in the MacHeist software bundle – haven’t had a chance to check it out yet. Really, the old text editors are the only things I miss from my PC. Keep meaning to actually try Espresso, since I have it already.

  • jDeppen

    “killall dock” didn’t work for me until I used “killall Dock” (capital D)

  • jDeppen

    “killall dock” didn’t work for me until I used “killall Dock” (capital D)

  • http://teknoinfo.web.id/ Raffaell

    Wow, I bookmarked this post was really great, as i am a noob on mac. hahaha
    Very good info, thanks a lot!

    Last blog post: Nokia Release 5530 Express Musik

  • http://teknoinfo.web.id Raffaell

    Wow, I bookmarked this post was really great, as i am a noob on mac. hahaha
    Very good info, thanks a lot!

    Last blog post: Nokia Release 5530 Express Musik

  • http://www.rogenstudio.com/screenprint/ custom screen print

    I end up opening the wrong app multiple times during the day, especially if their icons look similar (coda and zde, xchat and terminal, nice design.

  • http://www.rogenstudio.com/screenprint/ custom screen print

    I end up opening the wrong app multiple times during the day, especially if their icons look similar (coda and zde, xchat and terminal, nice design.

  • http://www.snipe.net snipe

    I do the same thing ;)

  • http://www.snipe.net snipe

    I do the same thing ;)

  • supersalo

    Another good one is 'ShellHere' (http://www.etresoft.org/shellhere.html). It opens a terminal window for the 'front-most' Finder window.

    I put this handy little 'app' in the toolbar of all my Finder windows. Sure beats opening a terminal window, then 'cd'ing to that deep directory.

  • http://www.mactricks.net/ mac tricks

    i have to say the cd eject one is pretty worthless, if your cd is stuck its not going to come out because you used the keyboard shortcut, lol

  • http://www.snipe.net snipe

    You would think – but that's actually not true. I've had disk's get stuck where they wouldn't eject on pushing the eject button or ejecting from finder, but that worked.

  • http://www.erif.org/ Kaolin Fire

    Wow. Incredibly helpful tips.

  • Paul

    Okay, I really like putting the widgets on my desktop, but how do I keep them from staying on top of all windows? I don't like the weather being over EVERYTHING I do… thanks.

  • Paul

    Okay, I really like putting the widgets on my desktop, but how do I keep them from staying on top of all windows? I don't like the weather being over EVERYTHING I do… thanks.

  • http://www.desenle.com/ Cep Kaplama

    Effective. Excellent and sleek moves!

  • ErrantWanderer

    A database of hidden Mac settings, as a Preference Pane: http://secrets.blacktree.com/

  • Jeff

    I am having trouble with my Wireless Logitech mouse behaving very erratically since the 10.5.8 system update, and the OS mouse software is overriding the Logitech mouse's software, causing multiple unwanted events to occur when right clicking. I need to find a UNIX command to delete the option to make either button on a two button mouse the “dominant” button. The mouse driver software does exactly what I need it to do, but now the action mapped to this button (changing between tools in Quark Xpress by simulating a key command/press) is now accompanied by a right click/contextual menu which brings up a menu that I DO NOT want to activate when right clicking! It is wreaking havoc on my ability to perform my work in Quark Xpress! I can see the solution-there needs to be an option in the Keyboard & Mouse system control panel to NOT designate a Primary mouse button!!! Is there a way to do this with a UNIX command?
    I can be reached at [email protected]

  • http://www.snipe.net snipe

    Hmm.. Have you looked at Better Touch Tool? When I got my Magic Mouse I was ready to throw it out the window. BTT lets you customise just aboout every imaginable part of your mouse/touchpad – and it's free: http://blog.boastr.net/?page_id=1722

  • Rajeev Masilamony

    Aw yeah I am totally diggin the spaces!! Im OCD about my mac and the way it is organized so this was PERFECT! :D

  • Donna

    How about doing things like:
    Delete a folder (and files within).
    or
    Compress a folder (and files within).

    • http://www.snipe.net snipe

      Deleting a folder is the standard Linux/BSD command:

      rm -R [directory name]
      rm -Rf [ddirectory name]

      The second one (with the f) means “fast”, which means it won’t ask you to confirm deletion of every file in the directory.

      To zip via command line:

      zip [zipfilename] [file to zip] {…zips and retains the original file}
      zip -m [zipfilename] [file to zip] {…zips and removes the original file}

      Also, I doubt very much that your email address is [email protected]. If you’re going to ask for my help, the least you can do is not use a fake email address that couldn’t possibly exist.

      • Donna

        You would have to be out of your mind to post your private email address on a public site.
        (We are EXTREMELY surprised snipe.net even asks such a thing.)
        You would instantly get tons of spam.

        Besides, what POSSIBLE difference could it make if my email address is “[email protected]” or “[email protected]”.
        Why do you even want to force me to give it out???

        I thought this site was about “great questions” and “great answers”, not “we demand you give out your private email address, to us, or to everyone”.

      • http://www.snipe.net snipe

        Actually no – almost every single blog that asks for your email address never posts it publicly. (That’s why it usually says “will not be displayed”.) The email address is used to be able to approve you for future comments immediately once we see that you’re a live human being, so that your comments aren’t held pending approval until we get to them. Your email address is never displayed, and we don’t actually collect it – I can see it in the disqus admin, but collecting such information would be absurdly tedious.

        Not to mention, another reason we use disqus is so that people can post using their Facebook or Twitter login, so they never have to give us any email address at all but can still be approved. Without some sort of identifying information from you, we cannot approve you as a “live human” who can post without being held for moderation.

        Providing your email address also allows you to get emailed notifications when someone has replied to your comment here, rather than having to come back and refresh 100 times.

        This is the way most blogs work – it’s not really new. Been that way for quite a few years now.

  • kerrse

    How can i turn off the green light when the isight camera is recording 

    • http://www.snipe.net snipe

      I am unaware of any way to do that from a software perspective. :(

  • EboTimbo

    EJECT A Stubborn CD or DVD         In Mac OS if a CD or DVD will not eject, restart the machine and hold down the left mouse button until the machine is fully booted and up. This gives the command to eject the CD or DVD from the tray.  Works every time.

    • http://www.snipe.net snipe

      Sure, but sometimes you don’t want to have to restart