Navigating the command line

Back in October we started down the road of learning Unix. You may also remember that I said that with Apple’s new operating system MacOS X, we weren’t just given a new look . In fact MacOS X is built on top of a Unix kernel, which makes for a stable, relatively crash-free experience. It benefits from multi-threading and multi-tasking. The most important part of using a Unix based operating system is the great assortment of Unix applications that come installed.

Unix is a collection of small but efficient applications. Each one performs a basic task. In the October article, I introduced a simple one, whoami, which returns a basic answer. The application, who, “prints” the result, or “returns” (geek-speak) the answer on the Terminal screen. (Applications>>Utilities>>Terminal.)
If you type it formally;
who am i
The result printed back is;
tim ttyp1 Nov18 22:47
This is what is returned; “tim” is the name I logged in as, “ttyp1” is the name of the terminal, and the date.

I should also point out that who and whoami are really different applications. If you check their manuals you’ll see that they both have different options. Manual? “What manual.” you say? Another cool thing about most of these little utilities is that they come with manuals installed. These manuals are referred to as “man pages”.
Type this on the command line;
man whoami
Your Mac will go off and find the man page for whoami and print in on the Terminal. MacOSX opens the man pages one screen at a time, for easy reading. Each screen full will end with a “:” and a prompt. Hit the “Space Bar” and the page will scroll down, one page at a time (“B” will scroll upwards.) until you see “(END)”. Hit the Space Bar once more, the man program will exit and you’ll be back at the prompt.
Hint: It is helpful to open a second terminal window, File>>New Shell (Cmd-N). Now you can read the manual while running the application in the first window.
If you look at the man page for who you’ll see a list of available options. I alluded to options in October. Options are added to the command line to extend an application. They are usually separated by a space and begin with a hyphen.
As an example;
who m
tim ttyp1 Nov 18 22:45
This returns the user information about the current terminal, just like whoami does.
who u
tim console Nov 18 22:45 00:36
This returns the user information and the idle time. You may remember that Unix is designed to be a multi-user environment, which is why it can be important to know who is logged in to the system.

You may not realize it if you’re on a single user machine, but you have in fact logged in. When you installed MacOSX the Setup Assistant created a username and password for you. It also assigns you a “home” folder. All users on a Unix system have a home folder and on MacOSX it’s created in /Users directory.

This brings up another Unix mystery “What are all these other folders for and how do we navigate them?” Remember the pwd application tells you where you are. When you start a new terminal window (or shell) you are in your home folder. If I enter pwd at the prompt and hit Return (or Enter.)
[Tims-G3:~] tim% pwd
/Users/tim
It returns “/Users/tim”, which translates the actual (or absolute) path to the folder. What I have been loosely referring to as the prompt on Mac is:
“[Tims-G3:~] tim%”, which gives me the Rendezvous name of my Mac ( a Jaguar technology,) a colon “:”, a tilde “~” (short for home) and my username and a percent sign (to indicate that I’m a regular user.) The pwd app returns the “present working directory” on the next line.

The metaphor that is used in Unix, similar to Mac or Windows, is of a directory tree. On your Mac it may start at “Macintosh HD. On Windows it starts at “C:” In the Unix shell the tree starts with a “forward slash – /” which we call the “root” directory. We then navigate down (I prefer to say “down”, to say that things are under the root directory) with a utility, “cd”, to call or change directories.
To get to root ry this:
cd /
ls
and you will list (ls) the contents of the root directory. You will see items such as Applications, Library, System, Users, but you will also see a number of Unix directories that are made invisible to the Finder. The directories; bin, dev, etc, sbin, tmp, usr, var are present on most Unix systems.

System wide applications are stored in “bin” (binaries), “dev” contains information about devices, “usr” stores user scripts, “var” contains preferences or variable items, and “etc” contains systems settings and apps. They are the Unix equivalents of Applications, System, Utilities and Work In Progress.

To get back home you can enter;
cd /Users/tim (enter your username instead of mine.)
or
cd /~
as the tilde is the short cut for Home.

Starting at root again (cd /) you can also get to Home by calling the directories relatively, by first entering cd Users and then cd tim. Starting at the root directory “Users” is a subdirectory (or relative to root) but “tim” is not. From root cd tim will result in a “no such file or directory” error.

If there were another user on the system you can navigate to her directory by entering the path relatively or absolutely. cd ../ will navigate up a directory, so from your home
cd ../carol will go up to the Users directory and down into the carol directory. Of course you can also get there by entering cd /Users/carol .

Now your homework is to get into the shell and navigate around. Use ls and ls la and try to cat and more some files Not sure what I’m talking about? Check the man pages See you next month.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.