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

Unix

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 5 - Working With Unix (page 1 of 2)

The Story So Far

The previous four parts covered Unix basics:

  • navigating and viewing the directory structure and the files within it
  • creating and removing directories
  • creating and removing files
  • understanding and changing file permissions.

We are now ready to start actually interacting with our files - viewing them, searching for them, searching within them, and changing them. In the next three tutorials I will introduce a set of core commands in some detail and describe how these can be combined to perform more complex tasks.

In this part I will lay the foundations by describing the shell and what it can do for you, and detailing some useful commands:

  • head, tail
  • locate, find.

In Part 6 I will introduce regular expressions and two commands that make use of them:

  • 'grep' for searching within files
  • 'sed' for changing files.

Part 7 will cover the concepts of re-direction and pipes, and how one uses pipes to combine simple commands to perform more complex tasks.

Parts 8 and 9 will cover shell scripting.

It's Magic

First off, the command left dangling at the end of part four - 'file'. 'file' examines any file and (attempts to) analyse it and report on what kind of file it is.

For example:

% file ~
/Users/saruman: directory

% file /bin/ls /bin/ls: Mach-O executable ppc
% file Documents/My\ Setup/change.log Documents/My Setup/change.log: ASCII text
% file bin/osxfaq-css bin/osxfaq-css: Bourne shell script text

Try it on any file; you will usually get an intelligent response. How does 'file' do this? Well, with a little inside information and a little magic - /etc/magic to be precise.


Before we begin, a note for those who have upgraded to Jaguar (Mac OS X 10.2).

Up until Mac OS X 10.2 a considerable amount of shell initialization was included by default. For whatever reason, the set up in 10.2 does not include it. Consequently you may notice that the shell behaves differently. If you wish to restore the shell initialization of pre 10.2 see the README file in /usr/share/tcsh/examples.


Greater Than the Sum of Its Parts

The Unix environment provides a huge number of command line tools - small programs that do a specific job. One of the most powerful aspects of the command line is the ability to combine these tools so that they work together to perform a more complex task - a task that is very specific to your particular requirements at that time.

Commands can be combined in two ways. Directing the output of one command to form the input of the next, thus chaining a small number of commands together. This is called piping.

More complex combinations of commands are written to a file and form a script, usually using the control framework afforded by the shell. This is called a shell script.

We will consider a useful subset of commands that form a core, and which can:

  • search out files that conform to prescribed criteria (this tutorial)
  • search through these files to find prescribed patterns (next tutorial)
  • change files in prescribed ways (next tutorial).

First we must learn how to use the individual commands effectively. Then we are in a position to combine commands using piping. Finally, by learning the shell scripting language we can combine commands in a programmatic manner to create a bespoke solution.

 
Tell Me More...

In the Bin

Executables are traditionally held in 'bin' directories - that's bin as in binary. The term comes from a programmer's perspective. A program starts life as source code in ASCII text, and is then compiled into machine code to form an executable. Executable code is in binary form - loads of 1's and 0's.


Which Bin?

Recall that the command 'which' will tell you where any given command can be found.

% which cd
/bin/ls

A Word in Your Shell-Like

What exactly are the command line, the Terminal, and the shell?

The shell is simply a program; one whose job it is to listen to what you type at the keyboard, to launch other programs like 'ls', and to display the output from those programs. The shell is your main method of interaction with Unix itself.

A shell will offer features to make your time at the command line more productive, like displaying a prompt, keeping a command line history, and auto completion. Most importantly the shell provides the framework to enable piping, and a scripting language to allow scripting.

There are many different shell programs in use in the Unix world, and each brings its own interactive experience and scripting language. The default shell for Mac OS X is called the t-shell ('tcsh') and it's the one I will assume you are using. Mac OS X provides most of the alternative shells too, and you are free to choose which you use. Most shells will work for this tutorial, but occasionally something may not work, or may behave differently.

You can think of a shell's abilities in terms of (a) its interactive experience, and (b) the scripting language it provides.

A Terminal Situation

In the days before personal computers, the shell's input and output went via a serial line between the computer and a 'dumb terminal' - a big lump of hardware that could not do much more than send characters down the line and display received characters on a screen (or on paper if it was a teletype).

Now we have a graphical interface and use a program that emulates those old lumps of metal. The Mac OS X Terminal is simply a program (an Aqua program) that does just this, and of course you can have as many of them as you like. When you open the Terminal, or a new Terminal window, it automatically starts the t-shell (or your default shell) for you.

The Command Line

The command line is a concept; a way of referring to the line of text you type in order to enter a command.

The Bourne Shell

The Bourne Shell (or 'sh') is the standard Unix shell that all installations must include. 'sh' scripts are therefore portable to any Unix system, and it is essential to lean 'sh' scripting. Many system features such as 'cron' run their scripts using it.

'sh' is very good as a scripting shell, but its interactive experience is lacking.

bash

'bash' is a popular shell with the Linux crowd. It is upward compatible with 'sh' and so can run all 'sh' scripts. A system with bash does not need 'sh', and I think the 'sh' of OS X is actually 'bash'.

Bash greatly improves on the interactive experience of 'sh'.

The name 'bash' comes from Bourne Again SH. Sorry :-)


Where is the Bin?

You will find Unix commands in one of eight places.

/bin contains a minimum set of essential user commands.

/usr/bin contains the rest of the user commands that are delivered with your machine.

/usr/local/bin is for user commands that you (or the system administrator) have added locally.

~/bin is for user commands that you personally have installed and are not intended for other users.

For each of the above there is a corresponding 'sbin' directory for system, as opposed to user, commands. These are commands that control the Unix system and its configuration.

The Right Path

The shell does not implicitly know which directories to search in order to find an executable. It must be told via the 'path' shell variable.

Type:

echo $path

to see which directories are in your search path.

It should include at least the following:

echo $path
/usr/bin  /bin  /usr/sbin  /sbin

and in that order.

Ideally, it should include:

echo $path
 /Users/your-user-name/bin
 /Developer/Tools  /Developer/Applications
 /usr/local/bin  /usr/bin  /bin
 /usr/local/sbin  /usr/sbin  /sbin
 .

You can set up 'path' from a login script in the file '.login' in your home directory. Use an editor to add these lines to the file:

## System-wide tcsh login file
#

# Set paths set path = ( \ ~/bin \ /Developer/Tools /Developer/Applications \ /usr/local/bin /usr/bin /bin \ /usr/local/sbin /usr/sbin /sbin \ . \ )

You only need the line:

/Developer/Tools /Developer/Applications \

if you have installed the developer tools.

The next time you start the Terminal, or create a new Terminal window, the shell running in that window will automatically execute '.login' and your new path should be active.

The environment variable 'PATH' also defines the search path. The shell automatically keeps 'PATH' and 'path' synchronized so you need only maintain one of these. I have not dealt with shell or environment variables before, but I shall do so in Part 8.

Note: If you add the 'set path' command to your login file, ensure that there are no characters (not even a space) after the '\' character at the end of each line.


Next Page

This page has set the scene, describing the environment in which commands are executed, and the potential for combining them.

Page two will consider some commands in more detail - head, tail, locate, and find.


previous

Part 5 - Working With Unix (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.