OS X revolutionized the Mac by building an elegant GUI over a BSD kernel. Most users didn't much care about the inherent security and stability of the UNIX foundation, but experienced UNIX users recognized the power underneath the Aqua interface. This article will introduce you to some of the power the built-in Terminal app and command-line interface (CLI) can unleash for Tiger users.
In /Applications/Utilities, you'll find the Terminal app. The Terminal is the doorway to the UNIX workings of Mac OS X. Keep in mind that both the Finder and the Terminal communicate with the underlying UNIX engine, but it's the user who chooses one method of inputting commands over another. In the GUI, the user clicks on icons to issue commands; via the CLI, the user types commands that accomplish the same tasks. The CLI displays something like this:
The command-line interface (CLI)
Underneath the "Welcome to Darwin!" greeting is the command line prompt. My 12" PowerBook is named tigerbeta; the
~ (tilde character) is a shortcut for one's home directory; tigerbeta1 is my short user name (in other words, the user who is logged in); all of this is followed by the
Just as files live in directories (/Applications/Utilities), the shell has a working directory. Let's issue our first command and type
pwd to see the full path to the shell's current working directory (
pwd stands for "print working directory," where "print" actually means display):
Last login: Tue Feb 22 17:35:46 on ttyp1 Welcome to Tiger Beta! tigerbeta:~ tigerbeta1$ pwd /Users/tigerbeta1 tigerbeta:~ tigerbeta1$
The full path to your home directory should be similar to mine (/Users/tigerbeta1), beginning with the forward slash character
/, which denotes the root, or top directory, of your filesystem.
At the top of the window, you'll see
Terminal - bash - 74x18 (your screen size will be a bit different). The
bash title tells you what shell you're using. A shell is a UNIX program that takes the commands you type and passes them to the system kernel. Another way to describe the shell is a "command interpreter." There are several different shells available in Mac OS X. Jaguar included the default
tcsh (an enhanced version of the Berkeley UNIX C shell) and Panther introduced the default
bash shell (GNU Bourne Again SHell) which has better international text features and is common in the Linux world. Tiger continues the default
bash option, but let's try out another command to see what other shells are included. After the
$ prompt, type
cat /etc/shells (with a space after
cat) and hit
tigerbeta:~ tigerbeta1$ cat /etc/shells # List of acceptable shells for chpass(1). # Ftpd will not allow users to connect who are not using # one of these shells. /bin/bash /bin/csh /bin/ksh /bin/sh /bin/tcsh /bin/zsh tigerbeta:~ tigerbeta1$
cat command ("concatenate") is run against the shells file in the /etc directory and gives us the output of the shells that are supported on Mac OS X. We see that a new addition to Tiger is
ksh, or the
korn shell. This will make many
korn shell fans happy; previously, to install
korn, one had to download and install a pre-compiled binary of the
korn shell for the Mac. Having the
korn shell provides maximum compatibility for Solaris users.
Let's start simple by changing the default greeting when our Terminal app launches. This lesson will introduce the command
sudo, the text processor
nano, and some new commands.
When you open the Terminal, you'll see the default greeting: "Welcome to Darwin!" (Darwin is a combination of technologies that make up Mac OS X, including the core BSD-based UNIX distribution). We can replace the Darwin greeting with something more personal and unique by editing the motd file (which resides in the /etc directory) using a text processor.
Remember that we're in our home directory when we open Terminal, so we'll change directories to the /etc directory using the
tigerbeta:~ tigerbeta1$ cd /etc tigerbeta:/etc tigerbeta1$
Note that the prompt is now showing us our current working directory, which has changed from our Home directory (tigerbeta:~) to the destination directory (tigerbeta:/etc). You can look at a listing of the files in this directory by using the command
tigerbeta:/etc tigerbeta1$ ls 6to4.conf httpd protocols X11 inetd.conf racoon afpovertcp.cfg mail.rc rmtab bashrc master.passwd rpc crontab motd shells cups mail sudoers
This is a partial listing; there are many more files in this directory than are shown above. (For the curious: the crontab file still exists in Tiger, but it merely directs the user to /System/Library/LaunchDaemons.)
We'll start by making a backup copy of the motd file in case we need to restore the file to its default. We'll try the the
cp (copy) command to make a copy of motd and rename it all in one step:
tigerbeta:/etc tigerbeta1$ cp motd motd.bak cp: motd.bak: Permission denied tigerbeta:/etc tigerbeta1$
Unfortunately, we hit a roadblock: we don't have permission to write to the /etc directory. In fact, only root can write to /etc and I'm logged in as merely my administrator self. As administrator, I have power--but not enough power to change core system files.
Instead of enabling root, we'll take the simpler (and safer) route and use the
sudo ("superuser do" or "substitute-user do") command to temporarily gain the highest level of privileges to make a copy of motd. To use
sudo against a command you want to run as root, type
sudo, a space, and the command. You'll be prompted for your password (not root's) and as long as you have administrator privileges, your password will run the command as
if you were root. But take heed: use
caution. You can seriously damage your system by running commands with root privileges!
cp command again with
tigerbeta:/etc tigerbeta1$ sudo cp motd motd.bak Password: tigerbeta:/etc tigerbeta1$
You can run
ls again to see the motd.bak file was, indeed, created (again, this is an abbreviated directory listing):
tigerbeta:/etc tigerbeta1$ ls 6to4.conf gdb.conf motd X11 gettytab motd.bak ftpusers inetd.conf php.ini.default
In the event you need to restore the original file, you would
cp command in reverse. However, note that the
cp command won't warn you that it's about to overwrite an existing file, so it's a good idea to use the option flag
-i to your
cp command. This option flag tells
cp to ask you to allow the overwrite action to be completed. Type in
y for yes or
n for no, which cancels the overwrite:
tigerbeta:/etc tigerbeta1$ sudo cp -i motd.bak motd Password: overwrite motd? (y/n [n]) y tigerbeta:/etc tigerbeta1$
Our next task is to edit the motd with a command-line text processor. Mac OS X has traditionally offered several options for CLI text editing:
vim, Emacs and
pico is the simplest to use for beginners, so that would be our natural choice. However, calling up
pico in Tiger gives us a new surprise! Instead of
pico, we get
nano, a "compatible but enhanced"
pico clone (for more information, see www.nano-editor.org). In Tiger, you can call either
nano and you'll get
Since we're dealing with a root-owned file, we'll use
tigerbeta:/etc tigerbeta1$ sudo nano /etc/motd Password:
If it's been less than five minutes since you last used
sudo and put in your password, you won't be prompted to enter it again. Since it's been longer than five minutes since my last
sudo use, I was prompted for my admin password. Once you enter your password and hit
Enter, you'll see this:
The file's text area is between the black GNU
nano header and the bottom two lines
of command prompts at the bottom. Use the down-arrow key to move the cursor down
line by line or use the page commands (the scrollbar doesn't allow you to scroll
through the document). The commands are prefaced with the carat character (
which is the equivalent of the
Control key. For example, to navigate to the next
page of a long text file, press the
Control key and the
V key simultaneously (
To get more information on all of the commands, press the
Control key and the
^G) for "get help."
When you're done with your creative hacking, you press the
Control key and the
O key (
^O) to "WriteOut," or save, the file. You'll be prompted to confirm the save. Just press the
Enter key to commit the changes. Press the
Control key and the
X key (
^X) to quit
nano. Open a new session of Terminal (File -> New Shell, or
N) to see your new terminal greeting:
Above, we used the option flag
-i with the command
cp. Option flags are used to modify how a command behaves. The
-i flag stands for "interactive," and adding it to the
cp command resulted in a prompt asking if it was OK to overwrite the file. There is no "undo" on dangerous commands like
remove, which is the same as "delete and empty the Trash"), and
move, which can overwrite other files by the same name), so it's a good idea to get into the habit of using the interactive flag.
However, it's a bit of a bore to have to type extra stuff when all you want to do is execute a command; it's easier to use an alias instead. In UNIX, an alias is shorthand for a longer command. Let's create some handy aliases for our "scary" commands. To do that, you'll need to edit your .bash_profile file:
Open Terminal or, if it's already open, type
cd to make sure you're in your Home directory. Then type the following at the command prompt and hit
tigerbeta:~ tigerbeta1$ nano .bash_profile
The .bash_profile file will open in the
nano text editor. Type in the following lines, hitting the
Enter key to move to the next line (space between the command and the option flag):
Save the file with
^O ("write out"), hit
Enter to confirm the save, and then exit using
Back at the Terminal, activate the changes by typing:
tigerbeta:~ tigerbeta1$ source .bash_profile
Now let's give it a try. Create a dummy text file in your Documents directory. Go back to the Terminal and look for your file:
tigerbeta:~ tigerbeta1$ cd Documents tigerbeta:~/Documents tigerbeta1$ ls BattleStarPlans.rtf Widgets Microsoft User Data tigerbeta:~/Documents tigerbeta1$
In my pristine Documents folder, I have a file called BattleStarPlans.rtf that I've decided can't fall into the wrong hands. So I'm going to delete the file before the Stormtroopers arrive, by using the
tigerbeta:~/Documents tigerbeta1$ rm BattleStarPlans.rtf remove BattleStarPlans.rtf? y tigerbeta:~/Documents tigerbeta1$
Since my alias is now in effect, I get prompted to confirm the remove, even though I only typed
To see the consequences of this, take a look in your Trash for the file you just removed. See it? Nope. The command
rm deletes and empties the Trash.
To see all of your aliases without having to open the .bash_profile file again, just type the command
alias at the prompt.
A few other useful option flags:
ls -a will display the names of folders and files and UNIX invisible folders and files, which are preceded with a dot:
tigerbeta:~ tigerbeta1$ cd library tigerbeta:~/library tigerbeta1$ ls -a . ColorSync Keychains .. Colors Logs .localized Cookies Mail Application Support Documentation Mail Downloads Assistants Favorites Preferences
-R: Another helpful flag for the
ls command that displays the contents of a directory, along with all of its subdirectories and their contents:
tigerbeta:~ tigerbeta1$ ls -R /etc/XGrid agent controller /etc/XGrid/agent: com.apple.xgrid.agent.plist.default /etc/XGrid/controller: com.apple.xgrid.controller.plist.default
-v: Used with the
mv command, this flag displays a fully detailed (or "verbose") explanation of what got moved:
tigerbeta:~/Documents tigerbeta1$ mv -v BattlestarPlans.doc ~/Documents/Secret BattlestarPlans.doc -> /Users/tigerbeta1/Documents/Secret
You can view the available option flags for commands by viewing the manpage (manual page). At the Terminal prompt, type
man plus the name of the command:
tigerbeta:~/documents tigerbeta1$ man ls
The manpage for the command
ls will appear, one screen at a time. It includes the name of the command; a synopsis of the syntax, including options and arguments; and a description of the command and how it works.
Here's a fun trick to do with the Terminal that affects the GUI--the famous animated-desktop trick. In this Terminal hack, you can change your desktop from a static picture to an animation. Along the way, we'll learn a trick to save on long typing (and typos).
Go to the Finder and then to the Screen Saver preferences pane. Select an animated screen saver; the new RSS Visualizer included in Tiger is a good choice for this exercise. Mine is set for Slashdot RSS. (Did my latest review get accepted yet?)
Now, you have a couple of choices. You can type in this loooong command:
tigerbeta:~ tigerbeta1$ /System/Library/Frameworks/ ScreenSaver.framework/Resources/ScreenSaverEngine.app/ Contents/MacOS/ScreenSaverEngine -background
Note: There is one space before the
-background (dash background). There are no other spaces in the entire command line.
or you can copy and paste the above command (cheater);
or you can use the Finder for a little help:
Open a new Terminal window.
Go back to the Finder and follow the path above up to ScreenSaverEngine.app.
Drag this app to your open Terminal window and drop it at your waiting command-line prompt.
Type (or copy/paste) in the last bit:
F11 and look at your desktop!
One caveat is that this takes up a huge amount of processing power. Open a new Terminal window and type
top and hit the
top gives a dynamic display of every program and background operation currently running by descending PID (Process ID) order. You'll see that the screensaver app is taking at least 10 percent of your system's attention.
To end the process, go back to the Terminal, where the desktop animation is still executing, and hit
^C). If the app turns psycho and won't quit, you can
kill it using
top and the Terminal. In the example below, the PID of the screensaver app is 223:
kill the offending application is the equivalent of using the Force Quit command in the Finder:
tigerbeta:~ tigerbeta1$ kill 223 tigerbeta:~ tigerbeta1$
If the process still won't quite, enter
kill -9 PID (the
-9 signal means "non-ignorable" or "I really mean it!"):
tigerbeta:~ tigerbeta1$ kill -9 223 tigerbeta:~ tigerbeta1$
A common problem many users experience at one time or another is a file stuck in the trash that can't be deleted. An easy fix is to use the Terminal:
tigerbeta:~ tigerbeta1$ sudo chflags nouchg ~/.Trash/* Password: tigerbeta:~ tigerbeta1$
nouchg option to the
chflag command ("change file flags") unlocks the file. The
nouchg keyword is actually the word
no in front of the
uchg option ("set the user immutable flag") so
no turns the option off. You should now be able to delete everything in the Trash normally.
There are many things the Finder is good at and certainly for many users, a trip to the Terminal is a scary prospect. But I hope this article introduces some useful (and fun!) things the Terminal can do for you that give you some courage to try out more advanced commands.
Mary Norbury-Glaser is the IT director at a University of Colorado affiliate center. She has over 15 years of experience in cross-platform systems administration in the education sector. She loves fast cars and geocaching.
Return to MacDevCenter.com.
Copyright © 2009 O'Reilly Media, Inc.