Introduction to the OS X Terminal Application

  Wayne Linder       January 22, 2007      Tutorials Mac Apps


The Terminal is an application from Apple used to gain access to the power that is “under the hood” of the operating system. Historically, there have been two ways to access the operating system; a Graphical User Interface (GUI) shell, or a command-line shell. The Mac OS graphical interface allows us to do our daily computing tasks in a manner that is familiar to us – if we want to throw an item away, we drag it to the trash. What is really happening “under the hood” is that the kernel is given a command to remove a file system object – a file or folder – from a directory. To the user, everything looks familiar – throwing something in the trash to delete it or dragging an object to another location to move it. The graphical interface works the way we work with physical objects in the real world. What is going on in the bowels of the operating system is normally “hidden” from the user. That is, unless, you use a command line shell to directly interface with the operating system. How is that done? Let’s move on.

Some have described the Mac OS as “Unix with a pretty shell.” That is, in essence, true, as the modern Mac OS is built on something called the FreeBSD kernel. The kernel is the central component of an operating system and connects the hardware with the software – communicating between the two. When you launch the Terminal, you are opening a “shell” or command-line interface to the OS. The Terminal is used to issue commands to this deep layer of the OS through the use of typed commands. Yes, I hear you groaning out there. “Didn’t we get rid of command line interfaces years ago?” For the most part, yes we did. However, there are times when there either isn’t an application that will do what you want to do, or using the Terminal is faster.

Now that you have a better idea of just what the Terminal is, let’s dive in and start using it. However, before we start on examples of what we can do with the Terminal, let’s instill in you a healthy dose of fear. For example, here is an example of what NOT to do in the Terminal.

If you were to launch the Terminal application and type the following command: sudo rm –r /System then hit the enter key, you would be prompted for your Administrator password. If you then entered that password and hit the Enter key, your computer would dutifully and cheerfully erase the contents of your System folder – or at least as much of it as it could before coming to a crashing and permanent halt. This is, as Martha would put it, “a bad thing.” Why did it do this? Because you told it to! The above command tells the computer “Hey, I’m the Administrator (sudo = superuser do) and I want you to remove (rm) the following folder (/System) and all that is in it (-r = recursive). Fun, eh? So remember that what you do in the Terminal doesn’t always come with all of the nice warnings that the OS gives you. It assumes that since you are in it and have the proper power, you know exactly what you’re doing.

Getting Nerdy

How, exactly, do you get to the point where you know what you’re doing? Let me explain the beauty that is the man command. Man is your built-in instruction manual. Say, for instance, that you wanted to know about this scary command named rm. Type man rm in the Terminal and it will display what the command does, how to modify it and some examples. It’s kind of like Apple’s Help system (without all that waiting for it to connect to the Internet). Since man is essentially a program running within your Terminal application, you need to exit it to return to the command prompt. To exit a Terminal program, type Control Z.

Mac terminal application

So, let’s get comfortable with the Terminal by doing a little touring around our directory structure. By default, when you launch Terminal, you are in the current user’s home directory. As you can see from the screen shot, my machine name is Linder, followed by the tilde (~) character, which represents the home directory of the current logged-in user – in my case, wlinder. If you get lost, you can easily find out where you are by typing pwd, which is short for present working directory. To change directories, you type cd. If you want to go up a level – to the parent of the folder you’re in – you type cd .. (cd, a space, followed by two periods). If you want to go down a level, you type the name of the folder you want to go to. For instance, if I was in my Users directory and wanted to go to my Desktop folder, I would type /Users/wlinder/Desktop and hit Enter or Return. How do you know what to type since you can’t see the folders? That’s where the ls command comes into play. Any time you type ls, the Terminal will list all of the files and folders in that directory. All of those that are visible, that is. For instance, type cd / and hit Enter. You are now in the root level of your hard drive. Type ls and hit Enter. You will see your normal folders, such as Applications, System, and maybe a few more. Now type ls –all and hit Enter. You will now see all of the invisible files and folders that the OS usually doesn’t show you. Play around a bit and get comfortable. Done? Good. Now that you’ve had your fill of typing around, let me tell you a few shortcuts. If you’re trying to go to a directory buried layers deep and don’t want to type the whole thing, you can just type cd followed by a space, and then drag the folder into the Terminal window. Your path will be typed for you (nice for those of us who can’t type very well). Another shortcut is the Terminal’s version of auto-complete. The OS already knows what is on your hard drive. It just wants you to type it in to make sure. Say you wanted to go to your Desktop in as few characters as possible. You could type cd ~ then hit Enter and you’d be in your home folder. If you then typed cd /De and hit the Tab key, it would fill in the rest of the word Desktop. Nice, eh?

All right, you should now be comfortable with navigating around your directory structure and you’re probably wondering if this is all you can do. Hardly. Let’s have some fun with the Terminal in a section I will call “Cool Terminal Tricks.”

Number one is the Terminal’s version of Activity Monitor. To get there, type top and hit Enter. You are now running a program that lets you see running processes, memory and more. There are many many options used in programming, system debugging and more. Feel free to look through the man page for more information. Remember, since TOP is also a running program, you have to quit it by typing Control Z.

I like 10.4, and Spotlight is one of the reasons I like it so much. I use it every day. What about those who don’t have 10.4 – and therefore no Spotlight? Terminal to the rescue! One of the greatest commands in the Terminal is grep. Want to find something that Spotlight can’t? Grep it. Here’s how. Say you were looking for all files containing the word “Bubba” inside a directory. Open up the Terminal, cd to the directory you want to look in and type grep –r -i bubba . (translation – grep recursively, ignoring case, for the string “bubba” in the current directory) and hit enter. If your statement contains spaces, enclose it in quotes, like this: grep –r “grep is majorly cool” . Otherwise you will find all instances of each word. You can also use wildcards in your searches. If you did a grep for Appl* you would find all words beginning with Apple, such as Apple, Application and Apply. You can also use the period as a single-character wildcard. Say you were looking for pot, pit and pat inside a file (don’t ask why – it’s just an example!). You would search using the expression grep p.t /Directory/OtherDirectory/filename. However, this would also give you the words other words such as spot and spit. To limit your search to words that begin with a character, you use the caret (^) character, like this: grep ^p.t /Directory/filename. There are many more ways to use grep. Once you start using it, it will become a part of your daily toolkit. Do a search and you will find some great tutorials out there that can help you get the most from this supremely useful command.

What happens when you go into an application and change your preferences? In OS X, a file is written called a plist file. You can use the defaults command to read and write your preference files manually. Why would you want to do this? Because sometimes a programmer will put options in the plist files that you can’t get to using the preferences dialog. One of the best-known examples is the Safari Debug Menu option. This preference will display a Debug menu in Safari that gives you control over many options you otherwise would not have. One of my favorites is called Browser Agent spoofing. My bank had a major website overhaul recently and my Safari browser no longer worked. Every time I tried to get in, I received a message telling me that they only allowed browsers from a certain company in Redmond that shall remain nameless. A simple trip to the debug menu to change my browser agent, and the website thought I was running Internet Explorer on a Windows machine. It immediately rolled out the welcome mat and all was well – all the while not realizing that I was in Safari. Before we change the preference, let’s take a look at it. Type defaults read com.apple.safari and hit Enter. Your preference file for Safari will display in the Terminal. Quite a lot of stuff in there, isn’t it? To enable the Safari debug menu, type defaults write com.apple.safari IncludeDebugMenu 1 and hit Enter. Quit Safari if it is open and relaunch it. Congratulations! You now have the Debug menu.

That should be enough to get you started. These few things are just scratching the surface of the Terminal, but you should now have an appreciation of how much power is available to you. For now, get comfortable using the Terminal and experiment – remembering the risks, of course.

We hope you enjoyed this first tutorial. For more ways to “get your geek on,” be sure to check back periodically. I think we’ll have a lot of fun here at Nerdification Station. If you have ideas for articles and tutorials, feel free to share!

Subscribe to our email newsletter

Sign up and get Macinstruct's tutorials delivered to your inbox. No spam, promise!


About    Privacy Policy    Terms and Conditions

© 2023. A Matt Cone project. CC BY-NC-SA 4.0. Made with 🌶️ in New Mexico.