Useful Mac OS X Terminal Commands

Terminal.app iconMac OS X is a very powerful OS. One of it’s most amazing traits is how well it scales from being dead easy to use for novice users to allowing power users near unlimited capabilities. The former comes from the excellent UI design and GUI tools that Apple has built, the latter from Mac OS X’s FreeBSD Unix underpinnings. While most users will never have to touch the Mac OS X Terminal application to go about their day to day computing, those who like to push the limits of what they can do will most likely spend at least a bit of time at the command prompt. This article is for those aspiring command line gurus, like myself.

I’ve been using Mac OS X since the Public Beta was released in 2000. Over the years, as I’ve delved further into learning about the operating system’s Unix underpinnings, I’ve always kept a Stickies note handy where I would paste terminal commands that I thought might come in handy at some point in the future. Every one of these commands required countless Google searches and time scouring the results to find solutions to the problem I had at the time. It’s in the interest of saving others from spending too much time searching that I present my Stickies Mac OS X terminal command list. I hope these help all of you as much as they have helped me.

A warning for newbies:

If you’re not familiar with the Mac OS X Terminal, it’s an application that allows a user to directly execute Unix commands by typing them into text based command prompt. You can find the Terminal application in /Applications/Utilities on your hard disk. Please be aware that the terminal and Unix commands are very unforgiving. A simple typo or misplaced punctuation can mean the difference between your command working and you deleting or overwriting something important, especially when executing commands with administrator rights (via the sudo command). If you aren’t sure what you’re doing, it’s better to take the time to research the answer than to simply guess and possibly mess something up you won’t know how to fix. This article is not meant to be a tutorial on how to use the Terminal. If have never used the terminal, may I suggest you visit O’Reilly’s MacDevCenter which has some good tutorials to get you started.

The Commands

Disclaimer: Please note that I cannot be held responsible for anything bad you do to your computer while attempting to use any of these commands. Use them at your own risk!

Eject Stubborn CDs

    disktool -e disk#

I found myself one day with a CDROM stuck in my CDROM drive, but no way to eject it. It didn’t show up in the Finder or on the Desktop, and hitting the “Eject” button on the keyboard did nothing. I found and tried two or three other terminal and GUI based solutions from sites like Mac OS X Hints, but none of them worked except this little gem. I haven’t had to use it for years and it may not work for every stuck CD, but this one saved me from having to reboot my computer to get a bad CD out. Type disktool by itself to get help.

Unmount Stubborn Network Volumes

    umount -f /Volumes/volumename

Like the stuck CD issue above, I have once or twice come across a network volume that I could not disconnect. Substitute the stuck network share’s name in place of “volumename” in this command. You can view the pdisk man page to get help by typing man umount at the prompt and hitting return.

Delete partition table on iPod

    pdisk

This menu based terminal app saved a dead iPod. I once made the mistake of formatting my iPod as an external FireWire drive as some non-standard format (maybe NTFS or Unix File System). After doing that, I couldn’t re-initialize the iPod using the latest iPod updater. While I don’t remember exactly the steps I used to get the iPod working again (it had to do with deleting the partition table), this tool is what did the trick for me. You can view the pdisk man page to get help by typing man pdisk at the prompt and hitting return.

Restart Bonjour service

    Stop Bonjour service:
    sudo launchctl unload /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
    Start Bonjour service:
    sudo launchctl load /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

A while back I had a problem where one of my Macs would periodically fail to broadcast it’s Bonjour name for things like print and file serving. Rebooting the computer would resolve the problem for a time, but it would always come back. Rather than having to reboot my computer all the time, I searched for and found this handy set of commands for stopping and starting the Bonjour service which would do the trick until the problem occurred again. Since this was a service, it was great not having to reboot. I eventually solved the problem, which if memory serves was caused by my router, but I felt it was handy to hold onto this in case I ever needed to stop or restart the Bonjour service again. These commands need to be run as administrator, which is why they are prefixed with the sudo command. You will be prompted to enter your admin password to execute them.

Restart Apple Remote Desktop (ARD) service

    sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -restart -agent -menu
    Note: This is a very long command that should all be typed or copied/pasted as one line.

I had a problem with Apple Remote Desktop once where if I left a remote control session running for too long (say overnight), the remote display would no longer refresh automatically. In my quest to avoid rebooting my server, I found this handy command that restarts the ARD service, which listens for and manages remote control requests. This has to be run on the machine you want to control normally. Since I was having problems remote controlling said computer, I would log in remotely via SSH by typing ssh ip_address and execute the command while logged in remotely. Very handy indeed. Being able to remotely login via SSH is no doubt one of the most useful reasons to learn the command line. All terminal commands that are covered in this article can be executed remotely. You can view the SSH man page to get help by typing man ssh at the prompt and hitting return.

Reset Network Interface

    Shut down network interface:
    sudo ifconfig en0 down
    Start up network interface:
    sudo ifconfig en0 up
    Note: Substitute en0 for appropriate network interface id (a.k.a. BSD device name). You can use System Profiler (in the /Applications/Utilities folder) to find the BSD device name for the network connection you want to reset.

I can’t remember why I needed to do this. Might have been while I was troubleshooting that Bonjour service issue, or maybe I was trying to change the connection’s MAC hardware address. Doesn’t really matter. The point is you can use this combination of commands to restart a network interface. These commands use the SUDO command to run using administrator rights and so you will be prompted for your admin password.

[Edit: Thanks to commenter "why o why" for pointing out, "it should be noted that if you remotely shutdown the ethernet interface you are using, it will be difficult to get it back up remotely ;) "]

Restart Apache Webserver

    sudo apachectl graceful

While restarting Mac OS X’s built-in Apache web server is as easy as toggling the “Personal Web Sharing” service’s status in the Sharing preference pane, you can’t do this easily remotely unless you can remote control the server in question. Over a modem connection, this can be near impossible or at least very painful. This command comes in handy if you’ve modified the Apache configuration file via a remote SSH connection as the changes only take effect after the Apache service is restarted.

Restart MySQL Server

    Stop MySQL Server
    sudo /Library/StartupItems/MySQLCOM/MySQLCOM stop
    Start MySQL Server
    sudo /Library/StartupItems/MySQLCOM/MySQLCOM start

Not so long ago I installed the MySQL open source SQL database on my web server so I could run a phpBB based forum site. MySQL also happens to be the database that runs the Art Of Geek blog. These two commands are useful for stopping or restarting the MySQL service via SSH, or if like me you have ever broken your MySQL installation just enough for the MySQL preference pane to stop working.

Change a file’s type code to make an AAC file an iTunes/iPod audio book

    /Developer/Tools/SetFile -t 'm4b ' filename.m4b
    Note: You must have the Mac OS X developer tools installed for this command to work!

This gem of a command will set the type code of a file to ‘m4b ‘ (there is a space after the ‘b’), which tells iTunes it’s an audio book. This is required if you have ever ripped an audio book from CD, and want iTunes to see it as an audio book. The file must be an AAC file, and you need to change the file extension to .m4b. The truth is, this command can come in handy any time you need to implicitly set a file’s ‘type’ or ‘creator’ codes. You can view the SetFile man page for help by typing man SetFile at the terminal prompt and hitting return.

That’s all the time (and commands) we have for today folks! I’ll post more in a future article!

30 comments to Useful Mac OS X Terminal Commands

  • me2

    It’s not command line based, but if you have a troublesome CD/DVD the following also works:

    Shutdown your computer.
    Turn it back on and hold the mouse button all of the way through the boot processes.
    The CD/DVD will auto-eject.

  • A very helpful suggestion me2, and one that every Mac user should know. Sometimes however, we would like to avoid rebooting our Mac for various reasons like we’re running a server, the system is busy with a task or we’re just really anal about resetting our up-time! ;-)

  • Cil

    I had the same problem once and quite easily solved by starting iTunes and hit the eject button….. all the other solutions (rebotting and holding mouse etc) did not work

  • Alex

    to eject a cd, try the following command in a terminal:

    drutil eject

  • why o why

    it should be noted that if you remotely shutdown the ethernet interface you are using, it will be difficult to get it back up remotely ;)

  • Stephen Bigelis

    My personal fav:

    /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background &

    puts the screen-saver on the desktop until you close the terminal window.

    and if your “open with” command ever gets cluttered up with a bunch of old software you don’t use anymore try:

    /System/Library/Frameworks/ApplicationServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

    finally everyone who doesn’t leave their computer on all night should run the following every month or so.

    sudo periodic daily weekly monthly

  • mark

    @Stephen Bigelis: You may also use man screen for your screensaver as desktop background!

    http://codesnippets.joyent.com/posts/show/1508

  • Good info for noob mac users like myself. Well written sir.

  • Brian

    An easier way to resolve the ARD remote refresh issue is to kill the AppleVNCServer process on the remote machine. The process will automatically relaunch.

    SSH in and run “top” or “ps -ax | grep AppleVNCServer” to discover the process ID and then run the kill command.

    This way you don’t have to remember the full path to the ARDAgent app.

  • [...] I was still able to access it through both Server Admin and SSH. After a little research, I found this useful page of commands, which includes this [...]

  • yap text is still a king :-D

    i have installed CLIX from apple download site, a compiled unix command that can you run with click on your mouse :-D

    anyway i am still looking for good guide unix command on mac os x

    cheers

  • I’ve been trying to make use of CLIX too. It’s a great tool but I can never find what I’m looking for in it. What I’d really like is a CLIX file of commands for all the MySQL functions.
    Your list of commands above are a good set. Thanks.

  • Trunko

    Can someone out there tell me PRECISELY how to use terminal to ‘bless’ a start up volume, so it boots?

    My work drive (OSX 10.4.6) hangs, and wont complete start up. I ran Tech Tool Pro and the following appeared:

    “Startup File
    This test checks the Startup file. The parameters within this file contain the information used by the computer ROM to determine what program will boot the computer as well as other system-specific aspects.
    Startup File ”

    Clearly I need this file, or to create a new one. There is a bootX file still, but that may be irrelevant.

    What to do? bless? How?

    BTW, I’m an audio engineer and no jack shit about terminal unix commands.

    • I’d suggest posting your issue on Apple’s Support Discussion Forums. You’re much more likely to have your problem resolved there.

      You might try the following:
      1. Boot in safe mode (hold SHIFT while booting)
      2. Boot from Mac OS X install disc, run Disk Utility and repair the disk as well as disk permissions.
      3. Boot from an external hard disk and attempt to install the 10.4.11 Combo update for your processor type, Intel or PowerPC.

      Good luck!

  • Trunko

    Thanks for replying, really!

    I’ll try and install an update across the drive in question, I have another bootable ‘admin’ drive.

    Trying the ’safe boot’ now…
    thanks once again!

  • Ari

    I am trying to find a command that will show me emac battery voltage. This way I could look at a whole lab and see what batteries are getting low.

    • If you’re talking about the motherboard battery that keeps the system clock and other settings, I don’t think the Mac has a sensor to check it’s level like a MacBook’s main battery. If it did, I think you’d find the results in System Profiler, in which case you’d be able to use the AppleSystemProfiler command to get the result.

  • Hey can U please help me?
    Look
    I habe no idea what is going on with my Terminal

    when I launched the terminal.app
    it doesn’t do any command it looks more loke a Text editor
    every thing I wrote and press enter just jump to another line and nothing happens
    Look
    http://triny.org/no-site/pictures/print/erro%20com%20terminal.png

    • Check your Terminal preferences. On the Startup tab, make sure you have the “Shell opens with:” set to “default login shell (usr/bin/login).

      You might also try deleting the com.apple.Terminal.plist file located in ~/Library/Preferences/

      That’s the Library folder in your user account’s home folder, not the one in the root of your hard disk.

      Good luck!

  • WeAreGeek

    LMAO! Mac users using terminal commands? Get serious! Anyone capable of seeing the advantage in that, should install a true free OS.

  • BlackMacX

    Too bad WeAreGeek forgot the first and most important aspect of being a troll (and being a free OS user too); remember sudo rm -R / to all your comments; it would show your maturity and that you’re an open minded OS user. Mac is a serious OS, it might not be free; but it has a great balance that of free OSes don’t.

    • TxMac

      You’re a d1ck for posting that. DO NOT TYPE IN THE COMMAND he listed! It will erase your harddrive!!

    • I think it’s pretty obvious that BlackMacX was being facetious TxMac, so no need to overreact. That being said, your warning about not typing the command doesn’t hurt.

      This is a good lesson for anyone new to terminal to learn: Don’t type commands in without understanding what they do or what they are for.

  • WeAreGeek

    True: Mac OS X is a great OS and there’s a lot of commercial software available for it, and that’s exactly what causes this “balance”. It has nothing to do with the OS, but with marketing.

    I admit: that was a bit immature and short sighted. Please accept my apologies. I know some OS X users and they’re all Mac fan boys. They don’t care at all how their hardware or software is build as long as there’s a shiny apple on it. When they see a terminal they panic. Hence my reaction. Most Mac users won’t ever use these commands.

    False: I’m not jealous at all. I could install Mac OS X on all my PC’s if I wanted to, but I won’t because I like to be in control of my machine. They just ripped some BSD code and build their own GUI on top of it without giving anything back to the community. Suckyness!

    • Apple does contribute a lot to the open source community. Any improvements Apple has made to BSD they have certainly contributed back, and trust me Apple has put a lot of effort into BSD, as well as any other open source project they’ve participated in such as WebKit (Konqueror).

      As for most users not having any use for Terminal and CLI, well that’s a good thing if you ask me. First off, it’s a true testament to the work Apple has put into Mac OS X that an average user can accomplish absolutely everything they want to on their computer without ever having to type in a command yet even hard core users can accomplish anything they want using the tools they prefer. It just doesn’t get any better than that.

  • [...] Useful Mac OS X Terminal Commands « Art Of Geek (tags: linux appletv unix osx mac software programming terminal command commandline macintosh shell useful computing os technology commands troubleshooting) [...]

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>