DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Forums
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OS X | UNIX

back to LC

Mac OS X Unix Tutorial

by Adrian Mayo - Senior Editor for Mac OS X Unix, Janice Mayo - Senior Editor for Mac OS X Unix

Part 1 - Lose the Mouse (page 1 of 2)

The Finder vs. The Terminal

Firstly, let's establish what the Terminal application is, exactly. Consider the Finder, it gives you a graphical view of the folders and files on your Mac's hard disc, a graphical way of manipulating them (new, copy, rename, delete, etc), and a graphical way of opening documents and launching applications. The Terminal is simply a textual equivalent to the Finder, allowing you similarly to view, manipulate, open, and launch.

Both do their job by communicating with the underlying Unix-based operating system that forms Mac OS X. The difference lies in the way you issue commands - graphically vs. textually. Additionally, the Terminal is more direct, more honest, and gives you complete control over your Mac. The Finder simplifies stuff and lies a little to protect you from harsh realities: control is limited to what Apple have made available via Aqua (and to what any 3rd party Aqua-based tools enable).

I will start by drawing parallels between the 'friendly Finder', and the 'threatening Terminal'.

Open a new Finder window and change to Column View. I choose this view because it shows the folder hierarchy clearly. Click on the toolbar Computer then Home icons. You should see the folder hierarchy from the system disc to your home folder, and also the contents of your home folder.

 
Tell Me More...

What is Unix?

Unix controls your Mac. It controls the processor, the memory, the file system, the network, and all your peripherals. And it does so rather well. Unix gives you network and internet connectivity, and powerful file system management. It gives you a host of useful utilities to help you get your work done. It provides an environment in which multiple applications, and multiple users, may co-exist without tripping over each other - something sorely lacking in Mac OS 9.

Now, launch the Terminal application. It is in the usual Applications folder, then within folder Utilities. (Use a new Finder window for this so you don't disturb the window you just set up.)

The first line is the 'Message of the Day', which is the same every day unless you change it yourself! Next is the prompt - you are being prompted for some input. The bit between [ ] is formed from 'name-of-your-machine:working-directory;' followed by your short user name, followed by '%'. Don't worry about the details for now.

Now type:

% pwd

Note: do not type the '%' character. Throughout this column I will use it to represent the prompt, and you should type just what follows it, then press 'return'.

Note 2: Sometimes you might type a command and Unix simply responds with a new prompt. This is good (unless you were specifically expecting output): it means the command executed without error. Unix works on the principle that "No News is Good News".

Next type:

% ls

(That's letter ell.) You should now see (something like):

So What Have I Done?

By typing ls at the prompt you have launched a Unix application called, well, 'ls'. In Unix speak, you have issued a command, and you did it on the command line. The lines that follow represent the response from 'ls' - its output. This is the contents of your home directory, and should match what you can see in the Finder window. 'ls' means 'list the contents of (i.e. the files and directories within) this directory'.

The first command, pwd, tells you what this directory actually is. In Unix speak it is termed your current working directory. When you open a new Terminal window your current working directory is set to be your home directory. Your home directory is the same as your home folder in the Finder. You are conceptually 'sitting' in this directory, and all commands you issue will assume you are referring to this directory (just as 'ls' did) unless you explicitly give a different one.

The output from 'pwd' will show the directory hierarchy from the top (the system disc) to your current working directory - '/Users/melkor' - similar to what you can see graphically in the Finder window. The first '/' represents the system disc, it is not named like it is in the Finder. Then follows directory 'Users', then another '/' character which is used to separate the names of directories, then the name of your home directory.

The 'ls' command assumed that you meant to list the current working directory.

Now try:

% ls /Users/replace-with-your-user-name

to list your home directory, as before. No matter what your current working directory, this command will list your home directory.

Note that Unix is case sensitive so you must type 'Users' not 'users'. But having said that, the HFS+ file system usually used in Mac OS X is not, so you can get away with typing the wrong case. Sometimes it does matter, such as auto-completion (see later).

Now try:

% ls /Users

to list the contents of the '/Users' directory - the one just above your home directory. Again, compare with what you see in your Finder window.

The command:

% ls /

lists the contents of your system disc. You will notice there are more files and directories at '/' than the Finder shows - it is hiding stuff from you!

And finally:

% ls Documents

lists the contents of the Documents folder within your home directory. This example differs from those previous in that 'ls' starts from your current working directory to find 'Documents', rather than from the system disc. This is explained in the next section.

Directories and Folders and Paths

We call folders directories in Unix. 'Users' is a directory.

/Users/you/Documents

is termed a path. It tells you how to get to a particular file from the root directory, and is therefore unambiguous.

Documents/a-file-name

is also a path, but because it does not start from the root directory (it does not begin with a '/'), it is relative to your current working directory.

Absolute and Relative Paths

The concept of absolute and relative paths is important to understand. If a path begins with '/' then it is absolute. Unix will start from the root of the file system (your system disc) and follow the path you have given to find the file or directory at the end of the path.

If the path does not start with a '/' then it is relative and Unix will start its search from your current working directory.

% ls /Users/melkor/Documents/iTunes

is absolute.

% ls melkor/Documents/iTunes

is relative, and is equivalent to the above if your current working directory is '/Users'.

% ls /melkor/Documents/iTunes

will not work.

 
Tell Me More...

Why Unix?

When Apple chose Unix as the basis for Mac OS X, they did so because it is tried and tested, stable, and supports pre-emptive multi-tasking and multiple users robustly. It was easier than starting from scratch, or migrating Mac OS 9 towards what users demanded. With Unix came instant compatibility with the rest of the world, and some of the most powerful tools like the Apache web server. And it opened the Mac to the world of open source and all the software that it brings.

The Root of it All

The top-most directory is your system disc and is called the root directory. It is represented by '/', and is not named as it is in the Finder. Later we shall see that all other discs and partitions reside within the system disc hierarchy. This is at odds with the way the Finder displays discs and partitions - all at the same level.

Eh? Current Working Directory

Click on the 'Documents' directory in your Finder window. There may be other folders on the machine called 'Documents', but the finder knows which one you mean because you pointed to it. If you were to type:

% ls Documents

at the command line, which particular 'Documents' directory could you mean? You must either give the full, absolute path name, or accept that 'ls' assumes your current working directory. The concept of a current working directory is really to save you from typing the full path name each time - most often you will be working on the files within a particular directory.

Where Does 'ls' Live?

Good question. Type:

% which ls

The output from 'which' is telling you where the command 'ls' is within the file system. It outputs an absolute path name showing the hierarchy of directories containing 'ls'. You can of course list this directory (using 'ls' itself) to see more commands that live in the same directory.

If you have more than one copy of 'ls' on your system, 'which' gives the path name of the particular 'ls' that will be executed. (This is where the name 'which' comes from - otherwise it might have been called 'where'.)

Next Page

Now we know how to list the contents of any directory on your disc. We can discover all the directories and files by listing the contents of the root directory, and then listing the contents of each directory within this, and so on right down to the deepest darkest directory. Next, I will show you how to change your current working directory, and reveal some of the power behind 'ls'.



Part 1 - Lose the Mouse (page 1 of 2)

next

Copyright © 2000-2008 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.