Using Unix: An Introduction

Platform: Unix
Level of Difficulty: Beginner
<hr>
Rutgers Main | OIT Main | NBCS Main | NBCS Documentation Main


Introduction

A computer's operating system is a "Master Program" that interprets commands people issue and then causes the specified actions to be taken. Unix is a time-sharing operating system (each user gets a portion of the computer's time) whose development began at AT&T Bell Labs in 1969. Many versions of Unix exist but the two main "flavors" of Unix are based on AT&T's System V or on Berkeley's 4.3BSD. This document describes the Berkeley version of Unix since it is more widely used at Rutgers than System V. Portions of this document are based on the Unix 4.2BSD Primer, written by Charles Hedrick for Rutgers University.

All examples used in this document will be from the rci login machines. User input will appear in this typeface.Text that is in italics represents place holders for specifics that you would supply. The notation <CTRL>d means that you should hold down the control key while typing the indicated character (in this case d).

In order to access a Unix computer, you must have a valid account. If you are an employee or a registered student you may create an account for yourself. For instructions in how to do this see the NBCS document Creating Accounts on OIT Systems. Do not give your password to other people; the only one who should have access to an account is the single user who it was assigned to and as such the owner is held responsible for the activity on their account.

Command Format/Execution

A command is an instruction to the operating system to perform a specific task. The Unix Operating System is case specific, which means that file names or commands that are spelled the same but have different capitalization are interpreted as different files/commands. The Unix command format is:

command-name [options] [parameters]

A command-name is usually an abbreviation of the name of the command. For example ls will list a directory's contents, mv will move or rename a file, and cp will copy files.

An option is a single letter or number preceded by a space and a hyphen '-': ls -l. Options may be grouped together with a single hyphen or separated with a space and a hyphen for each, ls -la is the same as ls -l -a (see the command descriptions at the end of this document for a brief explanation of this command). The square brackets are used here (and in Unix manuals) to indicate that the enclosed items are optional and may be left out of the command.

Parameters are usually file names (may include a path, which is explained later) or expressions.

To execute a Unix command simply type in the command-name and all options and parameters required to achieve the effect desired. After a command has been entered, the computer will simple issue another prompt, unless there has been a problem in its execution or the execution of the command requires some interaction.

Command Format/Execution Editing Commands

In the default Rutgers Unix shell (the t shell) with the default editor set to emacs commands may be edited and previous commands retrieved using control characters:
 
<CTRL>a  move the cursor to the beginning of the line 
<CTRL>b  move the cursor to the left 
<CTRL>c  abort the current command 
<CTRL>d  delete the character at the cursor 
<CTRL>e move the cursor to the end of the line 
<CTRL>f  move the cursor to the right 
<CTRL>h delete the character to the left of the cursor 
<CTRL>k  erase from the cursor to the end of the line 
<CTRL>l  clear the screen and display current line 
<CTRL>n  bring up next command line 
<CTRL>p  bring up previous command line 
<CTRL>q  continue displaying to the screen (see <CTRL>s) 
<CTRL>r  redisplay the current line 
<CTRL>s  stop displaying to the screen (see <CTRL>q) 
<CTRL>t  display system status 
<CTRL>u  erase the current line 
<CTRL>w  will erase from the cursor to the beginning of the line 
<CTRL>y  yank back the last item erased by <CTRL>k, <CTRL>u, or <CTRL>w 

Getting Online Help: Man pages

UNlX has a fairly complete set of documentation, called man (manual) pages, that is available online. Every command or program should have a man page. To access man pages, use the man command. For example, to get help on the 'ls' command,
type man ls.

In order to use the man command, there are some things you should know. First, many of the man pages were written by technical people for technical people and assume the person reading them knows more about Unix than a novice will. Second, the program used to display the man pages is called more (see the command descriptions at the end of this document for a brief explanation of this command). Third, for substantial programs, the manual pages do not give every detail you may need. They should tell you how to run the item selected but be short enough to read on your terminal. So man pc will tell you how to run the Pascal compiler and list all of the options, but it will not be a Pascal language reference manual. There are separate manuals for programming languages, the mail system, the document processors, and certain other complex pieces of software.

Generally the man page will refer to this documentation if it exists. Fourth, the man pages are organized into chapters. For example, if you use the command man -k unlink (man -k is described later in this section) to find out what documentation is available on unlink, it will offer man pages from chapters 2 and 3. It is useful to know that chapter 2 is on system calls and chapter 3 is on subroutines. So if you were looking for the Fortran subroutine information, you would look in chapter 3. The topics for each chapter are:

Chapter Topic

  1. Commands
  2. System Calls
  3. Subroutines
  4. Special Files
  5. File formats and conventions
  6. Games
  7. Macro packages and conventions
  8. Items of interest to system managers
To get an overview of what is in a given chapter, you use the command man intro. Some chapters are subsectioned, chapter 3 for example:

3F Fortran library

3M Math library

3N Internet Network library

3S C standard I/O routines

3X Other libraries

The man command is useful when you know what something is called. But if you can't remember the name of a command, the man command can't help because you can't tell it what to look up. The man option -k causes man to list the title lines of the man pages that have the supplied keyword in it. So man -k remove would show all the title lines that contain the word "remove". Generally the title lines contain most of the keywords people would use for looking them up. Using combinations of "man -k" and "man", you should be able to find most of what you want to know about Unix.

Files

A file is an area of storage that is assigned a name by the user. Anything can put into files programs, data, or text. Each file has a name that is chosen by the creator of the file. Names can be from 1 to 255 characters long and can safely include letters, digits, and the characters '.', '-', and '_'. A common naming convention used to organize files by their contents adds a '.' and a short description of the file. The part of the name after the '.' is called the extension. A file named demo.c would be a C program code file and data.txt would be a text file. Executable files often do not have extensions.

Editors

To create or change the contents of a data, text, or program file you use an editor. An editor is a program that reads in a file, lets a user modify it, and then, when instructed to, writes the updated copy of that file. The editors available on the Rutgers Unix systems are full screen editors, which means that they display a screenful of a file and allow you to alter any part that is on the screen. In order to change or add to the file move your cursor to the appropriate location in the text and then use the editor commands to make the changes which will then appear on the screen.

When you are in an editor, you are actually working on a copy of the file, not the permanent file. Changes are made to the copy that the editor is maintaining in memory. When you are finished, instruct the editor to save the current working copy as the new permanent copy (or into a new file altogether). This means that if you exit the editor without saving the changes, the changes will be lost (sometimes you may want this to happen).

Though most users choose Emacs as their editor, there are other editors. One advantage of Emacs is that it is available on most systems (Unix or otherwise) here at Rutgers. Although different machines may have different versions of Emacs, the differences between versions are slight.

The best introduction to Emacs is the emacs tutorial program: teach-emacs. To begin the tutorial, use the command teach-emacs or, from within emacs, type <CTRL>h t.

To edit a file with Emacs, type Emacs filename , whether the file exists or not. If the file does not exist, it will be created the first time you issue a save command.

If the computer does not know what kind of terminal you are using you will get the error message:

emacs: terminal XXX isn't powerful enough to run Emacs

Contact the OIT/NBCS Help Desk at (732) 445-HELP for help in determining your terminal type.

Printing Files

On NBCS Unix computers you use the lpr command to print files toeither your default printer or a printer you specify. To set a default printer place a line like
setenv PRINTER nbhilliop2

into your .login file (using the emacs text editor) and then logout and back in. To see what printers are available type the command printers. Some printers require a specified bin (so you will know where to find your ouput) a bin for the above printer is specified by setenv BIN H07E

With the above two settings lpr filename will send the specified file to the printer nbhilliop2 with a bin specification of H07E. To specify other printers or bins use the -P and -B options to the lpr command, as in lpr -Ptillettp1 -Bt075 filename which will print to the tillettp1 printer and get binned in T075.

Directories and Paths

Files are stored on computer disks that are attached to the computer. A disk can hold thousands of files belonging to many different users. Each user is assigned a portion of a disk for their own use; this space is called a directory. The normal login procedure connects you to your 'home' directory, which is referred to by your user name. When you refer to a filename without any special qualification, you will get a file by that name in the current directory. The ls command lists the files in the current directory and pwd gives you the name of the current directory.

There are many different directories on a disk. A user can have a home directory that contains a directory (known as a subdirectory). For example, John Q. Smith can have a main directory 'jqsmith', and a subdirectory 'work'. The subdirectory 'work' is contained within 'jqsmith'; 'work' could also be referred to as a child directory of 'jqsmith'.

This, in turn, falls within the structure of the entire Unix file system, which can be thought of as an upside down tree (as in a family tree). The top directory is called root, and is noted as "/". Next come other directory names, such as usr, u1, u2, etc. On Rutgers's systems the user directories are subdirectories of u1, u2, or some other u#. As shown below, the directory jqsmith is a subdirectory of u2, which in turn is a subdirectory of the root directory. On larger Rutgers systems, the u# directories are placed under a directory named for the system (eden or rci).
 

/ usr etc u1 u2 
           |
        jqsmith 
           |
          work
To specify the 'full name' of a directory (or file), you must start with the root and then work towards the directory (or file). Each directory is followed by a '/ '. In the illustration above, the full name of a file in the directory 'jqsmith' is: '/u2/jqsmith/filename'. This full pathname contains: root (/), subdirectory (u2), another subdirectory (jqsmith), and then the file (filename).

The directory you are in can be indicated by a '.' (period). The directory immediately above a directory is called its 'parent' (u2 is the parent of jqsmith). The parent of the current directory can be indicated by '..' (two periods). If the current directory is jqsmith, you can refer to a file in the parent directory with ../filename or by /u2/filename. If you are in /u2/jqsmith, a file in the subdirectory work may be referenced with ./work/filename or by /u2/jqsmith/work/filename. The '.' and '..' notations are 'relative pathnames' because the reference is relative to the current directory.

A short cut to denote a user's home directory is to precede the username with a '~' (tilde). The home directory of J. Q. Smith may be referred to as ~jqsmith. This is useful when copying from the current directory to your home directory, as in: cp filename ~/. which will copy the file named 'filename' from the current directory into your home directory ('~/ ' = current user's home directory, '.' = reuse the original filename).

Three of the many directory related commands are ls, pwd, and cd. The command ls lists the contents of a directory. The command pwd will print the working (current ) directory name to the screen. The command cd allows you to change directories.

Programming Languages

There are at least three language compilers (programs that convert program files into executable files) on our Unix systems. To ease the use of the compilers your source code files should be given an extension that matches the language used.
 
Language  Extension Compiler command
.c cc
FORTRAN .f f77
Pascal .p pc
 
Assume, for example, that you have a C program in the file prog.c. Before executing it, you have to compile it using the C compiler. This is done with the command cc prog.c. This will translate the program from source code into a binary executable file called a.out (this is the default name; you can choose the name with cc -o filename prog.c). Then to run this program you type in the path to that file, in this case ./a.out (or ./filename). Should you wish to run a program that is in your home directory when you are elsewhere in the file system type in ~/a.out (or ~/filename).

Two program debuggers that are commonly available are dbx and adb. For more information see the man pages. For C code there is also a pre-compilation program checker called lint.

Mail

Unix has mail facilities, so you can leave messages for other users and read those sent to you. The supported mail package is Pine. To get information on Pine, CLICK HERE for details.

News

Most Unix systems have a news posting/reading feature often referred to as NetNews. This is a facility for sharing information between users on many systems around the world. The information is broken up into various topics (called newsgroups), such as IBM microcomputers, cooking, astrology, music, etc., which are updated on a continuous basis. This provides an almost interactive conference in each group. The supported program for reading news is nn. There are a number of NBCS documents on nn available. CLICK HERE for details.

Input/Output Redirection

Unix works on the assumption that there is always a place for output, input, and error messages. Most of the time input is taken from the keyboard and output and errors are displayed on the screen. These are commonly referred to as the standard input, output, and error. One of the nice features of Unix is that you can change where these are. Sometimes it is desirable to have your input come from a file, and have your output go to a file. This is possible using redirection. The standard input and standard output of a command may be redirected by adding one or more of the following to the end of a command:
< filename Use the file filename as the standard input. 
> filename Use the file filename as the standard output. If it does  not exist, then it is created; if it exists, its previous contents are lost. 
>> filename Use the file filenameas the standard output like > but  appends the output to the end of filename. 
> & filename Use the file filename as the standard output and standard error. Replace contents of filename with new information. 
 

 To redirect the contents of a file to be the input for a command , type

command < filename

To mail a file to jqsmith@rci.rutgers.edu the command

mail jqsmith@rci.rutgers.edu < filename

would do the job in one command.

To redirect the output of a command to a file you would type

command > file

For example, the cat command usually takes one or more files and displays them on the screen, as in: cat file1 file2. By using redirection, it possible to append file2 to the end of file1 and place the results in file3. This is done with:

cat file1 file2 > file3

which will replace any prior contents of file3 with first the contents of file1 followed by the contents of file2.

Now suppose you want to append the contents of file4 and file5 to the end of file3. This is done with

cat file4 file5 >> file3

In this case the original contents of file3 are kept and the content of other files are added to the end of file3.

When using > and >> any error messages that occur will still be sent to the terminal screen. To redirect error messages in addition to output use >& or >>&. For instance you do an ls of a directory and see

Calendar/
Docs/
F-prot
F772F90*
F90/
FAQ.eden
but when you type ls ABC you get the error message "ls: ABC: No such file or directory" to report this you want to capture the error in a file to mail to help to get help with that file you would type ls ABC >& ls.err. You then compose a message in the file msg.txt and append the error to it with cat ls.err >> msg.txt and finally mail this with mail help < msg.txt.

As a final illustration of redirection, assume the executable program test takes its input from the keyboard (standard input) and sends output to the screen (standard output). To run a data file through the program, have the output go to an output file, and to have error messages go to a separate error file, use the command

(./test < datafile > outfile) >& errfile

The parentheses group what is inside them as a separate action then the output of that (only error messages in this case) will be redirected to errfile.

For example, the cat command usually takes one or more files and displays them on the screen, as in: cat file1 file2. By using redirection, it possible to append file2 to the end of file1 and place the results in file3. This is done with: cat file1 file2 > file3 which will replace the prior contents of file3 with the contents of the other files. Now suppose you want to append file4 and file5 to the end of file3. This is done with cat file4 file5 >> file3. In this case the original contents of file3 are kept and the other files are added.

To further illustrate the concept of redirection, assume the executable program test takes its input from the keyboard (standard input) and sends output to the screen (standard output). To run a data file through the program, have the output go to a file, and to have error messages go to a separate file, use the command

(./test < datafile > outfile) >& errfile.

Pipes are another way to redirect output. A sequence of simple commands separated by '|' characters forms a pipe. The output of each command in a pipeline is used as the input for the next. For example, the pipe who | sort | lpr will work as follows: the who program will produce as output the names of currently logged in users. This output will then be the input to sort, which will put that list of users in alphabetical order. lpr will take this output and send it to the default printer.

Online Documentation

In addition to the man pages there frequently will be a directory structure devoted to online documentation. This structure usually begins at /usr/local/doc and has subdirectories devoted to different subjects which, in turn, contain the documents that may viewed or printed. To view the divisions of the rci documents directory, for example, type ls /usr/local/doc. To view the contents of one of the subdirectories type ls /usr/local/doc/subdir. To view a document found in one of the subdirectories, type

more /usr/local/doc/subdir/docname

(more is a command that allows you to view files one screenful at a time, see man more). To print a document found type

lpr -Plp16 -BH07D /usr/local/doc/subdir/docname

(meaning: lpr, print; -Plp16, to the printer lp16 in the Hill I/O area; -BH07D, the output will be placed into Hill Center bin 07 D, the file to print is /usr/local/doc/subdir/docname). In the examples above, "subdir" and "docname" should be replaced with the desired directory and document names. The command printers will display a list of printers available to the computer you are on.
 
 

Introductory Commands Online Help

man display reference manual pages man mantitle
man -k the -k option locates commands by keyword lookup man -k keyword

Commnicating With Other Users

biff be notified if mail arrives and who sent it
chfn change finger entry
finger  lists informaiton about users finger username
from see who your mail is from
mesg permit or deny messages from write and talk mesg y
pine mail program
talk "talk" to another currently logged in user: talk username talk username
who list who is on the system
write send an interactive message to another user write username

Dealing with the File System

cat concatenate and display arguments to standard output cat file
cd  change directory 
Note: given a full or relative path, set the working directory as specified; without arguments set the working directory to the login directory.
cd path
cp copy files cp fromfile tofile
diff compares two files and reports the differences diff file1 file2
du gives the number of kilobytes contained in files and directories
ispell check the spelling of the contents of a file ispell file
lpr send a job to a printer 

Basic Options: 
-Pprinter specifies which printer
-Bbin specifies the bin
-Nnote print note on burster page
-m send mail upon completion
 

lpr [options] file
ls list the contents of a directory 

Basic Options: 
-a  all files
-F mark directories with /, executable files with *, symbolic links with @, and sockets with =
-l Long listing showing protections, number of links, owner, size in bytes, and time of last modification
-g with -l shows group ownership
-s size in kilobytes
 

ls [options] file
mkdir  create a new subdirectory in the current directory mkdir subdir
more browse or page through a text file. 
Usage: press the space bar to go forward a page and type b to go back a page.
more file
mv move or rename filesmv fromfile tofile mv fromfile tofile
printers show printers available on Rutgers systems
pwd display the current directory's full pathname
quota -v  display the disk usage and limits. Use the  command without the option to display warnings only. 
rm remove (delete) files 
Note: Once a file is deleted, you can't undelete it. Use the -i option to have Unix ask if you are sure about removing the file.
rm [option] file
rmdir remove empty directories rmdir dirname

Languages

adb general purpose program debugger adb objectcode
cc C compiler cc file.c
dbx program debugger dbx executable
f77 Fortran 77 compiler f77 file.f
lint attempts to detect features of C programs that are likely to be errors, non-portable, or wasteful lint file.c
make facilitates compilation of several program modules
pc Pascal compiler pc.file.p

Work Environment

clear clear the terminal screen
passwd change the login password
reset reset the terminal screen
script make a copy of everything displayed on the screen
Usage: to end scripting, type a <CTRL>d; 
Note: useful for capturing error messages and program execution.
script filename

Editors and Text Formatters

emacs the emacs editor emacs [filename]
latex/tex test formatting and typesetting
teach-emacs the emacs online tutorial

Communications

ftp transfer files between networked systems ftp computer
kermit kermit file transfer
telnet login on another computer on the network telnet computer

News Programs

lookfor find a newsgroup using a keyword search
nn newsreading program
postnews news posting utility

For More Information

Questions should be directed to the OIT/NBCS Help Desk (732-445-HELP). You can also send electronic mail to the address help on any OIT system (e.g., help@eden.rutgers.edu or help@rci.rutgers.edu) or post a message to the newsgroup ru.comp.qa.news .


<hr>
Copyright © 2008 Rutgers, The State University of New Jersey, NBCS Help Desk. All rights reserved.

Rutgers Logo

webmaster@nbcs.rutgers.edu
06/22/05