10 Terminal Commands That Every Mac User Should Know

10 Terminal Commands That Every Mac User Should Know

Tutorial Details
  • Topics: Terminal, Command Line
  • Difficulty: Intermediate
  • Estimated Completion Time: 20 Minutes
This entry is part 5 of 10 in the Taming The Terminal Session
« PreviousNext »

The Terminal in OS X is often, in my opinion anyway, an overlooked area of the operating system. Most newcomers to Macs look at it with fear, as the sight of anything code-related or advanced is enough to put them off for life. Fortunately, you don’t have to be scared of Terminal and think of it as a feature just for the tech-minded. Knowing some basic Terminal commands can really enhance your experience of OS X. So, for all the beginners out there, here is my pick of the top 10 Terminal commands that every Mac user should know.


Before We Start…

Before we start tinkering around with it, it is probably worth taking a couple of minutes to tell you exactly what Terminal is (all in a non-technical sense, of course!).

Terminal OS X

Terminal.app running under OS X (Lion).

Terminal (officially called Terminal.app) is, strictly speaking, an emulator and works off most typical UNIX commands (OS X is a UNIX-based system, as opposed to Windows, which is NT-based). Unlike OS X, which has a graphical user interface (shortened to GUI), Terminal works off a text-based interface and all commands have to be typed in – this may be the reason why some people are scared off by it!

We won’t go into massive detail on how Terminal works but it’s best to learn the following three commands before we start this tutorial:

  • ls - list the contents of a particular directory
  • cd - change to another directory (as in DOS)
  • sudo - authenticate yourself a superuser to gain extra security privileges

Tip: >Be extra careful when using the sudo command. You will usually have to enter your OS X system password and any mistakes can permanently mess up your system for good. When in doubt, leave it out!

1. Disable Lion/Mountain Lion’s Pop-up Accent Window

For people like me who write a lot of foreign accents then the pop-up accent window that came up when you hold down a letter for a longer period is extremely useful. But for those of you who want to write lots of repeat letters, it can be quite annoying. Use this simple trick to disable it:

defaults write -g ApplePressAndHoldEnabled -bool false

Hit Return then log out then back in again to see the changes. If you want to change back to the norm, then simply substitute false for true in the above.

2. Change the Default Backup Periods in Time Machine

By default, Time Machine backs up every hour when it’s connected but this can be changed through Terminal. To do so, simply enter the following command:

sudo defaults write /System/Library/Launch Daemons/com.apple.backupd-auto StartInterval -int 1800

This makes Time Machine backup now every 30 minutes. To change it to what you want, simply replace the 1800 with the interval time in seconds (so 15 minutes would be 900 seconds, therefore enter 900). Also be aware that you are using a sudo command, so make sure that you’ve typed the command exactly right before you hit Enter (and you’ll also be prompted to enter your system password).

3. Drag Dashboard Widgets onto the Desktop

I always forget that the Dashboard exists on my Mac. In fact, I hardly ever see a need for it. But I think that’s mostly down to the fact that the widgets are hidden away inside the operating system. To be able to drag your Dashboard widgets onto your desktop, then enter the following command:

defaults write com.apple.dashboard devmode YES

You must log out then back in again and to drag a widget onto your desktop click and hold down on it then press F12, which dismisses it from the Dashboard and allows you to drop it onto the desktop. Your widgets now float above open applications, similar to the Gadgets on Windows 7.

To disable this feature, simply replace YES with NO in the above command. Again, you’ll have to log out then back in again, unfortunately.

4. Disable Lion/Mountain Lion’s Auto-restore Feature

This really bugged me when I first upgraded to Lion – the fact that when you open up something like Preview, all your recently accessed PDFs coming popping up in your face, even if you closed them down properly. Unfortunately there’s no universal command to disable it across the entire OS so you’ll have to do it for each individual program. If you want to disable it for Preview, then enter the following command:

defaults write com.apple.Preview NSQuitAlwaysKeepsWindows -bool false

QuickTime is another culprit in my eyes, again just enter:

defaults write com.apple.QuickTimePlayerX NSQuitAlwaysKeepsWindows -bool false

Make sure you quit and restart the apps to see the changes and if you want to reverse anything, simply substitute false for true (I’m sure you’re getting the hang of this by now!).

5. Show Hidden Files in Finder

By default, the Finder in OS X hides some files away from view (mostly irrelevant ones) but if you want to see everything on your computer, then enter in the following command:

defaults write com.apple.finder AppleShowAllFiles TRUE

Again, substitute false for true to hide them again.

6. Change the File Format for Screenshots

Screenshots using the built-in shortcut for OS X are really useful but by default they are saved as PNG files. If you want to change this, then type in the following command:

defaults write com.apple.screencapture type file-extension

Replace file-extension with the three letter abbreviation of your desired file format, so for JPEG format simply enter JPG and for Acrobat PDF format, PDF. Terminal doesn’t have to be difficult, does it?

7. Kill the Dashboard Altogether

Above, we went over the command to drop Dashboard widgets onto the desktop, but if you want to get rid of the Dashboard altogether (for example, if you’re running an older Mac and have memory issues) then you can do via this simple Terminal command:

defaults write com.apple.dashboard mcx-disabled -boolean YES

Then, restart the Dock using the following command:

killall Dock

Et, voilà! No more Dashboard. If you find yourself afterwards lamenting after the good old days, replace YES with NO in the top command and repeat the process.

8. Securely Erase Free Space

When you delete files on your Mac, OS X still leaves fragments of the file all over the free space on your hard disk drive, until these are written over by new files. If you want to securely delete all the remaining fragments on a hard disk drive (for example if you’re going to sell your Mac), then execute the following command:

diskutil secureErase freespace 3 /Volumes/name-of-drive

Replace /name-of-drive with the drive you want to erase. This command uses a special algorithm to wipe over each free area of space 35 times, far above the US Department of Defense’s standard, which only requires 7 passes. Be aware though that this process can take days on larger drives.

On a side note, the command diskutil is a really useful one and allows you to manage local disks and volumes directly from the Terminal (a list of sample commands is given). Be aware, though, that most commands require root access.

9. Control Your Mac Remotely

Being able to control your Mac remotely via SSH, or secure shell, is far more advantageous than screen sharing as it uses less system resources and less bandwidth. The first thing you will have to do is enable Remote Login on your Mac, which you can do by heading over to System Preferences then Sharing then click on Remote Login.

SSH Mac
Be sure to enable Remote Login on your Mac before you start anything.

A little green light should come up, along with an IP address that you can use to log in via Terminal. The connection between the local Mac you’re using and the remote one (i.e. your own one) is secure and encrypted, meaning that any data being transferred between the two cannot be intercepted.

To log into your Mac on another Mac, execute the command:

ssh -l username remote-address

Replace username with the username you’d use to log into OS X and remote-address with the IP address given to you in the Sharing pane. You can now control your Mac and execute Terminal commands remotely, a real plus.

10. Talking Mac!

And last but not least, if you want your Mac to speak to you, then simply use the say command, as in:

say Hello World!

The words will be spoken in your Mac’s default voice but if you want to change this, then simply head over to System Preferences then either Dictation and Speech (on Mountain Lion) or simply Speech (on Lion). Here you can select different voices and download new ones from Apple’s servers if you fancy. Another “useful” feature is the ability to convert an entire text file into speech, if you so fancy. Simply enter:

<strong>say -o audio.aiff -f FILENAME.txt</strong>

Replace FILENAME.txt with your own file. This will create a reading of your file as an AIFF file audio.aiff in Terminal’s default directory.

Tip: Ever wondered if the Mac operating system is pronounced “OS ten” or “OS ex?” Your Mac knows the answer! Try typing “say OS X” into Terminal. The result may surprise you.

Conclusion

I hope that this little tutorial has helped you demystify the Terminal and realize that it’s really not that bad to use – you can achieve a lot more using it and if you want to customize OS X beyond simply changing the wallpaper, then it really is your first port of call.

I hope you have plenty of fun with the commands mentioned above, please feel free to share your own favourites in the comments section below for the benefit of all Mactuts+ readers!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • verpixelt

    Thanks for this nice and handy commands. I love the “get rid of dashboard” one! I rly never use it so hey thanks a lot =D

    • James Cull
      Author

      No problem and thanks for your feedback :)

      • http://www.facebook.com/jbuts Jonathan Butler

        how do you reverse it if u want to

  • Johnny Winter

    > Tip: Ever wondered if the Mac operating system is pronounced “OS ten” or “OS ex?” Your Mac knows the answer! Try typing “say OS X” into Terminal. The result may surprise you.

    If you type OS X into a text edit document (or Notes, or Word, or Byword) then select it and ask your Mac to read it to you (right-click and select Speech > Start Speaking)

    Your Mac will then read “OS X” to you.

    The result may surprise you! ;-)

  • Marcelo dos Santos Mafra

    You could post an article to show how to change Mac OS X terminal’s behavior to be like other UNIX system. Actually like Linux. The behavior of the keys is annoying.

    http://lifehacker.com/225873/mac-switchers-tip–remap-the-home-and-end-keys
    http://doublecommand.sourceforge.net/
    http://simplesideias.com.br/configurando-o-terminal-no-mac-os-x/

  • Philippe Gilbert

    the first tip is pretty useless in Lion as the option is in system setting -> keyboard. And it’s actually much faster to learn the option+something shortcuts than waiting for the pop-up to open. Example: to do ´, you just have to push option+e than the letter you want under it. for `, it’s option+ `, for ˆ it’s option+i, for the ç it’s option + c, for ¨ it’s option + u, for ˜ it’s option + n etc…

    • J de M

      Where is the option to turn it off in System Prefs? Not on my Mac you can’t (Mountain Lion). I use them because I speak other languages and type fast, but I can imagine the pop up happening if you only speak your mother tongue would be annoying. Having said that, you have to hold the key down for quite a long time before the pop up appears! And yes, I agree that the option commands are much quicker. No doubt those who have been using Macs for a long time (like myself) and who have got used to them and do them automatically without having to think will stick to those. :-)

  • http://www.webmaster-source.com redwall_hp

    Note: You should not use the secure erase (or the secure empty trash function!) if your Mac has solid state storage. It is bad for SSDs, and will shorten their life. (Also, defragmenting would be a horrible and absolutely unnecessary idea, too.)

    • Cat

      I thought SSD’s were supposed to have a pretty darn looooong life? Thought they were kind of tough as nails, and didn’t really wear out for ages because of the no-moving parts thing. I’m surprised at what you are saying, as it makes them sound fragile.

      • http://www.facebook.com/Titanyium Jonathan Fuchs

        The reason he said that was because SSD’s lifespans are limited to a finite number of reads/writes to the disk. Therefore, every time you write over a file you’re securely deleting, it’s also reducing your SSD’s lifespan. Reason is each file you securely erase, using this command, executes 35 read/write commands on the drive. Make sense now?

        • Cat

          Thanks for that. Yeah, that makes sense, and it sucks! So really, would they probably have a shorter lifespan than hard disks? I wonder aloud… I might do some googling.

          That’s a shame, I thought they would last for a long time….

  • K

    hi! the last feature to convert the entire text into speech – doesn’t work? and where’s Terminal’s default Directory? Thanks!

  • http://www.facebook.com/williamgummy William Landrum

    Hi i started using terminal once, and now it opens every time i restart my computer. What command will stop this?

    • Ti Du

      Right click on the item in your dock and select options. Uncheck the ‘open at login’ item.

  • http://www.facebook.com/mmci1525 Mark McIntosh

    I tried the code to bring widgets to the desktop, but it’s not working for me. they don’t come across. am I doing something wrong? I’m on 10.8.2

    • turbominicooper

      probably.

      run the code
      log out
      log in
      hit f3 for dashboard
      click and hold widget
      click and hold f12
      click f3 to close dashboard
      release on desktop

      simples!

  • AmyRedstone

    Gotta log out and in again to make the screenshot fix work, but YAY no more opening in photoshop to change formats! Thanks!

  • pineapple

    how do you end say command??

    • Rahmat Awaludin

      use ctrl+c

  • Rory

    I learnt from ardensdd.com

  • test

    Hello, nice stuff ! I tried to do the talking with a txt file I saved of my desktop but it doesn’t work for me. I wrote this say -o audio.aiff -f hello.txt and I get this: -bash: syntax error near unexpected token `newline’. Any suggestion what I ‘m doing wrong?

    • jj

      I think you forgot the opening and closing tags

  • electricbullet

    on number 3 cant you just hit enter and type something along the lines of killall finder or killall dock?

  • electricbullet

    instead of logging out

  • Diego

    say -o audio.aiff -f FILENAME.txt this doesn’t work

  • poo poo is better

    this site stinks poop is way tastyer

  • poo poo is better

    werghdnj

  • poo poo is better

    aergryrgh

  • poo poo is better

    asrhthyrjhrtdlk

  • poo poo is better

    sthhsatehaf

  • poo poo is better

    aryregsz

  • anny thingy

    Hello, thank you for the info, have learnt a few things. I am a mac and terminal newbie so please forgive me for my following possibly silly query.

    I used terminal for the first time yesterday. I was given a set of instructions to follow but I did not follow them exactly and now I have convinced myself that I have gone and messed something up. Especially so, as I am now reading in a few places as you must be very careful with “sudo”

    I was using a terminal method posted here by parallels http://kb.parallels.com/en/114624 to remove the desktop 8 application (I chose to use the terminal method as I was wanting to remove all traces of the program)

    The simple instructions I was meant to follow were:
    1. Launch Terminal application from Applications/Utilities
    2. Paste the commands into Terminal (one by one; hit return after pasting each command)
    sudo su
    (type your Administrator password and hit return. You will not see characters while typing)
    for pid in $(ps aux | grep “Parallels Desktop.app” | awk ‘{print $2}’); do echo kill -KILL $pid; done

    for kext in $(kextstat | grep parallels | awk ‘{print $6}’); do kextunload $kext; done

    rm /System/Library/Extensions/prl*

    rm -rf “Parallels Desktop.app”

    *But instead, this is what I entered:

    sudo su
    (hit return)

    (entered my password)zx
    (hit return)

    for pid in $(ps aux | grep “Parallels Desktop.app” | awk ‘{print $2}’); do echo kill -KILL $pid; done
    (hit return)

    for kext in $(kextstat | grep parallels | awk ‘{print $6}’); do kextunload $kext; done
    (hit return)

    (at this point I thought it wasn’t doing anything and for some stupid reason though I should try entering my password in again, twice!)

    zx
    (hit return)

    zx
    (hit return)

    (closed terminal)

    so after i entered in my password twice, i freaked out and just closed terminal down.

    I then opened terminal again and this time followed instructions exactly, everything worked and parallels removed.

    So I would just like to ask some terminal techy person, if by typing the ‘zx’ into terminal when I did, could this have done any damage or messed with something?

    I don’t know if this is related, but I just noticed today that after restarting my mac, it will not open with my current chosen desktop background but will instead open with a previously chosen background. I have re-chosen the background, shut down and restarted a couple of times and have the same issue each time.

    See what I mean, I am paranoid I have messed with something!!

    Any insight greatly appreciated, thanks in advance.

  • http://JonathanGraft.com/ Jonathan Graft

    I tried #6. When I open the app Grab and take a screenshot, it still wants to save the image as a .TIFF file but I set it as .PNG just prior.