Mac OS X Command Line 101
by Richard Burton
Learning How To Move Around
Part II of this series...
May 3rd, 2002
"I know God is an Irishman, because His book begins at the beginning."
- Dave Allen
We'll start with some basic commands in our first column. The three most important things you can know about anything are: where things are, where you are, and where you can find more information. That's as good a place to start as any.
>From the desktop, go into Applications -> Utilities, and open Terminal.app. This should give you a command line prompt:
Welcome to Darwin!
[localhost:~] dr_unix%
This provides you with some handy information. "localhost" is the computer name. ("localhost" means the computer in front of you.) After the colon is where you are, known as your current working directory. (The "~" is a shortcut that means "your home directory".) And the rest of the prompt before the "%" is your username.
So we know where we are, our home directory, but where, exactly, is that? Unix provides a simple way to do this, pwd. pwd will print the current working directory (i.e., where you are) to the command line. (Mnemonic: 'pwd' is an acronym for 'print working directory'.)
[localhost:~] dr_unix% pwd
/Users/dr_unix
[localhost:~] dr_unix%
When you first fire up Terminal.app, it will place you in what is called your home directory. This is your private area; it is yours to do with what you want (within reason, of course).
Now that you know where you are, you'll want to see what is around the place. The command ls will show you a list of all the things in your present working directory. (Mnemonic: 'ls' is short for 'list'.)
[localhost:~] dr_unix% ls
Desktop Documents Library Movies Music Pictures Public Sites
[localhost:~] dr_unix%
As a way to compare, go to Aqua and open up your home folder. There will be a one-to-one correspondence between the files and folders that Aqua shows, and the files listed by ls. (At least there darn well should be.)
Now that we've taken a quick look at where we are, we'd like to be able to move around a bit. To change directories, use the command cd. (Mnemonic: 'cd' is an acronym for 'change directory'.) The command takes the form cd name_of_directory. In this case, we are going to cd to the Library directory, which you'll note above is in the directory where we are.
[localhost:~] dr_unix% cd Library
[localhost:~/Library] dr_unix% ls
Application Support Documentation Keyboards Screen Savers
Assistants Favorites Keychains Sounds
Audio FontCollections Logs Voices
Caches Fonts Preferences iTunes
ColorPickers Internet Plug-Ins Printers
[localhost:~/Library] dr_unix%
The cd command has a couple nice tricks. One is that, if you make a mistake, you can type cd - to go back to the previous directory.
[localhost:~] dr_unix% pwd
/Users/dr_unix
[localhost:~] dr_unix% cd Library
[localhost:~/Library] dr_unix% pwd
/Users/dr_unix/Library
[localhost:~/Library] dr_unix% cd -
[localhost:~] dr_unix% pwd
/Users/dr_unix
[localhost:~] dr_unix%
Also, if you do not provide cd with a directory, it will assume you want to go to your home directory.
[localhost:~/Library] dr_unix% pwd
/Users/dr_unix/Library
[localhost:~/Library] dr_unix% cd
[localhost:~] dr_unix% pwd
/Users/dr_unix
[localhost:~] dr_unix%
Thus, it is always easy to find your way home.
Also, notice that Unix is clever with directories. If you are going to a directory under your present working directory, you don't need to type the entire directory. If you are in your home directory, cd Library and cd /Users/dr_unix/Library will do the same thing (assuming your username is "dr_unix", of course).
Now that you can move around without losing your way home, let's explore a little. Many, though not all, of Unix' commands reside in a special area called "/usr/bin". Let's go there now and take a look around.
[localhost:~] dr_unix% cd /usr/bin
[localhost:/usr/bin] ls
[MANY MANY FILES LISTED]
[localhost:/usr/bin]
Now that's a lot of stuff; this is where one of those daunts might appear. However, you know that each one of those things does something useful, otherwise it wouldn't be there. (Remember, Unix was created by computer geeks, and good computer geeks hate creating more work for themselves.) Also, you know that each of those commands does one thing, because this is Unix. So how do you find out what does what? You could pester your local Unix geek, but we aren't always the most approachable people. We don't like being bothered; that's why Unix has good online documentation. It's accessed using the man command. (Mnemonic: 'man' is short for 'manual'.) To find out about a command, just type: man command_name and you will get far more information than you ever wanted. (On a side note, man uses a command called more to display only one page at a time. Hit the space bar to advance to the next page, 'b' to go back a page, and 'q' to quit.)
[localhost:/usr/bin] dr_unix% man ls
man: Formatting manual page...
LS(1) System Reference Manual LS(1)
NAME
ls - list directory contents<
SYNOPSIS
ls [-ACFLRSTWacdfgiklnoqrstux1] [file ...]
DESCRIPTION
For each operand that names a file of a type other than directory, ls
displays its name as well as any requested, associated information. For
each operand that names a file of type directory, ls displays the names
of files contained within that directory, as well as any requested, asso-
ciated information.
If no operands are given, the contents of the current directory are dis-
played. If more than one operand is given, non-directory operands are
displayed first; directory and non-directory operands are sorted sepa-
rately and in lexicographical order.
The following options are available:
[localhost:/usr/bin] dr_unix%
(I hit 'q' here because there were nine or ten more screens of information. I told you it was probably more than you wanted to know.)
man is good for finding out what a specific command does, but what if you want to do something but don't know what the command is? Yes, Unix gives you that, too, with apropos. Did you notice that the first line of information we got from man ls was a brief synopsis?
ls - list directory contents
apropos will look through these synopses in the man pages and provide you a list of commands (and their synopses) so you can look for the one that best suits your needs.
[localhost:/usr/bin] dr_unix% apropos list
ciphers(1) - SSL cipher display and cipher list tool
column(1) - columnate lists
ls(1) - list directory contents
lsof(8) - list open files
perltodo(1) - Perl s-1TO-DOs0 List
sudoers(5) - list of which users may execute what
users(1) - list current users
xargs(1) - construct argument list(s) and execute utility
[localhost:/usr/bin] dr_unix%
And if that still isn't what you want, you can pester your Unix geek.
You are encouraged to send Richard your comments, or to post them below.
Most Recent Mac OS X Command Line 101 Columns
Command Line History & Editing Your Commands
November 22nd
Pico: An Easy To Use Command Line Editor
November 1st
Understanding The "grep" Command In Mac OS X
October 4th
Command Line History & Editing Your Commands
September 6th
Mac OS X Command Line 101 Archives
Back to The Mac Observer For More Mac News!
Richard Burton is a longtime Unix programmer and a handsome brute. He spends his spare time yelling at the television during Colts and Pacers games, writing politically incorrect short stories, and trying to shoot the neighbor's cat (not really) nesting in his garage. He can be seen running roughshod over the TMO forums under the alias tbone1.
|