Web Mail | LearnJCU | Contacts | Bulletins | Campus Maps
   Information For > Prospective Students | International Students | Current Students | Visitors | Staff | Jobs at JCU
Information About > The University | Research | Teaching | Courses & Degrees | Faculties & Divisions | Library & Computing

UNIX Guide for Beginners

JCU HPC

This document is designed to give the UNIX beginner a sense of the operating system. Please contact HPC Support if you require additions and or clarifications.

The discussion is broken into the following areas (click to jump to any heading):

General UNIX

UNIX is a line-oriented system similar to MS-DOS. Each command to the operating system is terminated by the "return" or "enter" key.

UNIX uses a text interface and supports upper and lower case. The disadvantage of using both upper and lower case is that commands and file names must be typed in the correct case. Most of the UNIX commands and file names are in lower case.

Special functions are generally invoked using the control (Ctrl) key. For example a running command can usually be aborted by holding down the "Ctrl" key while depressing the letter C. The short form for this is generally CTRL-C or ^C.

Using control keys instead of special function keys is sometimes difficult for users to remember. The advantage of using control keys for special commands is that nearly every terminal and terminal program supports the control keys allowing UNIX to be used from a wide variety of different terminals.

There are now graphical interfaces to UNIX based on the X-windows system. To use a X-interface you need an X-terminal on your PC or MAC. You can also use the free Linux operating system on PC's and MAC's, that is a UNIX-like system that has X-windows build right into it. This is the best and cheapest way to use a UNIX machine from a remote terminal.

Using files with UNIX

While the UNIX user interface can be less friendly than other O/S's, UNIX is provides one of the simplest and easiest to use file systems.

UNIX is the precursor to the tree structured file systems of MS-DOS and Macintosh. These file systems all consist of a tree of directories and sub-directories. There are commands (or mouse clicks) which allow you to move about the directory structure. These commands are generally quite similar between systems. In UNIX these commands are as follows:

cd or chdir - change directory
pwd - print working directory
mkdir - make a new directory
ls - list the contents of the directory (like dir in MS-DOS)

In any tree structured file system the concept of where you are at in the tree is very important. There are two types of file references which can be done. You can refer to a file relative to the current directory or refer to a file by its complete name. When the complete name of a file is given the current position in the directory tree is ignored.

When a user logs in to the system, they are placed into their "home directory". When an account is created, the home directory is created and associated with the account. To find the name of the home directory simply type 'pwd' after you log in:

IRIX (hydra.jcu.edu.au)
login: crozier
Password:
IRIX Release 6.5 IP35 hydra
Copyright 1987-2001 Silicon Graphics, Inc. All Rights Reserved.
Last login: Tue Mar 20 09:04:36 AEST 2001 by UNKNOWN@137.219.17.145
*******************************************************************************
* Service:             Hydra - James Cook University                          *
*              ***** This service is for authorised users only *****          *
*******************************************************************************
* WARNING:   It is a criminal offence to:                                     *
*            i.  Obtain access to data without authorisation                  *
*                   (Penalty 2 years imprisonment)                            *
*            ii. Damage, delete, alter or insert data without authorisation   *
*                   (Penalty 10 years imprisonment)                           *
*                                                                             *
*******************************************************************************

$ pwd
/homes/36/jc122736
$

To list the files in your home directory, you use the 'ls' command. There are many options to the ls command which you can look up by typing 'man ls'. The most useful options are the '-l' option and the '-a' options. The command 'ls -l' will list the files and directories in your current directory with extended information. A UNIX convention is that files with a . as the first character are not listed by the ls command unless the '-a' option is given. This convention has lead to a number of special configuration files having periods as the first character of the name. You may also create files beginning with a period if you do not want them to show up in a normal ls command. The following are several examples of the ls command:

   $ ls
   fileone     two      proj1
   $ ls -l
   -rw-------  1 example     10028 Sep 26 11:00 fileone
   -rw-rw----  1 example       281 Oct  5 14:21 two
   drwx------  2 example       638 Aug 21 17:27 proj1
   $ ls -a
   .login      .cshrc    fileone     two      proj1
   $

In the output from the 'ls -l' the additional information regarding the file permissions, owner of the file, size, date of last modification, and file name.

In the output from 'ls -a' the additional files '.login' and '.cshrc' are shown. Both of these files are executed when the user is logged in, much like AUTOEXEC.BAT is executed when a MS-DOS personal computer is booted. The user can place commands in these files which can be used to customize each user's individual environment.

Sub-directories are generally used to group files associated with one particular project or files of a particular type. For example, a person might store all of their memorandums in a directory called 'memo'.

The mkdir command is used to create directories and the chdir (or cd) command is used to move into directories. The pwd command can be used to keep track of the current sub-directory.

   $ ls
   fileone     two      proj1
   $ mkdir memo
   $ ls
   fileone     two      proj1    memo
   $ ls -l
   -rw-------  1 example     10028 Sep 26 11:00 fileone
   -rw-rw----  1 example       281 Oct  5 14:21 two
   drwx------  2 example       638 Aug 21 17:27 proj1
   drwx------  2 example       638 Aug 22 14:47 memo
   $ pwd
   /usr/example
   $ cd proj1
   $ pwd
   /usr/example/proj1
   $ ls
   main.c     sub.c      a.out
   $ cat main.c
	  - contents of main.c -
   $ cd ..
   $ pwd
   /usr/example
   $ cat /usr/example/proj1/main.c
	  - contents of main.c -
   $ cat proj1/main.c
	  - contents of main.c -
   $ cat main.c
   main.c not found.
   $

At the end of this example, the 'cat' command is used to show the use of relative and absolute file names. The first cat command shows the contents of main.c in the sub-directory proj1 of user example's home directory. This reference is a relative reference because it does not start with the slash (/) character. The command cd can be given the parameter of two dots to indicate to go back up to the parent of the current directory. After the 'cd ..' command the pwd shows that we are "back" in the home directory.

The next cat command shows the use of an absolute file name which starts with a slash. This cat succeeds because the file name is completely specified as the complete path name and the file name. The following command 'cat proj1/main.c' also succeeds because relative to the home directory, the sub-directory and file name are correctly specified.

The command 'cat main.c' in the home directory does not find the file stored in the sub-directory.

The UNIX file system is one of the simplest tree structured file systems to use. There are no syntax extensions for accessing system files, files belonging to other users, or files on network mounted file systems. The file system is one place where UNIX has a more friendly user interface than most systems.

Other file system commands:

   rm - remove a file
   mv - rename a file or group of files
   cp - copy a file to another file or a set of files into a directory

Your Login Environment

Nearly all operating systems have some way to customize your login environment by editor configuration files. On UNIX there is a file which is executed for every login called '.login'

An example .login follows:

   $ cd
   $ pwd
   /usr/example
   $ cat .login
   set path=(/usr/local/bin /usr/ucb /bin /usr/bin ./)
   $

The cd command without any parameters generally moves you to your home directory. This example .login file contains commands to set "environment variables". The variable in this case is 'path'. This variable specifies where programs are located, and you can add more directories to the path that can be searched to find programs or files.

UNIX Commands

This section will list a number of UNIX commands and key strokes in the following categories:
  • Important key strokes
  • Commands to get help and information about the system
  • Commands to find out information about the system and other users
  • File system commands
  • Networking commands

General Syntax for UNIX

The general command syntax is a command followed by some options, and then some parameters. If a command reads input, the default input for the command will generally come from the interactive terminal. The output from a command (if any) will generally be printed out on the users terminal.

The command syntax allows the input and outputs for a program to be redirected into a file or the output of one program can be passed as the input to another program.

General command syntax is as follows:

   cmd
   cmd -options
   cmd -options parameters

To cause the program to read from a file rather than the terminal, the < sign is used on the command line:

   cmd -options parameters < input 
   cmd -options parameters > output
   cmd -options parameters < input > output

To cause the output from one program to be passed to another program as input the vertical bar (|) is used.

   cmd -options parameters | cmd2

This feature is called "piping" the output of one program into the input of another. Several useful examples of this are shown in the example commands.

The following are some examples of the ls command showing the different command formats:

   ls
   ls -la
   ls -la mydat
   ls -la | more
   ls -la /etc > tmpout
   more < tmpout

Wildcards

Generally when a UNIX command expects a file name such as the more command:
   more filename

it is possible to specify a group of files using one or more wild card expressions.

Unlike other operating systems UNIX allows wild cards on any command. The reason for this is that wild cards are processed by the operating system and not the individual program.

The special characters which cause wild card searching to be performed are as follows:

   * Matches any string of characters zero or longer
   ? Matches any single character

The following are some examples of commands using wild card characters:

   more dat*
   more d?t
   more ???crs
   more */*.c
   more */raisememo

The second to last example will search all sub-directories one level below the current directory for files ending in '.c' and show you those files. The last example will search all sub-directories one level below the current directory for files named raisememo and show you the files, pausing between each screen of filenames.

A very useful command which allows searching through many files for a pattern is the grep command. The first parameter to grep is a search pattern. If no files are specified, grep will scan its 'standard input' for lines containing the pattern and write them to output. You may also specify a list of file names on the grep command. For example if you had a bunch of different C programs and wanted to find the one which called the fopen subroutine, you could use the following command:

   grep fopen *.c

Wild cards are more flexible in UNIX than any other operating system. This flexibility makes them more powerful, but you must also be careful not to when using them in potentially destructive commands such as rm (remove file).

UNIX Commands

The following table contains a list of the important key strokes used in UNIX.
Important key strokes
Ctrl-DLog off the system
Ctrl-CAbort a running process
Ctrl-ZStop a running process

The following command examples allow you to access on-line documentation and tutorials.

Commands to get information about the system
man lsGets you a manual page on the ls command
man -k batchGets you the title lines for every command with "batch" in the title

The following table allows you to look at what is happening on the system.

Looking at the system and other users
uptime Shows the time since the system was last rebooted.
Also shows the "load average". Load average indicates
the number of jobs in the system ready to run. The higher
the load average, the slower a system will run.
who Shows who is logged on to the system.
w Shows who is logged on to the system and what they are doing.
ps Shows your processes.
ps -aef Shows all of the processes on the system in a long format,
with the user name associated with the process, sorted from
the heaviest user to the lightest user.
finger jc183764 Gets information about the user with the login ID "jc183764".
top Displays information about all processes on the system.
Hit the "q" key to exit the "top" program.

The following table is a summary of the commands which affect the file system and access files.

File System Commands
ls Shows your files.
ls -l Shows your file in an extended format including
file size, ownership, and permissions.
ls -la Shows all files, including the system files in your account.
du Shows the disk usage for the current directory.
cat filename Shows the contents of a file at your terminal.
more filename Shows the contents of a file at your terminal, pausing to allow
you to spress the space bar to continue. Good for large files.
tail filename Shows the last few lines of a file.
grep pattern filename Shows the lines in a file which contain the specified pattern.
wc filename Counts the number of lines, words, and characters in a file.
cp file1 file2 Copies file1 to file2. Any previous contents of file2 are lost.
mv file1 file2 Renames file1 to file2.
mv file directory Moves the specified file into the specified directory,
keeping the original file name.
rm filename Deletes a file.
chmod perm file Changes the permissions of a file. See man chmod for details.
pwd Print Working Directory. Shows you where your are at in
the file system. Very useful when you get confused.
mkdir directory Makes a new directory in the current directory.
rmdir directory Removes a sub-directory from the current directory.
The directory must be empty before it can be removed.
rm -r directory Removes all files and sub-directories from a directory, then
removes the directory. Very convenient, useful, and dangerous.
cd Move back into your home directory.
cd directory Move into a directory from you current directory.
cd /path Move into a directory regardless of your current directory.

The following commands provide simple access to networking capabilities in UNIX.

Networking commands
ftp host File Transfer Protocol. Allows a limited set of commands.
(dir, cd, put, get) help gives help.
telnet host Connects to another ethernet connected host.
talk user Allow you to have an on-line, full screen dialog with another
user on the system. Talk may also be able to talk between
systems. The form talk user@host is used in these cases.
rlogin host Connects to the specified host and logs you on. Similar to telnet
but used where a group of systems are being treated as a single
distributed system.
rcp hosta:filea hostb:fileb Copies the specified filea from hosta to fileb on hostb.
Requires special security.
rsh host command Executes the specified command on the remote host, sending the
command input from the current host. The output is sent back to
the current host as standard output of the rsh command.
rexec host command Similar to rsh.

All of these commands have on-line documentation available through the man pages.

Using the UNIX Editor's

There are two commonly-used text based UNIX editors that you can use - 'Pico' and 'Vi'. There are alot more editor options if you have an X-windows terminal - 'nedit' is my favorite. Pico is simple to use, but can have problems if you have long lines (pico 'hard wraps' lines at 80 characters). Vi is very very powerful, but more difficult to use. Here are some instructions to both editors:

Pico

To run pico just type pico. There is on-screen help available. Pico is the same editor that is used in the pine Email client. I do not recommend using pico with PAUP as it inserts formatting characters that can upset PAUP's command files.

Vi

The UNIX editor is called 'vi' which stands for visual editing. Most people find vi a very difficult editor to learn compared to many of the microcomputer based editors today.

If you end up frustrated using vi, do not assume that it is because your lack of skill. You will eventually figure it out and become somewhat proficient at using it. There is no on-screen help or nice keyboard templates you just have to remember the control sequences for the various commands.

This is not a complete training on the use of the editor just a survival guide. The most important thing to remember about vi is that it has three "modes" a "move around mode", an "edit mode", and a "command mode".

vi Survival Guide
vi filename Start up vi.
ESC ESC CTRL-L Panic! Usually fixes things up.
ESC Gets you out of insert mode. Does nothing in escape
(move around) mode. Also gets you out of command mode.
CLRL-L Redisplays the screen.
Arrow keys Moves you around if your terminal is correctly setup.
k Move up one line (escape mode).
j Move down one line (escape mode).
ENTER Move down one line to first non-white space (escape mode).
CTRL-H or h Move left one space (escape mode).
space or l Move right one space (escape mode).
$ Move to end of current line (escape mode).
0 Move to start of current line (escape mode).
10G Goto line 10 (escape mode).
G Goto the last line in the file (escape mode).
i Begin inserting just before the character the
cursor is currently on (escape mode).
a Begin inserting just after the character the
cursor is currently on (escape mode).
dd Deletes the line your cursor is on (escape mode).
d SPACE or x Deletes the character your cursor is on (escape mode).
dw Deletes the word your curson is on (escape mode).
d10d Deletes 10 lines starting from the line your cursor is on (escape mode).
u Undoes your last change (escape mode).
cw Changes the current word you are on (escape mode). Terminate with ESC.
r Replaces the character directly under the cursor (escape mode).
R Goes into replace (overwrite) mode. Allows you to type over characters.
Terminate with ESC. Cursor movements are not allowed in this mode.
:wq ENTER or ZZ Enter command mode. Write the file and quit.
:w ENTER Enter command mode. Write the file and stay.
:q ENTER Enter command mode. Quit the file if no changes.
:q! ENTER Enter command mode. Quit the file and abandon changes.

Conclusion

This document is intended to give you some perspective on the UNIX system and guide you toward learning more about the UNIX system.

You must understand that UNIX is not the easiest computer system to learn. Have patience, ask questions, and don't get down on yourself just because it doesn't seem as easy as other computer operating systems. The power and flexibility of UNIX is worth the extra effort.

Have fun!