macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

An Introduction to Tiger Terminal
Pages: 1, 2, 3

Sudo

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 sudo with caution. You can seriously damage your system by running commands with root privileges!



Here's that cp command again with sudo's help:

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 sudo the 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: vi/vim, Emacs and pico. 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 pico or nano and you'll get nano.

Since we're dealing with a root-owned file, we'll use sudo again:

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:

Flags and Aliases and Man Pages, oh my…

So How Do You Use nano?

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 (^V). To get more information on all of the commands, press the Control key and the G key (^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 Command-N) to see your new terminal greeting:

Flags and Aliases and Man Pages, oh my…

Flags and Aliases and Man Pages, Oh My

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 cp, rm (remove, which is the same as "delete and empty the Trash"), and mv (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 Enter:

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 ^X. 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 rm command:

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 rm.

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:

-a: Typing 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.

Pages: 1, 2, 3

Next Pagearrow