The University of Western Australia
School of Computer Science and Software Engineering
 
 

School of Computer Science and Software Engineering

CITS2200 Data Structures and Algorithms

This tutorial introduces the MacOSX environment, including both the GUI environment and the underlying Unix operating system. Remember, only practice and experience will improve your skills, so don't be afraid to try things!

Getting Started

Start by logging in using your student number and Pheme password. Before doing anything else, you'll want to access the on-line version of this tutorial sheet so that you can follow the dynamic links. On the Dock (the application bar at the bottom of the screen) you'll find the icon for Safari (it looks like a compass) - MacOSX's native web browser. Click on this to start Safari. (You can use FireFox if you prefer.)

Notice that the menu bar at the top of the screen changes to the menus for Safari. This menu bar is context sensitive, and reflects whatever application is currently active. We'll come back to this later.

To find this document, enter the URL:

http://undergraduate.csse.uwa.edu.au/units/CITS2200/Labs/MacOSXIntroduction.html

into your browser and press Return. You should now be viewing this document electronically in your Safari browser.

MacOSX Basics

A good starting point for learning your way around MacOSX is the Mac 101 tutorial. Don't do the whole thing now (you'll be too impatient to get started to read it all), however there are four pages that are worth reading. These are lessons 1, 3, 4, and 5 of the Grand Tour. You can ignore lesson 2, as you shouldn't (ab)use the desktop by filling it up with junk as you do with some windowing environments - there are better ways of quickly accessing everything you need in MacOSX. In particular, the Dock provides a neat way of quickly accessing the most used applications, and the Finder sidebar provides a quick way of accessing parts of the filesystem hierarchy that you use most often.

Note that you may see some differences in the above lessons from what you will see on your computer screen. This is because your setup has been optimised for programming work. We will cover some of the differences below. The other applications mentioned in the lessons can still be accessed via the Applications directory.

The Finder

Probably the most important application, and the one you'll use most often, is the Finder. This can be thought of as the Mac's user friendly interface to the Unix operating system underneath. It allows you to navigate through the filesystem, navigate through the network that the computer is mounted on, open files, and launch applications.

Open the Finder by clicking on the "happy face" icon on the Dock. Change to "column view" by selecting the as Columns option from the View menu. This is the recommended view for browsing as it shows you where you are in the hierarchical filesystem (or "tree"). It is recommended that you stick with this rather than the icon view that you may be used to from other operating systems. Once you are used to it, you will find it more efficient and informative.

You should see that the Finder window is open at your home directory - labelled mac under Places in the sidebar on the left side of the window. If not, navigate to it by clicking on Macintosh HD, selecting home, students.labs, the last digit of your student number, your student number, and finally linux. The correct location should have a little house icon next to it - if it has a red circle with a dash, the folder is locked, and you are looking in the wrong place. If not already present (noting that it was likely pre-configured to be called mac), drag this folder to the sidebar under Places on the left. This creates a symbolic link (or shortcut) to your home directory. Click on the link now. You should see a number of sub-directories listed including Desktop, Documents, and Library, potentially among others.

Your subdirectories Documents and Desktop are automatically backed up to your network drive, so work you save here will be accessible to you anywhere on campus. Use these folders (or subdirectories in them) to save all your work.

Create a new sub-directory in your Documents directory for each unit you are taking in the CSSE school (e.g. CITS2200). Use these directories for your lab work for each unit. You should get into the practice of maintaining a well-structured directory hierarchy - it will help you organise and separate your work into logical divisions.

Now drag these unit directories to the sidebar for quick reference (you can always remove them later by dragging them onto the background).

The Menus

It is worth mentioning a few things about the menus at the top of the screen as MacOSX works differently to other desktop environments. The menu on the top left with an apple icon is a system menu that is available in all applications. Some important things under this menu include:

  1. System Preferences...: used to set general or system preferences. As an example, go to System Preferences... | Mouse. Here you can change what the different buttons on the "mighty mouse" do. Check that the right hand side of the mouse is set to Secondary Button. If not, change it, as "right-clicking" is a useful shortcut for bringing up a context-dependent shortcut menu.

  2. Force Quit...: useful for killing an application that gets stuck or "hung" (as indicated by a spinning beachball that never stops).

  3. Log Out...: this is where you log out when you finish your session.

The remainder of the menu is different depending on what application is active. However, there are some common conventions used by all (good) applications. The first drop down menu will be named after the active application itself. For example, if it's not already active, click on the Safari window you're using to read this page. You will see a menu labeled Safari at the top. This menu is where you will find preferences specific to the application, as well as the Quit Safari menu selection used to quit the application. Note: many applications allow multiple windows, so closing a window by clicking the red button in the top left of the window does not terminate the application, just the window. To terminate the application, you need to use the Quit <application> menu selection in the application's menu. This can be important if you are running out of memory because applications you have used, but not quit, are still resident in memory.

As an example, open Safari's preferences now (select Preferences... from the Safari menu). In the General pane you can see where the home page is set. Change it if you wish. The Save downloaded files to option should be set to your Downloads directory in your home directory - using the desktop is not good practice and should be avoided as many files may be downloaded over time and you don't want to clog up the desktop. Change it if it's currently set incorrectly.

Now go to the Tabs pane. Check that all three boxes are ticked to enable tabbed browsing. You may also wish to go to the RSS pane and change how frequently you check for RSS updates. Close the preferences window, ensure you have the tab bar visible (if not, select View | Show Tab Bar), and check that right-clicking on the Safari tabs bar opens a new tab. Before we leave Safari, go to View | Customize Toolbar... and drag the Home button onto the address bar. This is useful for quickly finding course materials.

While we're on the topic of application preferences, open the Finder preferences (select Preferences... from the Finder menu) and go to the Advanced page, and check that the Show all file extensions is ticked. The hiding of file extensions is another "feature" popularised by another well-known operating system to "protect" the user from useful information. When we come to Unix later, you will see that filename extensions are always shown - they are important.

Returning to our discussion about the top of screen menus, the second menu is usually labelled File and contains options for opening, saving, and printing files. The drop down menu also shows shortcut keys for commonly used options. For example Command-s is a useful one to remember for saving your work (which you should do often!)

The third menu typically contains editing commands. At the right hand end will be the Help menu. Other menus depend on the application, although there will typically be menus for managing windows and views (for example, zooming in and out).

The Dock

The Dock has been mentioned earlier as a quick way of accessing regularly used applications. The Dock is also useful for showing you what applications are currently open (indicated by a small blue dot/light under the icon). If you have a lot of windows open, clicking on an open application in the Dock is useful for bringing its windows to the top. The right hand side of the Dock is where any minimised windows (done by clicking the orange circle) reside.

Developer Applications

The applications used by most people are contained in the Applications directory which, for convenience, appears in the Finder sidebar. Some of the applications we have installed for your lab work also reside here. Have a look in this directory and you will find, for example the Preview application - a tool which allows you to view and print a range of image file types very quickly. Normally when viewing files, you will not need to explicitly open Preview as it will open automatically if you double click on a file type that it handles.

As developers, you will also be interested in some other applications. There is a sub-directory of Applications called Utilities. This directory contains, for example, the Terminal program that we will use shortly (which also appears on the Dock), as well as other useful applications.

As an example, lets say you want to see how hard your computer is working. Open the Activity Monitor. This allows you to view CPU usage, memory usage, network traffic, and other system functions, both overall and by application. If your system is slowing, perhaps due to excessive memory usage, you can look here to see what program is the culprit.

Another directory you may be interested in is Developer/Applications (found by clicking on the Macintosh HD in the Finder). Here you will find Xcode (again, also on the Dock) and, for example, Apple's Performance Tools for optimising code. The Developer directory also contains the developer documentation and examples, although these can usually be accessed directly through an IDE. You may wish to add the Developer directory to your Finder sidebar.

Beyond the GUI: Accessing Unix

Now that we've found our way around the MacOSX GUI, let's have a look at what's going on behind the scenes.

Underlying MacOSX's graphical interface is an open source Unix operating system (based on FreeBSD). Also included is the X11 windowing system used by the Unix community. This provides you with a lot of extra power and access to a huge range of open source projects. In fact, many of the applications shipped in MacOSX use open source projects, or are provided by Apple as open source for use by the rest of the community. Unix is also the platform of choice for some commercial applications.

A good example is the Apache web server included in MacOSX. If you go to the Apache Software Foundation website, you will see that the Apache HTTP server has been the leading web server since 1996. You will also see other projects that you may bump into during the course of your studies or working life. The Ant build tool, for example, is available through Xcode and can be used to automate the compilation process.

There are times when you want more control than the GUI applications provide, or when it is simply faster or more convenient to access the Unix operating system directly. In this section of the tutorial, we will introduce some basic Unix commands to give you a flavour of using Unix directly. For more detail, you can access many Unix tutorials available on the web.

If you have used a Unix variant before, for example the Linux operating system available in the CSSE labs, then you should be able to skip through this section quite quickly. If you haven't, then you will find this introduction useful, but with a steep learning curve. Only practice and experience will improve your skills, so don't be afraid to try things!

Unix Basics

To access the operating system through a command-line interface, click the Terminal program on the dock. This will open up a terminal window containing an operating system shell. It should say something like:

Last login: Mon Feb 28 12:00:01 on ttyp1 csse2100%

The line ending in % is the prompt, which is supplied by the shell. Anything you type into the terminal after the prompt will be interpreted as a Unix command. The prompt indicates that the terminal is expecting you to type in a command, after which it will respond with the output (if any) and then another prompt to signify that it is waiting for the next command.

In the following examples we will assume your prompt will be represented by the text csse2100%, although it is probably different depending on the computer you have logged into. You can tailor the prompt, as many people do, to indicate the current directory and command number being entered. When entering the examples below, don't type the prompt, just what follows it.

Try typing the command date at your prompt and see what happens:

csse2100% date Mon 28 Feb 2011 13:00:00 WST csse2100%

You should have observed that the shell has executed your command by printing the current date on the screen. Immediately following the date, the shell has printed another prompt, indicating it is again ready for user input. This "prompt-command-prompt" style of interface is called a command-line interface to distinguish it from more modern graphical user interfaces (GUIs). In the past, Unix programmers did all their work using just a single command-line and therefore became very adept at using Unix's sometimes less than intuitive abbreviated commands. Current windowing environments allow people to work far more efficiently, offering the ability to open multiple terminal windows at the same time.

Try:

csse2100% whoami 20000001

which will return your user name.

In the following examples, try all the commands yourself as you read through this tutorial sheet.

Filesystem Commands

Just as the Finder is a core component of the GUI environment, many of the commands you use most often in Unix are related to navigating and modifying the filesystem. Try the following:

csse2100% pwd /Users/20000001/

short for "present working directory", which will tell you where you currently are in the filesystem hierarchy or tree. The response returned by the pwd command is called the pathname. Initially, this should be your home directory.

The pathname returned by the pwd command is an absolute pathname - it states the full name of the current working directory starting from the very top of the directory hierarchy. The top of the directory hierarchy is called the root directory (which has an absolute pathname of /). Thus, /home/ (the last / is often omitted, leaving just /home) is the name of a (sub-)directory at the next level down the hierarchy. It in turn contains a sub-directory called student.labs (the full name is therefore /home/student.labs/), which in turn contains another sub-directory called 1 (/home/student.labs/1/), and so on.

The command ls allows you to find out the contents of the current working directory (ls is short for "list"). You should see the same sub-directories you saw in the Finder, including the new sub-directories you created for your CSSE units (and potentially a Downloads directory depending on what you did above):

csse2100% ls CITS2200 Desktop Documents Downloads Library

The command ls -al will show you all the files contained in the current working directory. Files starting with a . are considered hidden and can only be seen with the -a option.

Change into one of your unit sub-directories by using the cd ("change directory"). For example:

csse2100% cd Documents/CITS2200

and check that worked with the pwd command:

csse2100% pwd /Users/20000001/Documents/CITS2200

Now create a new directory inside this directory with the mkdir ("make directory") command for your work for your first laboratory exercise:

csse2100% mkdir Lab01

and check that its appeared with the ls command. Now open the Finder and check that you can see the same directory there too.

The full name of the directory you just created is: /Users/20000001/Documents/CITS2200/Lab01/ even though you created it just using the command mkdir Lab01. The command:

csse2100% mkdir /Users/20000001/Documents/CITS2200/Lab01

would have achieved the same thing. This is because Unix distinguishes between an absolute pathname and the relative pathname of a file or directory. Any pathname that begins with a / describes the full name of the file right from the top of the filesystem (the root). Any other name (those not starting with a /) is treated as a pathname relative to the current working directory.

A nice feature of the shell that saves typing is called filename completion. Rather than typing the full directory name, the Unix shell will complete filenames where it can. Type the following without pressing Return:

csse2100% cd La

then press the Tab key. The shell should complete the name for you. Press the Return key and check you are now in the Lab01 sub-directory.

There are many other shortcut key besides the Tab key that can be used to speed up the way you enter or repeat commands. These depend on your setup. For example, you should be able to use the arrow keys to cycle back (and forward) through your previous commands. Try that now.

To move up the directory hierarchy you can use .. to refer to the parent of the current directory. Try:

csse2100% cd .. csse2100% pwd /Users/20000001/Documents/CITS2200

In addition, the special symbol . refers to the current directory.

Finally, the command cd with no arguments returns you to your home directory:

csse2100% cd csse2100% pwd /Users/20000001/

Running Programs

To run Unix commands you simply type their name at the command prompt. Try:

csse2100% cal February 2011 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

which displays a calendar for the current month.

Most commands can be modified by giving either arguments or options (switches) to the command. You can think of arguments as being input data. For example, as we just saw the cal command on its own gives the calendar for the current month. You can specify exactly which month you want by giving some additional arguments. For example:

csse2100% cal 8 2011

gives the calendar for August 2011. Here the 8 and the 2011 are the arguments to the cal command.

Options are a special kind of argument that specifies a variation in the way that the command operates. For example, the date command has a switch to present the time in Coordinated Universal Time (UTC). That is,

csse2100% date -u

gives the time and date in UTC. The -u is a switch to the date command.

Many Unix commands have a bewildering variety of arguments and switches, which are often subtly (or sometimes blatantly) different between different implementations of Unix. Even experienced Unix users often cannot remember all the options for all the commands that they use. Fortunately there is help available in the form of the on-line manual. The command man can be used on Unix systems to find out the details of the arguments and options for the other commands. For example:

csse2100% man cal CAL(1) BSD General Commands Manual CAL(1) NAME cal, ncal -- displays a calendar and the date of easter SYNOPSIS cal [-jy] [[month] year] cal [-j] -m month [year] ncal [-jJpwy] [-s country_code] [[month] year] ncal [-Jeo] [year] DESCRIPTION The cal utility displays a simple calendar in traditional format and ncal offers an alternative layout, more options and the date of easter. The ...

The manual page (man page) tells you what the command does and explains the various arguments and options. The square brackets [] indicate that the arguments to the cal command are optional (i.e. you can leave them out). Options often (but not always) start with a -. Use the arrows to scroll and press q to quit the man page.

Options can be invoked separately or together. Contrast:

csse2100% cal -j csse2100% cal -y

with:

csse2100% cal -jy

For the two arguments to the cal program (month and year), the nesting of the brackets indicates that if there is one argument then it will be treated as a year and if there are two arguments they will be treated as a month and a year respectively.

The Unix manual pages are not renowned for being well-written. They are usually terse, often to the point of incomprehensibility and it takes quite a bit of practice to be able to read them. Nevertheless, they are your first line of help.

Many programs also have a short help option, such as:

csse2100% man --help

You can also find out where in the filesystem the program you are running is stored with the which command:

csse2100% which cal

Manipulating Files

Using the Safari web-browser, download and save this document (save the file as MacOSXIntroduction.html) to your home directory (ensure you select Page Source in the Format: drop-down menu).

There are several commands that allow you to manipulate files. The simplest thing that you may want to do is to view/look at a file. The command cat (short for "concatenate") allows you to do this. In your terminal window, change to your home directory and have a look at the source file for this tutorial sheet:

csse2100% cat MacOSXIntroduction.html

You will notice that the text flies past too fast for you to read. Some commands that let you look at a portion of a file include:

  • more which shows one "page" of a file at a time.
  • less which is similar to more, but arguably better! (press q to quit less)

Moving files or changing their names can be accomplished by using the commands cp (copy), mv (move), and rm (remove).

For example, the cp command

csse2100% cp MacOSXIntroduction.html temp

makes a copy of the file called MacOSXIntroduction.html and names it temp. Using ls you can see both files.

The mv command

csse2100% mv temp newtemp

actually "moves" the file temp to the new file newtemp (in reality it just renames it, which is far more efficient). The file temp is no longer present in the filesystem.

The rm command is used to delete files that you no longer need. This allows us to save disk space and avoid cluttered directories.

The command:

csse2100% rm newtemp

will remove this file from your directory. You can give rm several file names at once and all the files will be removed. If you want to get rid of a directory, use the command rmdir, but note that the directory must be empty in order for it to be removed.

Editors

Throughout your studies, you will be expected to edit programs and documents you have created (or downloaded) in order to correct errors, add more material, and so on. The Unix operating system provides several text editors to choose from. Which editor you choose is up to you, however, arguably the two most popular choices are the vi and emacs families of editors.

As is the case with most Unix-based editors, both of these editors will require you to learn a number of keyboard-based commands to interact with the editor. Again, mastery takes time and practice, so don't be afraid to try things!

This Page