Unix Primer Unix Primer

Department of Chemical Engineering

Indian Institute of Science



1  Getting Started
    1.1  Introduction
    1.2  Logging in to a system
    1.3  Logging out
    1.4  Changing your password
    1.5  Remote Login
2  Files and Directories
    2.1  Files
    2.2  Directories
    2.3  Transferring files
    2.4  Other Commands
3  The vi Editor
    3.1  What is a text editor?
    3.2  Using the vi editor
    3.3  Operating Modes
    3.4  Saving and Quitting
    3.5  Commands for Editing
4  Compiling and Executing Programs
    4.1  Further References
5  Practice Sessions
    5.1  Session 1
    5.2  Assignment
    5.3  Session 2


This tutorial on Unix is designed to help you get over ``starting problems'' one generally has on encountering computers running Unix for the first time. Once this step is accomplished, the learning of the more arcane secrets & tools of the Unix world becomes an enjoyable, and rewarding experience.

This tutorial will give you sufficient information to use a Unix machine to create files, compile source code (you will be needing this for computer assignments given by your instructors), accessing computers in the Supercomputer Education and Research Center (SERC), etc.

This material is more or less sufficient (well ... almost!) to get you through the various jobs you will be required to do in the institute. But you may want to learn more; this tutorial provides information about where you can find additional material. The most important in our opinion are the people around you. Most often you will find that the person on the next terminal, your friend or your senior will be of help; this forms the dominant mode by which information is transmitted, and you will find ready made solutions to many of the problem you may face-these solutions have often been given to the person by the same means! Of course, the chain has to stop somewhere; they are the computer Gurus.

We hope this tutorial will be of help, and that you will have a happy and enjoyable computing time at IISc.


Following styles will be used in this document to distinguish user and terminal response.
User response (in bold)
  % User response at the command prompt (%)
  Terminal output (in typewriter font)

Chapter 1
Getting Started

1.1  Introduction

The core of a computer that acts as a link between you and the hardware is called the operating system of the computer. There are two major operating systems-Unix and DOS (Disk Operating System). This manual restricts itself to giving you the basics of the Unix operating system.

Unix is like a hostel with several rooms in it. Similar to the room numbers, you are identified by the system by a username. Just like you would be given a key to your room, so that nobody else is allowed to access your room, you will be given a password, which is known only to you.

An ordinary user has several restrictions like, limited access to other areas, not all programs can be executed, etc. But there is one super-user, known as the root, who has absolutely no restrictions. The root manages the system and is the one who gives you an account to work on the computer.

1.2  Logging in to a system

Entering into one's area in a system is known as logging in. Before you can use the system, you will need to have been given a username and a password.

Example To login to an account type:


This logs in the person of username tintin in the system. Unix is case sensitive. If your username is asterix, do not enter ASTERIX or Asterix. Your password is never displayed on the screen. If it were, other users might see it!

Problems with logging in

   login incorrect

Either you made an error in typing your username or password or the < CAPS-LOCK > key was on and everything was being sent to the system in uppercase letters.

1.3  Logging out

To finish using a Unix system you must ``log out''. To logout enter the command logout or exit. If this does not work press Ctrl-d.1

CAUTION: Do not leave the computer without logging out. Do not ever switch off any Unix system.

Hung Terminal

Sometimes it might happen that your terminal does not respond to the keyboard. The terminal is then said to be ``hanging''. The computer might be working, and you might be still logged into it. To logout in such situations you have to login to the same computer from some other terminal (see section on Remote Login) and terminate your login forcibly using the kill command. To see if you have logged in more than once type:
% ps u

This displays all the process currently being executed by the user.
chesen   11061 pts/2 S    19:47:54  0:00 -csh
chesen   11618 pts/2 R    19:50:14  0:00 ps u
chesen   15387 pts/0 S    19:47:08  0:00 -csh
chesen   16179 pts/0 S    19:47:53  0:00 vi Unix.tex

The number of logins is the number of times -csh or (-sh or -bash) appears under the COMMAND. In this example the user chesen has logged in twice. To terminate all the processes enter:
% kill -1 -1
  or in certain systems
  % kill -1 -

This will terminate all the logins-the present one and all the other previous ones under your username-and return you to the computer from which you issued the remote login command.

1.4  Changing your password

Your password is important; it stops other users from gaining access to your account. Never give your password to anyone-not even to your best friend. If you do, you may have your access to the system suspended or withdrawn. As an extra security measure change your password at regular intervals. Be sure to read the guidelines below before creating a new password for yourself.

Example To change your password enter the command passwd and then respond to the prompts by entering your old password followed by your new one. You are then asked to confirm your new password.

% passwd
  Old password:enter your current password
  New password:enter your new password
  Retype new password: re-enter your new password

The passwords will not appear on the screen as you type, to prevent other people from seeing them. If you make a mistake, the message
Mismatch - password unchanged.

is displayed and your password remains unchanged. Try again.

Guidelines for creating a password



1.5  Remote Login

It is possible to communicate from one computer to another through what is called a network. You probably have heard of local area networks (LANs) in which communication is limited to iwithin a department or an office. The Internet is a network spread through out the world. Almost all the computers in IISc are on the Internet, which means you can communicate directly between a computer here and one anywhere in the world! You can transfer or receive files, look at web pages, read e-mails, etc.

Every computer on the Internet is identified by a name and an associated number known as IP (Internet protocol) address. This is like a dial-up number, to be specified whenever you need to contact a computer.

For example the main computer of our department is called chemeng.iisc.ernet.in and has a number, they can be used interchangeably. Some machines within the department or the institute have a shortened name too, in this case chemeng. This section describes how to login to a remote computer through a network.

Logging in using rlogin

To login to your account on a remote system use the command:

%  rlogin remote_system_name

To login to a remote system using a different username, use the command:
% rlogin remote_system_name  -l username

You are prompted for the password to your account on that system.

Examples To login to the remote system prithvi under the same username use:

%   rlogin prithvi

This logs you into your account on the remote system named prithvi. The username on this system is identical to the username on the system you are currently using. In case the username is different in the remote system, then press < RETURN > which will prompt for the username. To login to the remote system ibms9 using the username cheuser :
%   rlogin ibms9 -l cheuser

Logging in using telnet

telnet enables you to connect your computer to another remote system. To login to your account on a remote system use the command:
% telnet remote_system_name
You are prompted for your username and password for your account on the remote system. Once you are logged in, you can begin to issue commands on the remote system.

Examples To connect to an account on the remote system chemeng type:

%   telnet chemeng
  %   telnet
  The following prompt is then displayed.

At this prompt if the user name is given then, you will be asked for a password.

Chapter 2
Files and Directories

2.1  Files

A file may be a program, a database, a dissertation, a reading list, a simple letter etc; its name consists a collection of letters, numbers and special characters. Sometimes you may import a file from elsewhere, for example from another computer. If you want to enter your own text or data, you will start by creating a file. Whether you copied a file from elsewhere or created your own, you will need to return to it later in order to edit (see next chapter) its contents.

Ordinary files

This type of file is used to store your information, such as some programs you have written or an image you have drawn. This is the type of file that you usually work with. Files which you create belong to you; you are said to ``own'' them-and you can set access permissions to control which other users can have access to them. A file is always contained in a directory.

We will learn more about access controls in a separate section.

2.2  Directories

A directory is a file that holds other files and other directories. You can create directories in your home directory to hold files and other sub-directories. Having your own directory structure gives you a definable place to work from and allows you to structure your information in a way that makes sense to you. Directories which you create belong to you-you are said to ``own'' them-and you can set access permissions to control which other users can have access to the information they contain.

Home Directory

A Unix system can have many users accessing it at one time. As a user you are given a home directory in which you are placed whenever you log on to the system. You are allowed to create, copy, remove files and subdirectories from here.

Users' home directories are usually grouped together under a system directory such as /home. A large Unix system may have several hundred users, with their home directories grouped in subdirectories according to some scheme such as their organisational department. For example consider Anil's and Preethi's home directories. Although both Anil and Preethi are registered as users on their local Unix system, they belong to different categories; Anil is a student while Preethi is a faculty in a department. The directory tree may look like:


The full pathname to Anil's home directory is /home/students/anil-that of Preethi's is /home/faculty/preethi.

Changing to another directory

To change your current working directory use the command:

%   cd pathname 

where pathname specifies the directory that you want to move to. The pathname can be given as either a full pathname or a relative pathname.

Examples To move down one level to a subdirectory:

%   cd expt

This moves you down one level from your current directory to the subdirectory expt. To move up one level of the directory tree:
%   cd ..

Every directory contains a hidden directory.. (dot dot) that is a shorthand name for this directory's parent directory. Using this shorthand name enables you to move up the directory tree very quickly without having to enter long pathnames. To move to another directory using a relative pathname:
%     cd ../math

This moves you up one level in the directory tree and then moves you into the subdirectory math. To move to a directory using a full pathname:
%  cd /home/students/anil/expt

This moves you to the directory expt which is in the home directory of the user anil. To go directly to your home directory from anywhere in the tree:
%   cd

A short form to represent your home directory is the ~. From any directory a command,
%   cd ~/flumech

will take you to the directory flumech which is under your home directory. Similarly from any directory,
%  cd ~preethi/papers

will take you to the papers directory under the user preethi.

Making directories

To create a directory you use the mkdir command. It is a good practice to group files and put them in separate (sub)directories. For example you may like to put the assignment files of mathematics in one directory and that of fluid mechanics in another.


To make a directory in the current directory:

%  mkdir math

This creates a new directory math in your current working directory. To make a new directory in the parent directory:
% mkdir ../flumech

This creates the directory flumech in the parent directory of the current working directory. In effect, this creates a new directory at the same level of the file system as the current directory.

Removing directories

To remove a directory use the command:
%   rmdir directory_name

The directory must be empty before you can delete it. You will need to remove any files and subdirectories that it contains. To remove a directory that contains files use the command:
%  rm -r directory_name

This deletes all the contents of the directory including any subdirectories. Be very careful in using commands such as these.

CAUTION If you remove a directory that still holds files there is no way to retrieve it. To avoid this, always use the rm command together with the -i option.

%    rm -ir directory_name

List the files in a directory

You can use the ls command to list the files in a directory:
% ls [-options] [directory_name]

By combining different command options you can display as little or as much information about each file as you need.

ls: command options

Option Action
-a list (all) hidden files
-F show directories with a trailing '/' executable files with a trailing '*'
-l long listing giving details about files and directories
-t sort by time modified instead of name

Using a long listing

To get more information about each file and directory, use the command:

%  ls -l

This gives you a long listing about each file and directory, giving information about its: access permissions, number of links, owner, group ownership, size and date and time last modified.

Copying files in the same directory

To create an exact copy of a file use the cp (copy) command.
% cp [-options] source destination 

The source is the name of the file to be copied; the destination is the name of the file in which the copy is to be placed.

Take care! Take care what name you give to the file copy. If you choose the name of a file that already exists it will be overwritten by the new copy! To avoid this,

Always use the cp command together with its -i option. This will prompt you for confirmation whenever the copy would overwrite an existing file.
Create a command alias for the cp command. Including the -i flag in the alias will make sure that you are always prompted for confirmation whenever the copy would overwrite an existing file.

% alias cp 'cp -i'

For more details on alias command see at the end of this chapter.

Examples To copy a single file in the current directory:

%   cp notes sect3.txt

This makes a copy of the file notes in the file sect3.txt. To copy a file using the -i option:
%   cp -i notes part2.txt

This makes a copy of the file notes in the file part2.txt. If this file exists, the message
 part2.txt: File exists

is displayed. The file is not copied. To copy a file from your current working directory to a subdirectory:
% cp fig2 part2/figure2

This copies the file fig2 from your current working directory to the file figure2 in the subdirectory part2. To copy a file to the parent directory:
% cp mail.txt ..

This copies the file mail.txt to the directory immediately above the current working directory. The .. (dot dot) is shorthand for the parent directory. To copy a file from a subdirectory to a directory elsewhere in the file system:
% cp docs/vi.Z  /tmp

This copies the file vi.Z in the subdirectory docs to the directory /tmp. To copy a file to your home directory:
% cp /usr/local/doc/ue.txt $HOME
  % cp /usr/local/doc/ue.txt ~

This copies the file ue.txt to your home directory. $HOME is the environment variable that contains the value for your home directory.

Removing files

To remove a file use the command:

%  rm [filename1] [filename2] [...]

You cannot remove a file in another user's account unless they have set access permissions for the file which allow you to delete them. Use the -i (interactive) option which makes the command prompt you for confirmation that you want to remove each file.

Examples To remove a single file:

%    rm help.txt

This removes the file help.txt from the current directory. To remove several files interactively:
%   rm -i *.fm

This will prompt you to confirm that you want to remove a file from the current directory when-ever that file has the suffix .fm. Answering y will delete the file. The file is not deleted if any other response is given.

Viewing Files

To display a file use the command:

%    more filename

This will display the first screen full of the file. The prompt

is given in the bottom left-hand corner of the screen. You can now use one of the following commands:

< space bar > Display next screen full of text.
< RETURN > Display next line of text.
q Exit from more: this can be done at any time.
d Scroll forwards about half a screen full of text.
b Skip backwards one screen full of text.
h,? Display a list of commands (help).

Renaming Files

You can rename a file or ``move'' it to another name using the
mv command.
 To rename a file:
  %    mv junk precious

This renames the file junk as the file precious. To be prompted when using the mv command:
%    mv -i draft1.txt part1.txt
  remove part1.txt? n

This command prompts you to confirm that you really want to move the file draft1.txt to the file part1.txt; a file with that name already exists. Entering n cancels this action: a y confirms it. To move a file to another directory:
%  mv precious treasure

This moves the file precious from the current directory to the directory treasure. To move a file to another directory and rename it:
%    mv jewel casket/amethyst

This moves the file jewel to the filename amethyst in the subdirectory casket.

2.3  Transferring files

Often you may need to take a file from one system to another connected through a network. You can transfer files between hosts using the ftp (file transfer protocol) command. This program is the standard means of transferring files between hosts on the Internet network.

Transferring files using the ftp program

To begin using the ftp program enter the command:
%    ftp

The program starts and the ftp > prompt is displayed. You are now ready to give commands to the ftp program.

Opening Connection

To open a connection to the ftp server on a remote host:
ftp >  open chemeng

Connected to chemeng. 220 chemeng FTP server ready.

Name (chemeng:guest): guest 331 Password required for guest. Password: not displayed

230 User guest logged in. ftp >

This shows the user opening a connection to their account on the remote host chemeng. Notice that you must supply your username and password for your account on the remote host. After successful login, the user is placed in his/her home directory on the remote machine. The user now has simultaneous access to two home directories-in local and remote machine. To close the connection type quit.

Sending Files

To send a single file from your current directory on the local host to the current directory on the remote host:

ftp> put myfile

This copies the file myfile from your current directory on the local host to your current directory on the remote host. If you don't give a name for the remote-file the name of the local-file you are copying is used to name the remote-file. To use file pathnames to define both the local-file and the remote-file:
ftp> put REPORTS/april  reports/month4

This copies the contents of the file april in the sub-directory REPORTS, which is in your current directory on the local host to the file month4 in the sub-directory reports, which is in your login directory on the remote host. To send multiple files to a remote host:
ftp> mput *.xdh
  mput index.xdh?

This prompts you to confirm for each file with the extension *.xdh in the current directory in your local host that it is to be copied to your current directory on the remote host. Entering a y copies the file; an n cancels this.

Getting Files

To get a single file from the remote host:

ftp> get news

This transfers a copy of the file news from the current directory on the remote host to your current directory on the local host. If you don't give a local filename the filename of the remote file you are copying is used to name the local file. To get a file from a subdirectory on the remote host:
ftp> get reports/quarterly/march  tmp/month3

This transfers a copy of the file march in the remote subdirectory reports/quarterly to the local filename month3 in the subdirectory tmp. To get several files from a remote host:
ftp> mget *.f

This transfers a copy of each file with the filename extension .f in the current directory on the remote host to the current directory on the local host. Entering a y copies the file; an n cancels this copy. To get a help of the ftp commands available use:
ftp> ?

2.4  Other Commands

Look-up Online Manual

The command man gives you access to an on-line manual containing a complete description of every command available on the Unix system. man can also provide you with one line descriptions of commands specified by name; or for all commands whose description contains any of a set of keywords.

Examples To search the online manual for a command with the keyword mail

%   man -k mail

To display the manual page for the cp (copy) command use:
% man cp
     --More--48 %

appearing at the bottom left of the screen means that only 48% of the man page is displayed. Press the space bar to display more of it or type q to quit.

The alias command

The alias command allows you to define shortcuts to save your time. For example, to use the rm command in interactive mode you might use:
% alias rm 'rm -i'

By typing this the alias command allows you to type rm as a shortcut.

It is possible to specify a given set of commands that are to be executed every time you login. These are kept in files such as the .profile and .cshrc. You may get these files from senior users who have customised useful and important commands.

Chapter 3
The vi Editor

3.1  What is a text editor?

While using Unix you will often want to create a text file and then change its content in some way. A text editor is a program for inserting or amending text in a file. A text editor is not a word processor although some text editors do include word processing facilities.

3.2  Using the vi editor

The vi editor (pronounced ``vee eye'') is available on all Unix systems. Being able to use vi ensures that you will always have an editor available to you.

Examples of starting vi and opening files

To open an existing file for editing:

%vi intro

This opens the file intro for editing. This file already exists in the current directory. To create a file to edit:
% vi part2

This command starts vi with a new file named part2. This file does not exist in the current directory until it is written to using the write command. To simply start vi use:
% vi

This command places you in vi. You can then enter and edit text and write it to a file.

3.3  Operating Modes

There are two modes in which you use vi.

Command mode

This is the mode you are in whenever you begin to use vi. In this mode commands are used to move around and edit text objects such as words, sentences and paragraphs. Pressing the ESC key returns you to command mode.

Insert mode

This is the mode you use to type (insert) text into a buffer. There are several commands that you can use to enter this mode and the various ways are discussed below.

3.4  Saving and Quitting

Saving changes

To save changes while editing a file type in the command mode:

:w  < RET > 

and then continue editing. To save changes to another file:
:w filename  < RET > 

Quitting vi

To quit the vi editor from the command mode:

:q  < RET > 

But if you have edited (changed) the file you will be prompted with the message:
No write since last change

You can either save the changes and quit by typing :wq or quit without saving any of the changes you have made by typing :q! in the command mode.

3.5  Commands for Editing

Press the ESC key to enter command mode before using the following commands.

Cursor movement commands

To move the cursor Do this
one character to the left of the current cursor position h
down by one line from the current cursor line j
up by one line from the current cursor line k
by one character to the right of the current cursor position l
to the next word w
to the previous word b
down by 12 lines CTRL d
up by 12 lines CTRL u
up by 24 lines CTRL b
down by 24 lines CTRL f
to the end of the line $
to the beginning of the line 0 (zero)

List of insert commands

These are the commands to go from command mode to the insert mode.

To do this ... Command
Insert text after the cursor a
Insert text at the cursor i
Append text at the end of the current line A
Insert text at the start of the current line I
Open a new line below the current line o
Open a new line before the current line O

Searching for text

To search Do this ...
forward for a pattern /pattern < RETURN >
backward for a pattern ?pattern < RETURN >
repeat search n
repeat search in reverse direction N

Deleting characters

To delete Do this
current character x
previous character X or dh

Deleting words and lines

To delete Do this
current word dw
previous word db
entire line dd
to start of line d0 (zero)
next n lines ndd
rest of the line from current position D

Undeleting text

To get back a word or line that you have just deleted enter the command:

p  or u

Modifying Text

To modify Do this
Replace character at current position with character x rx
the current line cc
the current word cw
rest of the line from current position C
to join lines J

``Cut-and-Paste'' commands

To do this.. do this
Yank current line into buffer yy
Yank next n lines into buffer nyy
Put back yanked text after cursor p
Put back yanked text before cursor P

Yank in this context means to copy a certain number of lines to a buffer.

Line Number Search

To do this.. do this
Display current line number CTRL g
Go to line number n n G

Chapter 4
Compiling and Executing Programs

A source code is a program written in high level languages such as FORTRAN or C. Converting this code to a form that can be understood by the computer is called compilation. The computer then executes the resulting file and produces an output. Here we will see how FORTRAN and C programs should be compiled and executed in a Unix platform.

Example 1 - FORTRAN Consider the file sample.f:

C sample.f : A sample FORTRAN code.
        IMPLICIT none
        REAL a, b, c
        a = 5.0
        b = 6.0
        c = a*b
        WRITE(*, *)'a =', a
        WRITE(*, *)'b =', b
        WRITE(*, *)'a * b  =', c

To compile this file use the command
% f77 sample.f

This by default creates an executable file a.out. To execute this file type:
% a.out

To create an executable file of a different name (such as sample.e) use:
% f77 sample.f -o sample.e

This FORTRAN code must give the following output on execution.
 a =  5.
 b =  6.
 a * b  =  30.

Example 2 -C Consider the file sample.c

/*  sample.c: A sample C code */

void main(){
  float a=5.0,b=6.0,c;
  c = a*b;
  printf("a = %.3f\nb = %0.3f\na * b = %.3f\n",a,b,c);

To compile this file use the command
  % cc sample.c

This by default creates an executable file a.out. To execute this file type:
% a.out

To create an executable file of a different name (such as sample.e) use:
% cc sample.c -o sample.e

This C code must give the following output on execution.

a = 5.000
b = 6.000
a * b = 30.000

4.1  Further References

  1. Thomas, Rebecca and J Yates, User Guide to Unix , 2nd Edn. McGraw Hill (Asian Edition), 1996.
  2. Lipschutz, S and A Poe, Schaum's Series: Programming with Fortran including Structured Fortran.
  3. Gottfried, Byron S, Schaum's Series: Programming with C.
  4. Your Senior Neighbour!

Chapter 5
Practice Sessions

Read Unix Primer document carefully before you start practice session. If you have any difficulty during the session, consult the convener in the laboratory you are working.

5.1  Session 1

  1. Using telnet command (page pageref), login into ibm machines in SERC. Use telnet ibms*, where * is the number of the laboratory in which you are working.
  2. Username for the account which you will be using is chesen. The password required for login will be keyed in by the convener in your laboratory.
  3. See who else is logged in along with you. Try w or who. Logout of the system and try alternative commands such as rlogin and rsh.
  4. Find out what is the name of the Home directory in which you are. Use pwd command. Examine this directory to see how many files and directories are there in this account. Use ls command (page pageref) with various options.
  5. There are 5 directories, lab1 through lab5 in home directory. Change to the directory (page pageref) which corresponds to the laboratory in which you are working, for example, if you are in laboratory 1, change to directory lab1.
  6. Create a directory (page pageref) using your name. Copy file named vi.tutor from home directory to your working directory. This will be your working directory for the session. Please work in this directory only.
  7. Work with various commands for copying, renaming and removing files (page pageref). Be careful in using any remove command. Try to get online help for these commands (page pageref).
  8. Logout from the machine and transfer some files from your local machine to ibms* using ftp (page pageref) and receive some other files to your local machine.
  9. Edit the file vi.tutor and get used to the various commands of vi. Create a new file and type the sample programs (page pageref). You will be using vi editor very often in future, so spend as much time as you can with the commands.

5.2  Assignment

In the next session, you will be compiling a program and running it. Here is a small exercise for you. Find the root of the polynomial function,
f(x)  = x3   -  4x2   +  x   +  2    =   0,
(5.1) using Newton-Raphson iteration scheme given by
xk+1   =  xk   -    f(xk)

(5.2) where xk+1 is the new estimate of the root based on the current estimate xk. Use stopping condition |xk+1 - xk | 10-5. Your program should contain a subroutine which calculates values of f(x) and f(x) given the value of x. Think about the problem while you are enjoying dinner today. Be ready with a fortran code so that you can try it out in the next session. If you are more familiar with the C language, feel free to use it.

5.3  Session 2

  1. Compile the sample programs that you have entered previously. Execute the program and verify the results.
  2. Find the roots of the polynomial given in the assignment. Type the program using vi editor, compile it and run it. If compilation shows errors, examine the error and the manner in which it is indicated by the compiler. Each error is identified by the filename, line number and the column number in the program. Go to the line number using commands in vi and rectify the errors and compile till you get the exact solution. Remember you have to find 3 different roots.

We hope that you would have gained a good exposure to Unix through these sessions. There are many features of Unix which you will learn only through practice. We have compiled information about some of the advanced features which is available in a small document kept in our library.


1This means press ``d'' while pressing the ``control key''

This document was originally written with LATEX tags by L Srinivasa Mohan and P Sunthar with the help of a few WWW resources and books. It was translated by TTH, version 3.01. on 12 Aug 2001, 19:38. You are free to download this document.